c++ - Emit Signal Only When QCheckBox is Checked -
i creating set of qcheckbox dynamically based on user input so:
qwidget *wid = new qwidget(); qvboxlayout *layout = new qvboxlayout(); for(int i=0; i<numbermodes; i++) { int k = amplitudes(i,0); int m = amplitudes(i,1); qstring ks = qstring::number(k); qstring ms = qstring::number(m); qstring position = qstring::number(i); qstring mode = "a"+ks+ms; qcheckbox *check = new qcheckbox(mode); connect(check, signal(toggled(bool)), &mapper, slot(map())); connect(check, signal(toggled(bool)), &selectmodes, slot(map())); mapper.setmapping(check,position); selectmodes.setmapping(check,mode); layout->addwidget(check); updategeometry(); } wid->setlayout(layout); ui->scrollarea->setwidget(wid);
the qsignalmapper connected class performs calculations:
connect(&selectmodes, signal(mapped(qstring)), this, signal(checkboxclicked2(qstring))); connect(this, signal(checkboxclicked2(qstring)), &supress2, slot(listenselectedmodes(qstring))); connect(&mapper, signal(mapped(qstring)), this, signal(checkboxclicked(qstring))); connect(this, signal(checkboxclicked(qstring)), &suppress, slot(listenselectedmodes(qstring)));
what need classes receive signals when qcheckbox checked; meaning if check once, , un-check no signal should emitted, or received. not sure best approach is. ideas?
the suggestions given user2672165 excellent!
if want monitor check event not uncheck event, 1 way subclass qcheckbox widget emits particular signal when checkbox checked (e.g. checkboxchecked
)
then connect signal mapper custom signal checkboxchecked
, instead of standard toggle(bool)
signal.
in way slot associated signal mapper invoked when checkbox checked , not when unchecked.
here simple example
#include <qapplication> #include <qtgui> #include <qvboxlayout> #include <qsignalmapper> #include <qcheckbox> #include <qdebug> class checkablecheckbox : public qcheckbox { q_object public: checkablecheckbox(const qstring &text, qwidget *parent = 0) : qcheckbox(text, parent) { connect(this, signal(toggled(bool)), this, slot(verifycheck(bool))); } signals: void checkboxchecked(); public slots: void verifycheck(bool checked) { if (checked) emit checkboxchecked(); } }; class test : public qwidget { q_object public: test(qwidget *parent = 0) : qwidget(parent) { qsignalmapper *mapper = new qsignalmapper(); qvboxlayout *layout = new qvboxlayout(); (int = 0; < 10; i++) { qstring mode = "a" + qstring::number(i); checkablecheckbox *check = new checkablecheckbox(mode); connect(check, signal(checkboxchecked()), mapper, slot(map())); mapper->setmapping(check, qstring::number(i)); layout->addwidget(check); setlayout(layout); } connect(mapper, signal(mapped(qstring)), this, slot(checkboxclicked(qstring))); } public slots: void checkboxclicked(const qstring &mapping) { qwarning() << "checkbox:" << mapping << " checked"; } }; int main(int argc, char *argv[]) { qapplication a(argc, argv); test *wid = new test(); wid->show(); return a.exec(); } #include "main.moc"
edit:
if want monitor change in check status , notify other portions of code status of checkbox (which want) can this... don't need qsignalmapper...
i have implemented test method testmonitorcheckstatus
show mean. note need typedef qlist<bool> checkboxstatuslist;
(at least far know) use qlist argument slots , signals.
edit #2: number of checkboxes set @ object creation
hope helps
#include <qapplication> #include <qtgui> #include <qvboxlayout> #include <qsignalmapper> #include <qcheckbox> #include <qlist> #include <qdebug> typedef qlist<bool> checkboxstatuslist; class test : public qwidget { q_object public: test(int totalcheckboxes, qwidget *parent = 0) : qwidget(parent) { qvboxlayout *layout = new qvboxlayout(); (int = 0; < totalcheckboxes; i++) { qstring mode = "a" + qstring::number(i); qcheckbox *checkbox = new qcheckbox(mode); connect(checkbox, signal(toggled(bool)), this, slot(monitorcheckstatus())); m_checkboxlist.append(checkbox); layout->addwidget(checkbox); } setlayout(layout); connect(this, signal(checkboxstatuschanged(checkboxstatuslist)), this, slot(testmonitorcheckstatus(checkboxstatuslist))); } public slots: void monitorcheckstatus() { checkboxstatuslist checkboxstatus; (int = 0; < m_checkboxlist.count(); ++i) checkboxstatus.append(m_checkboxlist.at(i)->ischecked()); emit checkboxstatuschanged(checkboxstatus); } void testmonitorcheckstatus(const checkboxstatuslist &checkboxstatus) { (int = 0; < checkboxstatus.count(); ++i) qwarning() << "checkbox:" << << " is" << (checkboxstatus.at(i) ? "checked" : "unchecked"); qwarning(" "); } signals: void checkboxstatuschanged(const checkboxstatuslist &checkboxstatus); private: qlist<qcheckbox *> m_checkboxlist; }; int main(int argc, char *argv[]) { qapplication a(argc, argv); test *wid = new test(10); wid->show(); return a.exec(); } #include "main.moc"
Comments
Post a Comment