QT and check boxes problem
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);
开发者_StackOverflowQWidget *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!
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
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.
精彩评论