QComboBox的使用

fromComboBox = QComboBox() 添加一个 combobox

fromComboBox.addItem(rates) 添加一个下拉选项

fromComboBox.addItems(["%d years" % x for x in range(2, 26)]) 从序列中添加

fromComboBox.setMaxVisibleItems(10) #设置最大显示下列项 超过要使用滚动条拖拉

fromComboBox.setMaxCount(5) #设置最大下拉项 超过将不显示

fromComboBox.setInsertPolicy(QComboBox.InsertAfterCurrent) #设置插入方式

插入方式有:NoInsert,InsertAtTop,InsertAtCurrent,InsertAtBottom,InsertAfterCurrent

InsertBeforeCurrent,InsertAlphabetically

字面意思都好理解 最后一个是按字母表顺序插入

QComboBox 发出一个currentIndexChanged(int) 的信号.

QComboBox 得到当前项 currentIndex() + 1 #QComboBox 默认的currentIndex为 -1

QComboBox.findText('dsfds') #返回 内容为dsfds的索引

QComboBox 得到当前项文本内容     currentText()

fromSpinBox = QDoubleSpinBox()
fromSpinBox.setRange(0.01, 10000000.00)

fromSpinBox.setSuffix(" %d") #设置后缀 如显示 10.0%d

fromSpinBox.setPrefix('#d') #设置前缀
fromSpinBox.setValue(1.00) 设置值

QDoubleSpinBox 发出 valueChanged(double) 信号 有setValue(double)插槽


QComboxBox可以建立下拉菜单,以供使用者选择项目,以下直接例子,程序中包括下拉菜单,选项后会更新QLabel的文字內容:

#include <QApplication>
#include <QWidget>
#include <QLabel>
#include <QComboBox>
#include <QVBoxLayout>
#include <QIcon>
 
int main(int argc, char *argv[]) {
QApplication app(argc, argv);
 
QWidget *window = new QWidget;
window->setWindowTitle("QComboBox");
window->resize(300, 200);
 
QComboBox *combo = new QComboBox;
combo->setEditable(true);
combo->insertItem(0, QIcon( "caterpillar_head.jpg" ), "caterpillar");
combo->insertItem(1, QIcon( "momor_head.jpg" ), "momor");
combo->insertItem(2, QIcon( "bush_head.jpg" ), "bush");
combo->insertItem(3, QIcon( "bee_head.jpg" ), "bee"); 
 
QLabel *label = new QLabel("QComboBox");
 
QVBoxLayout *layout = new QVBoxLayout;
layout->addWidget(combo);
layout->addWidget(label);
 
QObject::connect(combo, SIGNAL(activated(const QString &)),
label, SLOT(setText(const QString &)));
 
window->setLayout(layout);
window->show();
 
return app.exec();
}

说明:QComboBox的setEditable()方法设置combo可编辑,使用insertItem()插入项时,可以使用QIcon设置图标,当选择某个选项时,会发出activated()的Signal,QString的部分即为选项文字,这里将之连接至QLabel的setText(),以改变QLabel的文字。

posted @ 2022-12-19 11:13  阳光下的小土豆  阅读(382)  评论(0编辑  收藏  举报