Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    [SOLVED] Linking errors

    General and Desktop
    2
    5
    1539
    Loading More Posts
    • 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.
    • C
      chax0 last edited by

      The following code causes linking errors when the commented out line is uncommented, and I would like to understand why. Can someone please help?

      @
      #include <QObject>
      #include <QApplication>
      #include <QMainWindow>
      #include <QComboBox>

      class SlotMachine : public QObject {
      Q_OBJECT
      public:
      explicit SlotMachine(QObject *parent = 0) : QObject(parent) {
      }

      ~SlotMachine() {
      }
      

      public slots:
      void SelectedItem(QString item) {
      qDebug(QString("Item selected was: %1").arg(item).toLocal8Bit().data());
      }
      };

      int main(int argc, char *argv[]) {
      QApplication app(argc, argv);

      QMainWindow *window = new QMainWindow();
      window->setGeometry(QRect(QPoint(100, 100), QSize(600, 400)));
      
      QComboBox *comboBox = new QComboBox(window);
      comboBox->setGeometry(QRect(QPoint(50, 50), QSize(100, 20)));
      
      QStringList comboItems;
      comboItems << "Alpha" << "Bravo" << "Charlie" << "Delta" << "Echo";
      comboBox->addItems(comboItems);
      
      SlotMachine *slotMachine;
      //slotMachine = new SlotMachine();
      QObject::connect(comboBox, SIGNAL(activated(QString)), slotMachine, SLOT(SelectedItem(QString)));
      
      window->show();
      
      return app.exec&#40;&#41;;
      

      }
      @

      1 Reply Last reply Reply Quote 0
      • T
        thisisbhaskar last edited by

        Separate your SlotMachine class to a different .h file, then it should work

        or you need to include moc_*.cpp file in this main file.

        1 Reply Last reply Reply Quote 0
        • T
          thisisbhaskar last edited by

          http://doc.qt.nokia.com/latest/moc.html for more details.

          @For Q_OBJECT class declarations in implementation (.cpp) files, we suggest a makefile rule like this:

          foo.o: foo.moc

          foo.moc: foo.cpp
          moc $(DEFINES) $(INCPATH) -i $< -o $

          This guarantees that make will run the moc before it compiles foo.cpp. You can then put

          #include "foo.moc"

          at the end of foo.cpp, where all the classes declared in that file are fully known.@

          1 Reply Last reply Reply Quote 0
          • C
            chax0 last edited by

            Separating the SlotMachine class declaration into a separate .h file and #including the .h file in main.cpp took care of the linking errors. Thank you!

            Can you also please show an example of how to include the moc_*.cpp file in the main.cpp?

            With respect to the makefile rule, is it possible to specify "something" in the .pro file that generates the appropriate makefile rule? Unlikely I think, but then they say the only stupid question is the one you do not ask ;-)!

            1 Reply Last reply Reply Quote 0
            • T
              thisisbhaskar last edited by

              I am not sure if you can put moc_*.cpp related makefile rules in .pro file. My understanding was is that it should be kept it in the platform specific make file that gets generated from .pro file.

              1 Reply Last reply Reply Quote 0
              • First post
                Last post