[Beginner Question] Cannot get this Code to run
-
wrote on 7 Jan 2012, 22:30 last edited by
Hello Community, i play a little bit with signals/slots (hte manual way) but my code does not work.
@
#include <QtGui>
#include <QMessageBox>class Demo : public QObject
{
Q_OBJECTpublic slots:
void showMessage();public:
Demo(){}
};
void Demo::showMessage()
{
QMessageBox::information(0,"Message","Hello World",QMessageBox::Ok);}
int main(int argc, char* argv[])
{
QApplication app(argc,argv);//Create a new QPushButton QPushButton * button = new QPushButton("Hello Wolrd!"); button->show();
//Construct my Class
Demo *dapp = new Demo();//Here i try to connect that on pressing the Button the Demo->showMessage Member will be displayed
QObject::connect(&button,SIGNAL(clicked()),&app,SLOT(showMessage()));return app.exec();
}
@What did i wrong in the connect Member?
Thanks for Help!
-
wrote on 7 Jan 2012, 23:04 last edited by
You must connect to the Demo object, not the QApplication object:
@
QObject::connect(&button,SIGNAL(clicked()),dapp,SLOT(showMessage()));
@You should have got a warning message that the connect failed on application startup.
-
wrote on 8 Jan 2012, 00:00 last edited by
Danke Volker!
-
wrote on 8 Jan 2012, 00:27 last edited by
No problem - you're welcome.
-
wrote on 10 Jan 2012, 14:33 last edited by
The "&button" should really just be "button". button already is a pointer, no need to turn it into a pointer to a pointer...
-
wrote on 10 Jan 2012, 16:15 last edited by
Sorry, but you are a little bit inconsecutive (see the "star" place):
char* argv[]
Demo *dapp
QPushButton * buttonThis can confuse other developers.
Be consecutive!When you declare (or define) a pointer, bind the "star" to the type, and when you dereference a pointer, bind the "star" to the variable name.
-
wrote on 10 Jan 2012, 19:17 last edited by
Yea, thank you i will declare it better in my next little failure programms :)
6/7