c++ - Qt can't assign value to QLabel -
i start out saying i'm noob come. first c++ program built scratch. gotten of bugs ironed out, can't seem able assign value qlabel. want function 'value' called when calculate button pressed. function 'value' should math , return answer assigned qlabel 'results'. here have far.
#include <qapplication> #include <qpushbutton> #include <qlabel> #include <qslider> #include <qstring> #include <qspinbox> #include <qhboxlayout> #include <qcombobox> double x; double value(qspinbox *spinner) { int speed; speed = spinner->value(); x = speed/8; return x; } int main(int argc, char *argv[]) { qapplication prog(argc, argv); qwidget *mainwindow = new qwidget; mainwindow->setwindowtitle("plex calculator"); qpushbutton *calculate = new qpushbutton("calculate"); qcombobox *kbormb = new qcombobox; qspinbox *spinner =new qspinbox; qslider *slider = new qslider(qt::horizontal); qlabel *results = new qlabel; spinner->setrange(1,1000); slider->setrange(1,1000); spinner->setvalue(1); qobject::connect(spinner, signal(valuechanged(int)),slider, slot(setvalue(int))); qobject::connect(slider, signal(valuechanged(int)),spinner, slot(setvalue(int))); kbormb->additem("kb/s"); kbormb->additem("mb/s"); qhboxlayout *layout = new qhboxlayout; layout->addwidget(slider); layout->addwidget(spinner); layout->addwidget(kbormb); layout->addwidget(calculate); layout->addwidget(results); qobject::connect(calculate, signal(clicked()), &prog, slot(results->setnum(value(*spinner));)); mainwindow->setlayout(layout); mainwindow->show(); return prog.exec(); }
the issue following signal slot connection.
qobject::connect(calculate, signal(clicked()), &prog, slot(results->setnum(value(*spinner));));
you trying connect clicked()
signal of calculate
button results->setnum(value(*spinner));
slot of prog
. results->setnum(value(*spinner));
not slot.
a slot method in class inherits qobject
. method should added under slots:
section in class. read more signals , slots here.
to fix this, have create separate class widget , add logic their. can add slot newly created class , connect clicked()
signal that. can calculation in slot.
Comments
Post a Comment