Qt 实战(8)控件 | 8.1、QComboBox


前言:

QComboBox 是 Qt 框架中一个非常实用的控件,它允许用户从一个下拉列表中选择一个项目。这个控件广泛应用于需要用户从预设选项中进行选择的界面设计中,如设置对话框、表单填写等场景。下面,我们将详细介绍 QComboBox 的基本用法、功能特性以及如何通过编程方式操作它。

一、QComboBox

1、简介

QComboBox 继承自 QWidget ,它结合了 QListWidget(列表显示)和 QPushButton(按钮点击)的功能,提供了一个下拉式的选择框。用户可以通过点击下拉箭头来查看所有可用的选项,并通过点击列表中的某个项目来选中它。

2、功能特性

下面介绍下QComboBox 常用的一些功能,如下:

2.1、添加和移除项目

// 添加条目
void addItem(const QString &text, const QVariant &userData = QVariant());
void addItem(const QIcon &icon, const QString &text, const QVariant &userData = QVariant());
void addItems(const QStringList &texts);

// 删除条目
void removeItem(int index);
void clear();

2.2、设置和获取当前选中项

// 获取当前选项
int currentIndex() const;
QString currentText() const;

// 设置当前选项
void setCurrentIndex(int index);
void setCurrentText(const QString &text);

2.3、模型/视图架构

QComboBox 支持模型/视图架构,这意味着你可以使用自定义的模型来管理选项,提供更大的灵活性和数据控制能力。但是,如果是存储一些简单的数据,可以使用下面这些成员方法,如下:

// 获取数据
QVariant currentData(int role = Qt::UserRole) const;
QVariant itemData(int index, int role = Qt::UserRole) const;
			
// 设置数据		
void setItemData(int index, const QVariant &value, int role = Qt::UserRole);

2.4、信号与槽

QComboBox 提供了多个信号,这些信号在用户更改选择时发出,允许开发者根据用户的选择执行特定的操作,如下:

// 切换选项触发下面的信号
void currentIndexChanged(int index)
void currentIndexChanged(const QString &text)

// 当前选项的内容发生变化,触发该信号
void currentTextChanged(const QString &text)

3、总结

QComboBox 是 Qt 框架中一个功能强大且易于使用的控件,它为开发者提供了一种方便的方式来让用户从一组预定义的选项中进行选择。通过添加、移除选项,设置当前选中项,以及响应选项变化等操作,QComboBox 可以轻松地集成到各种复杂的界面设计中,提升用户体验。无论是简单的应用程序还是复杂的系统,QComboBox 都是处理选择问题的理想选择。

  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以在 QTreeView 的 delegate 中重新实现 createEditor() 函数,使其返回一个 QComboBox。然后在 setModelData() 中保存 QComboBox 的值。具体代码实现可以参考以下示例: ```cpp class ComboBoxDelegate : public QStyledItemDelegate { public: QWidget* createEditor(QWidget* parent, const QStyleOptionViewItem& option, const QModelIndex& index) const override { if (index.column() == 0) { return QStyledItemDelegate::createEditor(parent, option, index); } else { QComboBox* comboBox = new QComboBox(parent); comboBox->addItem("Item1"); comboBox->addItem("Item2"); comboBox->addItem("Item3"); return comboBox; } } void setEditorData(QWidget* editor, const QModelIndex& index) const override { if (QComboBox* comboBox = qobject_cast<QComboBox*>(editor)) { QString value = index.model()->data(index, Qt::EditRole).toString(); int index = comboBox->findText(value); comboBox->setCurrentIndex(index); } else { QStyledItemDelegate::setEditorData(editor, index); } } void setModelData(QWidget* editor, QAbstractItemModel* model, const QModelIndex& index) const override { if (QComboBox* comboBox = qobject_cast<QComboBox*>(editor)) { QString value = comboBox->currentText(); model->setData(index, value, Qt::EditRole); } else { QStyledItemDelegate::setModelData(editor, model, index); } } }; QTreeView* treeView = new QTreeView; treeView->setModel(model); treeView->setItemDelegate(new ComboBoxDelegate); ``` 以上代码将在 index.column() == 0 的情况下使用默认的编辑器,而在其他情况下使用 QComboBox 作为编辑器,并设置 QComboBox 中的选项。在 setEditorData() 中从 model 中获取值并将其设置为 QComboBox 的当前选项。在 setModelData() 中将 QComboBox 的当前值保存到 model 中。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值