c++ - Change label text from another class using Qt signals and slots -
i'm trying change text of class label class. have class mainwindow, contains label.
i have bot class wanna change value of label.
i'm trying create signal , slots have no idea start.
i created signal , slots so:
//in mainwindow.h signals: void changetextsignal(); private slots: void changetext(); //in mainwindow.cpp void mainwindow::changetext(){ this->label->settext("foobar"); }
but have no idea how connect signal able change label's text class.
read on qt signal-slot mechanism. if understand correctly, trying signal bot mainwindow label text needs change. here's how it...
//bot.h class bot { q_object; //other stuff here signals: void textchanged(qstring); public: void somefunctionthatchangestext(const qstring& newtext) { emit textchanged(newtext); } } //mainwindow.cpp mainwindow::mainwindow { //do other stuff this->label = new qlabel("original text"); mybot = new bot; //mybot bot* member of mainwindow in example connect(mybot, signal(textchanged(qstring)), this->label, slot(settext(qstring))); } void mainwindow::hello() { mybot->somefunctionthatchangestext("hello world!"); }
Comments
Post a Comment