QT和复选框问题
也许我要问的很简单,但我被困住了! :(
我有一个QWidget
,其中加载了一个QGridLayout
,其中我添加了一些
我似乎无法理解的 QCheckBox
如何捕获来自复选框的更改状态信号......
复选框被添加到 for 循环中,其中我创建了一个新的 QCheckBox 并将其作为小部件添加到 qgridlayout 中...
我应该连接什么? 每个按钮的更改状态信号发送到我的自定义插槽,还是其他什么?
我使用的代码是
QGridLayout *myLayout = new QGridLayout;
for (int i=0; i<(int)m_List.size(); i++)
{
QCheckBox *button = new QCheckBox;
button->setText(m_List[i].m_strName.c_str());
button->setIcon(QIcon(m_List[i].m_strThumbNailPath.c_str()));
button->setIconSize(QSize(50, 50));
button->setCheckable(true);
myLayout->addWidget(button);
connect(button, SIGNAL(stateChanged), this, SLOT(checkboxStateChanged));
}
myLayout->setRowStretch(2, 10);
myLayout->setColumnStretch(2, 10);
QWidget *myWidget = new QWidget;
myWidget->setLayout(myLayout);
myToolbox = new QToolBox;
myToolbox->addItem(myWidget, "Options");
它正确显示复选框,我可以选中和取消选中它们,但我无法捕获信号...... 我缺少什么? 谢谢!
probably what i am asking is very easy, but i'm stuck! :(
i have a QWidget
in which i load a QGridLayout
in which i add a number of QCheckBox
'es
i can't seem to figure out how to catch the changestate signal from the checkboxes....
the check boxes are added in a for loop, in which i create a new QCheckBox
which and adding it as a widget to the qgridlayout...
what am i supposed to connect?
each button's changestate signal to my custom slot, or something else?
the code i use is
QGridLayout *myLayout = new QGridLayout;
for (int i=0; i<(int)m_List.size(); i++)
{
QCheckBox *button = new QCheckBox;
button->setText(m_List[i].m_strName.c_str());
button->setIcon(QIcon(m_List[i].m_strThumbNailPath.c_str()));
button->setIconSize(QSize(50, 50));
button->setCheckable(true);
myLayout->addWidget(button);
connect(button, SIGNAL(stateChanged), this, SLOT(checkboxStateChanged));
}
myLayout->setRowStretch(2, 10);
myLayout->setColumnStretch(2, 10);
QWidget *myWidget = new QWidget;
myWidget->setLayout(myLayout);
myToolbox = new QToolBox;
myToolbox->addItem(myWidget, "Options");
it displays the checkboxes correctly, i can check and un-check them, but i can't catch the signal...
what am i missing?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的连接有问题(无签名):
将参数添加到:
stateChanged
和checkboxStateChanged
应该类似于:
您将在
checkboxStateChanged
中获得复选框状态code> int 参数作为枚举值之一,请参阅此处
Your connection is problematic (no signature):
add the parementers to:
stateChanged
andcheckboxStateChanged
should be something like:
You will get the checkbox state in the
checkboxStateChanged
int parameter as one ofenum values, see here
是的,soulSurfer 给出的答案可能会修复你的连接,但不是你的问题,还有更大的鱼要煎。您如何了解单击了哪个按钮(我认为这很重要)?
要了解单击了哪个按钮,您必须使用 QSignalMapper 或 QButtonGroup。
希望这会有所帮助。
Yes, the answer soulSurfer gave probably will fix your connection, but not your problem, there is a bigger fish to fry. How are you going to understand which button was clicked (I assume it matters)?
To understand which button was clicked you will have to use QSignalMapper or QButtonGroup.
Hope this helps.