QComboBox设置行高、代理

QComboBox设置行高、代理

1.第一种(行高)

QComboBox行高就是按QIcon的高度和字体QFont的高度取最大值。所以,调整行高可以通过设置一个透明的icon并设定其高度。如下:

QComboBox comboBox;
QPixmap pix(1, 50);
pix.fill(Qt::transparent);
QIcon icon(pix);
comboBox.setIconSize(QSize(1, 50));
comboBox.addItem(icon, "test1");
comboBox.addItem(icon, "test2");
comboBox.setStyleSheet("QComboBox QAbstractItemView {outline: 0px;}");//去掉选项虚框
comboBox.show();

2.第二种(行高)

combox.setView(new QListView());
combox.setStyleSheet("QComboBox QAbstractItemView::item{height:50px; }");
comboBox.addItem("teste");

3.第三种(行高)

QStyledItemDelegate *delegate = new QStyledItemDelegate(this);
ui->comboBox->setItemDelegate(delegate);
ui->comboBox->addItem("test1");
ui->comboBox->addItem("test2");
ui->comboBox->setStyleSheet("QComboBox QAbstractItemView::item{min-height:50px;}");

4.第四种(行高,可以自定义选项风格,比较灵活)

代码来自http://blog.sina.com.cn/s/blog_a6fb6cc90101ed6n.html

实现方案:QListWidget + QListWidgetItem + QComboBox

(1)设定代理组成

AccountItem::AccountItem(QWidget *parent):QWidget(parent)
{
    mouse_press = false;
    account_number = new QLabel();
    delede_button = new QPushButton();

    QPixmap pixmap(":/loginDialog/delete");
    delede_button->setIcon(pixmap);
    delede_button->setIconSize(pixmap.size());
    delede_button->setStyleSheet("background:transparent;");
    connect(delede_button, SIGNAL(clicked()), this, SLOT(removeAccount()));

    QHBoxLayout *main_layout = new QHBoxLayout();
    main_layout->addWidget(account_number);
    main_layout->addStretch();
    main_layout->addWidget(delede_button);
    main_layout->setContentsMargins(5, 5, 5, 5);
    main_layout->setSpacing(5);
    this->setLayout(main_layout);
}

AccountItem::~AccountItem()
{
}

void AccountItem::setAccountNumber(QString account_text)
{
    account_number->setText(account_text);
}

QString AccountItem::getAccountNumber()
{
    QString account_number_text = account_number->text();
    return account_number_text;
}

void AccountItem::removeAccount()
{
    QString account_number_text = account_number->text();
    emit removeAccount(account_number_text);
}

void AccountItem::mousePressEvent(QMouseEvent *event)
{
    if(event->button() == Qt::LeftButton)
    {
        mouse_press = true;
    }
}

void AccountItem::mouseReleaseEvent(QMouseEvent *event)
{
    if(mouse_press)
    {
        emit showAccount(account_number->text());
        mouse_press = false;
    }
}

(2)添加代理至QComboBox

account_combo_box = new QComboBox();
list_widget = new QListWidget();
account_combo_box->setModel(list_widget->model());
account_combo_box->setView(list_widget);
account_combo_box->setEditable(true);//设置QComboBox可编辑
for(int i=0; i<3; i++)
{
    AccountItem *account_item = new AccountItem();
    account_item->setAccountNumber(QString("safe_") + QString::number(i, 10) + QString("@sina.com"));
    connect(account_item, SIGNAL(showAccount(QString)), this, SLOT(showAccount(QString)));
    connect(account_item, SIGNAL(removeAccount(QString)), this, SLOT(removeAccount(QString)));
    QListWidgetItem *list_item = new QListWidgetItem(list_widget);
    list_widget->setItemWidget(list_item, account_item);
}

(3)实现代理选项进行的操作

//将选项文本显示在QComboBox当中
void LoginDialog::showAccount(QString account)
{
    account_combo_box->setEditText(account);
    account_combo_box->hidePopup();
}

//删除帐号时,弹出提示框,与用户进行交互,告知是否确定要删除此帐号的所有信息!
void LoginDialog::removeAccount(QString account)
{
    account_combo_box->hidePopup();
    msg_box->setInfo(tr("remove account"), tr("are you sure to remove account?"), QPixmap(":/loginDialog/attention"), false);
    if(msg_box->exec() == QDialog::Accepted)
    {
        int list_count = list_widget->count();
        for(int i=0; i
        {
            QListWidgetItem *item = list_widget->item(i);
            AccountItem *account_item = (AccountItem *)(list_widget->itemWidget(item));
            QString account_number = account_item->getAccountNumber();
            if(account == account_number)
            {
                list_widget->takeItem(i);
                delete item;
                break;
            }
        }
    }
}

5.第五种(Model/View + QStyledItemDelegate)

//把表格的背景调成黄蓝相间  
//这种方法是在网上看到的,用起来还真方便啊  
view->setAlternatingRowColors( true );  
view->setStyleSheet("QTableView{background-color: rgb(250, 250, 115);alternate-background-color: rgb(141, 163, 215);}");  

猜你喜欢

转载自blog.csdn.net/wyy626562203/article/details/79707410