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. Signals and Slots and vtable, oh my!!
Forum Updated to NodeBB v4.3 + New Features

Signals and Slots and vtable, oh my!!

Scheduled Pinned Locked Moved Unsolved General and Desktop
9 Posts 3 Posters 614 Views 3 Watching
  • 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.
  • R Offline
    R Offline
    radar
    wrote on last edited by
    #1

    Using Qt5 with my C++ (open source) project, under both VS Code/makefile and Qt Creator dev environments, execution of the connect() method yields:

    QObject::connect: No such slot QComboBox::MyIndexChanged() in BashQtHelper.cpp:118
    

    Or, when I include Q_OBJECT, the linker error:

    undefined reference to `vtable for MyComboBox'
    

    The class is:

    class MyComboBox: public QComboBox
    {
    public:
        MyComboBox(QWidget *parent = nullptr): QComboBox(parent) {
            connect(this, SIGNAL(activated(int)), this, SLOT(MyIndexChanged()));
            }
        ~MyComboBox(){}
    
    public slots:
        void MyIndexChanged() {
            if (currentIndex()) {std::cout << currentText().toStdString() << "\n"; exit(0);}}
    };
    

    When I try the alternative call, namely with pointers to functions:

    connect(this, &activated, this, &MyComboBox::MyIndexChanged);
    

    The compiler complains:

    error: no matching function for call
    

    The minimalist program by ChatGPT is:

    #include <QtWidgets/QApplication>
    #include <QtWidgets/QComboBox>
    #include <QtWidgets/QVBoxLayout>
    #include <QtWidgets/QMessageBox>
    
    class ComboBoxApp : public QWidget {
    public:
        ComboBoxApp(QWidget *parent = nullptr) : QWidget(parent) {
            // Create a combo box
            comboBox = new QComboBox(this);
    
            // Add items to the combo box
            comboBox->addItem("Option 1");
            comboBox->addItem("Option 2");
            comboBox->addItem("Option 3");
    
            // Connect the signal for item selection to the custom slot
            connect(comboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(onComboBoxSelectionChanged(int)));
    
            // Create a layout and set it for the main window
            QVBoxLayout *layout = new QVBoxLayout(this);
            layout->addWidget(comboBox);
    
            setLayout(layout);
        }
    
    public slots:
        // Custom slot to handle combo box selection changes
        void onComboBoxSelectionChanged(int index) {
            QString selectedOption = comboBox->itemText(index);
            QMessageBox::information(this, "Selection Changed", "Selected: " + selectedOption);
        }
    
    private:
        QComboBox *comboBox;
    };
    
    int main(int argc, char *argv[]) {
        QApplication app(argc, argv);
    
        ComboBoxApp window;
        window.setWindowTitle("ComboBox Application");
        window.resize(300, 150);
        window.show();
    
        return app.exec();
    }
    

    How do I make this code work, or do I need to scrap the whole thing and start with a better SIGNAL/SLOT template?

    Pl45m4P 2 Replies Last reply
    0
    • R radar

      Using Qt5 with my C++ (open source) project, under both VS Code/makefile and Qt Creator dev environments, execution of the connect() method yields:

      QObject::connect: No such slot QComboBox::MyIndexChanged() in BashQtHelper.cpp:118
      

      Or, when I include Q_OBJECT, the linker error:

      undefined reference to `vtable for MyComboBox'
      

      The class is:

      class MyComboBox: public QComboBox
      {
      public:
          MyComboBox(QWidget *parent = nullptr): QComboBox(parent) {
              connect(this, SIGNAL(activated(int)), this, SLOT(MyIndexChanged()));
              }
          ~MyComboBox(){}
      
      public slots:
          void MyIndexChanged() {
              if (currentIndex()) {std::cout << currentText().toStdString() << "\n"; exit(0);}}
      };
      

      When I try the alternative call, namely with pointers to functions:

      connect(this, &activated, this, &MyComboBox::MyIndexChanged);
      

      The compiler complains:

      error: no matching function for call
      

      The minimalist program by ChatGPT is:

      #include <QtWidgets/QApplication>
      #include <QtWidgets/QComboBox>
      #include <QtWidgets/QVBoxLayout>
      #include <QtWidgets/QMessageBox>
      
      class ComboBoxApp : public QWidget {
      public:
          ComboBoxApp(QWidget *parent = nullptr) : QWidget(parent) {
              // Create a combo box
              comboBox = new QComboBox(this);
      
              // Add items to the combo box
              comboBox->addItem("Option 1");
              comboBox->addItem("Option 2");
              comboBox->addItem("Option 3");
      
              // Connect the signal for item selection to the custom slot
              connect(comboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(onComboBoxSelectionChanged(int)));
      
              // Create a layout and set it for the main window
              QVBoxLayout *layout = new QVBoxLayout(this);
              layout->addWidget(comboBox);
      
              setLayout(layout);
          }
      
      public slots:
          // Custom slot to handle combo box selection changes
          void onComboBoxSelectionChanged(int index) {
              QString selectedOption = comboBox->itemText(index);
              QMessageBox::information(this, "Selection Changed", "Selected: " + selectedOption);
          }
      
      private:
          QComboBox *comboBox;
      };
      
      int main(int argc, char *argv[]) {
          QApplication app(argc, argv);
      
          ComboBoxApp window;
          window.setWindowTitle("ComboBox Application");
          window.resize(300, 150);
          window.show();
      
          return app.exec();
      }
      

      How do I make this code work, or do I need to scrap the whole thing and start with a better SIGNAL/SLOT template?

      Pl45m4P Offline
      Pl45m4P Offline
      Pl45m4
      wrote on last edited by Pl45m4
      #2

      @radar said in Signals and Slots and vtable, oh my!!:

      The minimalist program by ChatGPT is:

      Rule No. 1: Dont trust any AI/Bot, when you are not able to confirm the result's correctness ;-)

      class MyComboBox: public QComboBox
      {
      public:

      You are missing the Q_OBJECT macro. Without this, no signals ands slots ;-)
      Add the macro, include QComboBox and then clean your build, and build again.

      connect(this, &activated, this, &MyComboBox::MyIndexChanged);

      You need the complete, fully qualified function pointer there &activated is not enough, unless it's a global function which doesn't belong to any class.

      Apart from all this, connecting this to this doesn't make too much sense. Then you just can call the function when you need it


      If debugging is the process of removing software bugs, then programming must be the process of putting them in.

      ~E. W. Dijkstra

      R 2 Replies Last reply
      1
      • Pl45m4P Pl45m4

        @radar said in Signals and Slots and vtable, oh my!!:

        The minimalist program by ChatGPT is:

        Rule No. 1: Dont trust any AI/Bot, when you are not able to confirm the result's correctness ;-)

        class MyComboBox: public QComboBox
        {
        public:

        You are missing the Q_OBJECT macro. Without this, no signals ands slots ;-)
        Add the macro, include QComboBox and then clean your build, and build again.

        connect(this, &activated, this, &MyComboBox::MyIndexChanged);

        You need the complete, fully qualified function pointer there &activated is not enough, unless it's a global function which doesn't belong to any class.

        Apart from all this, connecting this to this doesn't make too much sense. Then you just can call the function when you need it

        R Offline
        R Offline
        radar
        wrote on last edited by
        #3

        @Pl45m4 said in Signals and Slots and vtable, oh my!!:

        Q_OBJECT

        I tried that, that's when I got the vtable not found on linking.

        1 Reply Last reply
        0
        • Pl45m4P Pl45m4

          @radar said in Signals and Slots and vtable, oh my!!:

          The minimalist program by ChatGPT is:

          Rule No. 1: Dont trust any AI/Bot, when you are not able to confirm the result's correctness ;-)

          class MyComboBox: public QComboBox
          {
          public:

          You are missing the Q_OBJECT macro. Without this, no signals ands slots ;-)
          Add the macro, include QComboBox and then clean your build, and build again.

          connect(this, &activated, this, &MyComboBox::MyIndexChanged);

          You need the complete, fully qualified function pointer there &activated is not enough, unless it's a global function which doesn't belong to any class.

          Apart from all this, connecting this to this doesn't make too much sense. Then you just can call the function when you need it

          R Offline
          R Offline
          radar
          wrote on last edited by
          #4

          @Pl45m4 said in Signals and Slots and vtable, oh my!!:

          Rule No. 1: Dont trust any AI/Bot, when you are not able to confirm the result's correctness ;-)

          Okay then, where's the official template/example provided by Qt?

          It's easy to say "don't do this, don't use that", the useful answers are, "here's an example of how to do it right".

          Pl45m4P 1 Reply Last reply
          0
          • R radar

            @Pl45m4 said in Signals and Slots and vtable, oh my!!:

            Rule No. 1: Dont trust any AI/Bot, when you are not able to confirm the result's correctness ;-)

            Okay then, where's the official template/example provided by Qt?

            It's easy to say "don't do this, don't use that", the useful answers are, "here's an example of how to do it right".

            Pl45m4P Offline
            Pl45m4P Offline
            Pl45m4
            wrote on last edited by
            #5

            @radar said in Signals and Slots and vtable, oh my!!:

            Okay then, where's the official template/example provided by Qt?

            Have you tried the official documentation instead of ChatGPT? :)

            • https://doc.qt.io/qt-6/signalsandslots.html
              • https://doc.qt.io/qt-6/signalsandslots.html#a-small-example

            If debugging is the process of removing software bugs, then programming must be the process of putting them in.

            ~E. W. Dijkstra

            1 Reply Last reply
            1
            • R radar

              Using Qt5 with my C++ (open source) project, under both VS Code/makefile and Qt Creator dev environments, execution of the connect() method yields:

              QObject::connect: No such slot QComboBox::MyIndexChanged() in BashQtHelper.cpp:118
              

              Or, when I include Q_OBJECT, the linker error:

              undefined reference to `vtable for MyComboBox'
              

              The class is:

              class MyComboBox: public QComboBox
              {
              public:
                  MyComboBox(QWidget *parent = nullptr): QComboBox(parent) {
                      connect(this, SIGNAL(activated(int)), this, SLOT(MyIndexChanged()));
                      }
                  ~MyComboBox(){}
              
              public slots:
                  void MyIndexChanged() {
                      if (currentIndex()) {std::cout << currentText().toStdString() << "\n"; exit(0);}}
              };
              

              When I try the alternative call, namely with pointers to functions:

              connect(this, &activated, this, &MyComboBox::MyIndexChanged);
              

              The compiler complains:

              error: no matching function for call
              

              The minimalist program by ChatGPT is:

              #include <QtWidgets/QApplication>
              #include <QtWidgets/QComboBox>
              #include <QtWidgets/QVBoxLayout>
              #include <QtWidgets/QMessageBox>
              
              class ComboBoxApp : public QWidget {
              public:
                  ComboBoxApp(QWidget *parent = nullptr) : QWidget(parent) {
                      // Create a combo box
                      comboBox = new QComboBox(this);
              
                      // Add items to the combo box
                      comboBox->addItem("Option 1");
                      comboBox->addItem("Option 2");
                      comboBox->addItem("Option 3");
              
                      // Connect the signal for item selection to the custom slot
                      connect(comboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(onComboBoxSelectionChanged(int)));
              
                      // Create a layout and set it for the main window
                      QVBoxLayout *layout = new QVBoxLayout(this);
                      layout->addWidget(comboBox);
              
                      setLayout(layout);
                  }
              
              public slots:
                  // Custom slot to handle combo box selection changes
                  void onComboBoxSelectionChanged(int index) {
                      QString selectedOption = comboBox->itemText(index);
                      QMessageBox::information(this, "Selection Changed", "Selected: " + selectedOption);
                  }
              
              private:
                  QComboBox *comboBox;
              };
              
              int main(int argc, char *argv[]) {
                  QApplication app(argc, argv);
              
                  ComboBoxApp window;
                  window.setWindowTitle("ComboBox Application");
                  window.resize(300, 150);
                  window.show();
              
                  return app.exec();
              }
              

              How do I make this code work, or do I need to scrap the whole thing and start with a better SIGNAL/SLOT template?

              Pl45m4P Offline
              Pl45m4P Offline
              Pl45m4
              wrote on last edited by Pl45m4
              #6

              @radar said in Signals and Slots and vtable, oh my!!:

              VS Code/makefile

              I checked your GitHub... and there is no moc in your makefile... Maybe I've missed something but AFAICS you are not running the meta-object-compiler, which would explain why none of your QObject / metaobject stuff, including signals, is working.

              • https://doc.qt.io/qt-6/moc.html#writing-make-rules-for-invoking-moc

              Edit:
              Also, why do you have everything (two QObject classes and even the main) in one cpp file?!
              MOC can only run *.h files and you need to include your moc'ed output in your *.cppfile.
              [ Edit: can also run cpp files, but it's unusual as stated by @Christian-Ehrlicher ]

              Start to learn how Qt and everything that comes with it works, otherwise you will run into more trouble sooner or later


              If debugging is the process of removing software bugs, then programming must be the process of putting them in.

              ~E. W. Dijkstra

              Christian EhrlicherC 1 Reply Last reply
              1
              • Pl45m4P Pl45m4

                @radar said in Signals and Slots and vtable, oh my!!:

                VS Code/makefile

                I checked your GitHub... and there is no moc in your makefile... Maybe I've missed something but AFAICS you are not running the meta-object-compiler, which would explain why none of your QObject / metaobject stuff, including signals, is working.

                • https://doc.qt.io/qt-6/moc.html#writing-make-rules-for-invoking-moc

                Edit:
                Also, why do you have everything (two QObject classes and even the main) in one cpp file?!
                MOC can only run *.h files and you need to include your moc'ed output in your *.cppfile.
                [ Edit: can also run cpp files, but it's unusual as stated by @Christian-Ehrlicher ]

                Start to learn how Qt and everything that comes with it works, otherwise you will run into more trouble sooner or later

                Christian EhrlicherC Offline
                Christian EhrlicherC Offline
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on last edited by
                #7

                @Pl45m4 said in Signals and Slots and vtable, oh my!!:

                MOC can only run *.h files and you need to include your moc'ed output in your *.cppfile.

                Thats not true

                But why create a custom Makefile instead using CMake or qmake to let do all this work?

                Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                Visit the Qt Academy at https://academy.qt.io/catalog

                Pl45m4P 1 Reply Last reply
                3
                • Christian EhrlicherC Christian Ehrlicher

                  @Pl45m4 said in Signals and Slots and vtable, oh my!!:

                  MOC can only run *.h files and you need to include your moc'ed output in your *.cppfile.

                  Thats not true

                  But why create a custom Makefile instead using CMake or qmake to let do all this work?

                  Pl45m4P Offline
                  Pl45m4P Offline
                  Pl45m4
                  wrote on last edited by
                  #8

                  @Christian-Ehrlicher said in Signals and Slots and vtable, oh my!!:

                  Thats not true

                  What part? The h-only or that you have to include your moc in your cpp?
                  I know that you dont have to, but you have to run moc on the files where you declare your Q_OBJECT, right?! So usually you would declare your class in some header file and moc outputs something like moc_myclass.cpp

                  Found this, because I wasnt 100% sure:

                  • https://forum.qt.io/topic/83854/class-with-q_object-macro-declared-and-defined-in-cpp

                  Now I'm also confused :D


                  If debugging is the process of removing software bugs, then programming must be the process of putting them in.

                  ~E. W. Dijkstra

                  Christian EhrlicherC 1 Reply Last reply
                  0
                  • Pl45m4P Pl45m4

                    @Christian-Ehrlicher said in Signals and Slots and vtable, oh my!!:

                    Thats not true

                    What part? The h-only or that you have to include your moc in your cpp?
                    I know that you dont have to, but you have to run moc on the files where you declare your Q_OBJECT, right?! So usually you would declare your class in some header file and moc outputs something like moc_myclass.cpp

                    Found this, because I wasnt 100% sure:

                    • https://forum.qt.io/topic/83854/class-with-q_object-macro-declared-and-defined-in-cpp

                    Now I'm also confused :D

                    Christian EhrlicherC Offline
                    Christian EhrlicherC Offline
                    Christian Ehrlicher
                    Lifetime Qt Champion
                    wrote on last edited by Christian Ehrlicher
                    #9

                    @Pl45m4 You can also moc a .cpp file even though it's unusual.
                    e.g. when you've a small private, object-derived class in your cpp file (as some Qt source code is doing it) instead moving the class definition into an own foo_p.h.
                    I mostly use it for small testcases only containing a main.cpp - then you need to include <main.moc> and cmake is doing the rest (instead fiddling around with a Makefile).

                    Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                    Visit the Qt Academy at https://academy.qt.io/catalog

                    1 Reply Last reply
                    1

                    • Login

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