[Beginner Question] Cannot get this Code to run



  • 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_OBJECT

    public 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!



  • 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.



  • Danke Volker!



  • No problem - you're welcome.


  • Moderators

    The "&button" should really just be "button". button already is a pointer, no need to turn it into a pointer to a pointer...



  • Sorry, but you are a little bit inconsecutive (see the "star" place):

    char* argv[]
    Demo *dapp
    QPushButton * button

    This 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.



  • Yea, thank you i will declare it better in my next little failure programms :)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.