QT控件之QComboBox(下拉框相关)

本文介绍了Qt库中的QComboBox控件,包括如何使用`addItems`方法添加选项,通过`currentText`获取当前选中项,利用`count`获取选项总数,以及用`currentIndex`获取当前选中项的索引。通过示例代码展示了添加选项和获取信息的实现过程,帮助开发者更好地理解和运用QComboBox。
 QComboBox提供了下拉列表框的控件。下面简单介绍几个的方法和属性。

  (1)addItems

  void addItem(const QString &text, const QVariant &userData = QVariant())

  void addItem(const QIcon &icon, const QString &text, const QVariant &userData = QVariant())

  在列表的最后一项添加一个文本内容为test选项

  (2)currentText

  QString currentText() const

  返回下拉列表框中当前选中的文本

  (3)count

  int count() const

  返回当前列表框中选项数量

  (4)currentIndex

  int currentIndex() const

  返回当前列表框中选中文本的序号


简单的案例:
#include "widget.h"
#include <QComboBox>
#include <QLayout>
#include <QDebug>
 
Widget::Widget(QWidget *parent)
    : QWidget(parent)
{
    QComboBox *combobox = new QComboBox(this);
    combobox->addItem(tr("Circle"));
    combobox->addItem(tr("Pology"));
 
    QGridLayout *mainLayout = new QGridLayout(this);
    mainLayout->addWidget(combobox,0,0);
 
    qDebug() << "Now there are " << combobox->count() << "Items";
    qDebug() << "The current item is" << combobox->currentText();
}
 
Widget::~Widget()
{
}

结果:
在这里插入图片描述
程序输出:

 Now there are 2 Items
 The current item is "Circle"

代码2:

    //不带图标写法
    ui->comboBox->addItem("A1");
    ui->comboBox->addItem("A2");
    ui->comboBox->addItem("A3");
    ui->comboBox->addItem("A4");
    ui->comboBox->addItem("A5");
    ui->comboBox->addItem("A6");
    //带图标写法
    ui->comboBox->addItem(icon,QString::asprintf("Item %d",i)); 
    //可以使用QStringList 一次写入多个数据
    QStringList strList;
    strList<<"A1"<<"A2"<<"A3"<<"A4"<<"A5"<<"A6";
    ui->comboBox->addItems(strList);

在这里插入图片描述
ui->comboBox->setCurrentIndex(2);
在这里插入图片描述

获取ComboBox控件总索引数
    //索引为1-6
    int intc = ui->comboBox->count();
    QString StrIntN=QString::number(intc);
    QMessageBox::information(this, "comboBox", StrIntN, QMessageBox::Ok);

在这里插入图片描述

获取ComboBox控件当前选中索引:
    //索引为0-5
    int index = ui->comboBox->currentIndex();//获得索引
    QString StrIntN=QString::number(index);
    QMessageBox::information(this, "comboBox", StrIntN, QMessageBox::Ok);

在这里插入图片描述

获取当前内容:
QMessageBox::information(this, "comboBox", ui->comboBox->currentText(), QMessageBox::Ok);

在这里插入图片描述

ui->comboBox->clear(); //清除列表

在这里插入图片描述

Qt 中设置 `QComboBox` 下拉框选项的选中与未选中状态的样式,可以通过样式表(QSS)实现。`QComboBox` 的下拉选项本质上是 `QAbstractItemView` 的子类(通常是 `QListView` 或 `QTreeView`),因此需要针对其下拉菜单的视图部分进行样式定义。 可以通过以下方式设置 `QComboBox` 的下拉选项的样式: ```css QComboBox QAbstractItemView { outline: 0px; background-color: #ffffff; border: 1px solid #cccccc; } QComboBox QAbstractItemView::item { background-color: transparent; padding-left: 5px; padding-right: 5px; } QComboBox QAbstractItemView::item:selected { background-color: #3a78f2; color: white; } QComboBox QAbstractItemView::item:hover { background-color: #e0e0e0; } ``` 上述样式表定义了以下内容: - `QComboBox QAbstractItemView`:设置下拉列表的整体样式,包括背景颜色和边框。 - `QComboBox QAbstractItemView::item`:设置每个下拉项的默认样式。 - `QComboBox QAbstractItemView::item:selected`:设置选中项的背景颜色和文字颜色。 - `QComboBox QAbstractItemView::item:hover`:设置鼠标悬停时的背景颜色。 在代码中设置样式表的示例: ```cpp QComboBox *comboBox = new QComboBox(this); comboBox->addItem("Option 1"); comboBox->addItem("Option 2"); comboBox->addItem("Option 3"); comboBox->setStyleSheet( "QComboBox QAbstractItemView {" " outline: 0px;" " background-color: #ffffff;" " border: 1px solid #cccccc;" "}" "QComboBox QAbstractItemView::item {" " background-color: transparent;" " padding-left: 5px;" " padding-right: 5px;" "}" "QComboBox QAbstractItemView::item:selected {" " background-color: #3a78f2;" " color: white;" "}" "QComboBox QAbstractItemView::item:hover {" " background-color: #e0e0e0;" "}" ); ``` 如果需要更复杂的样式控制,例如为不同项设置不同的属性或状态,可以通过子类化 `QStyledItemDelegate` 并设置自定义委托来实现。 ### 注意事项 - `QComboBox` 的下拉菜单样式可能受到系统主题的影响,因此在不同平台上可能显示效果略有不同。 - 使用 `QAbstractItemView::item:selected` 可以很好地控制选中项的外观,但需确保没有与其他样式冲突。 如果样式表未生效,可以检查是否需要启用控件的样式背景绘制,例如通过设置 `setAttribute(Qt::WA_StyledBackground, true)` 来确保控件支持样式表背景绘制[^1]。 ---
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值