[英]How to create a Qt Combobox
Hi would like to populate a QCombobox
with a number of items which equals a variable set in a QSpinBox
您想在QCombobox
填充一些项目,这些项目等于QSpinBox
设置的变量
When varying the QSpinbox
value to N , then the combobox should show the same number of options numbered from 0 to N. 将QSpinbox
值更改为N时,组合框应显示从0到N编号的相同数量的选项。
Is it possible to do it with a QObject::connect
是否可以使用QObject::connect
来完成它
You can use the signal QSpinBox::valueChanged ( int i )
. 您可以使用信号QSpinBox::valueChanged ( int i )
。
Implement your own class which derives from QComboBox
and create a slot which has the same signature as the signal. 实现自己的类,该类派生自QComboBox
并创建一个与信号具有相同签名的插槽。
Something like: 就像是:
public Q_SLOTS:
on_setItemsFromSpinBox( int i );
Then you can connect the signal to the slot. 然后,您可以将信号连接到插槽。
connect( m_spinbox,
SIGNAL(valueChanged(int)),
m_my_combobox,
SLOT(on_setItemsFromSpinBox(int)) );
In the slot you clear the combobox ( QComboBox::clear ()
) and then use a loop in which you create as many items as specified by the integer you received in your slot from your signal. 在插槽中清除组合框( QComboBox::clear ()
),然后使用一个循环,在该循环中,您可以根据信号从插槽中收到的整数创建任意数量的项目。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.