QT和复选框问题

发布于 2024-11-03 18:27:57 字数 1037 浏览 2 评论 0原文

也许我要问的很简单,但我被困住了! :(

我有一个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 技术交流群。

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

发布评论

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

评论(2

抠脚大汉 2024-11-10 18:27:58

您的连接有问题(无签名):

connect(button, SIGNAL(stateChanged), this, SLOT(checkboxStateChanged)); 

将参数添加到:stateChangedcheckboxStateChanged

应该类似于:

connect(button, SIGNAL(stateChanged(int)), this, SLOT(checkboxStateChanged(int))); 

您将在 checkboxStateChanged 中获得复选框状态code> int 参数作为枚举值之一

Qt::CheckState

,请参阅此处

Your connection is problematic (no signature):

connect(button, SIGNAL(stateChanged), this, SLOT(checkboxStateChanged)); 

add the parementers to: stateChanged and checkboxStateChanged

should be something like:

connect(button, SIGNAL(stateChanged(int)), this, SLOT(checkboxStateChanged(int))); 

You will get the checkbox state in the checkboxStateChanged int parameter as one of

Qt::CheckState

enum values, see here

超可爱的懒熊 2024-11-10 18:27:58

是的,soulSurfer 给出的答案可能会修复你的连接,但不是你的问题,还有更大的鱼要煎。您如何了解单击了哪个按钮(我认为这很重要)?

要了解单击了哪个按钮,您必须使用 QSignalMapperQButtonGroup。

希望这会有所帮助。

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.

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