QComboBox使用方法,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()方法可設定下拉選單的選項是否可編輯,使用insertItem()插入選項 時,可以使用QIcon設定圖示,當您選擇下拉選單的某個項目時,會發出activated()的Signal,QString的部份即為選項文字,這邊 將之連接至QLabel的setText(),以改變QLabel的文字,一個程式執行的畫面如下


参考:http://hi.baidu.com/lujizhen/blog/item/c061885cad0a385ffbf2c0ff.html


  • 2
    点赞
  • 35
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值