如何创建 Qt 组合框

发布于 2025-01-01 04:34:44 字数 182 浏览 5 评论 0原文

您好,想用许多项目填充 QCombobox ,这些项目等于 QSpinBox 中设置的变量

当将 QSpinbox 值更改为 N 时,则组合框应显示相同数量的选项,编号从 0 到 N。

是否可以使用 QObject::connect 来实现

Hi would like to populate a QCombobox with a number of items which equals a variable set in a QSpinBox

When varying the QSpinbox value to N , then the combobox should show the same number of options numbered from 0 to N.

Is it possible to do it with a QObject::connect

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

半窗疏影 2025-01-08 04:34:44

您可以使用信号QSpinBox::valueChanged (int i)

实现您自己的从 QComboBox 派生的类,并创建一个与信号具有相同签名的槽。

类似于:

public Q_SLOTS:
on_setItemsFromSpinBox( int i );

然后您可以将信号连接到插槽。

connect( m_spinbox,
         SIGNAL(valueChanged(int)),
         m_my_combobox,
         SLOT(on_setItemsFromSpinBox(int)) );

在槽中,您清除组合框 (QComboBox::clear ()),然后使用循环,在其中创建与槽中从信号接收的整数指定的项目数相同的项目。

You can use the signal QSpinBox::valueChanged ( int i ).

Implement your own class which derives from QComboBox and create a slot which has the same signature as the signal.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文