Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [Beginner Question] Cannot get this Code to run
QtWS25 Last Chance

[Beginner Question] Cannot get this Code to run

Scheduled Pinned Locked Moved General and Desktop
7 Posts 4 Posters 2.1k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    devwatch
    wrote on last edited by
    #1

    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!

    1 Reply Last reply
    0
    • G Offline
      G Offline
      goetz
      wrote on last edited by
      #2

      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.

      http://www.catb.org/~esr/faqs/smart-questions.html

      1 Reply Last reply
      0
      • D Offline
        D Offline
        devwatch
        wrote on last edited by
        #3

        Danke Volker!

        1 Reply Last reply
        0
        • G Offline
          G Offline
          goetz
          wrote on last edited by
          #4

          No problem - you're welcome.

          http://www.catb.org/~esr/faqs/smart-questions.html

          1 Reply Last reply
          0
          • T Offline
            T Offline
            tobias.hunger
            wrote on last edited by
            #5

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

            1 Reply Last reply
            0
            • B Offline
              B Offline
              broadpeak
              wrote on last edited by
              #6

              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.

              1 Reply Last reply
              0
              • D Offline
                D Offline
                devwatch
                wrote on last edited by
                #7

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

                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved