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. [SOLVED] Linking errors
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] Linking errors

Scheduled Pinned Locked Moved General and Desktop
5 Posts 2 Posters 1.9k Views 1 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.
  • C Offline
    C Offline
    chax0
    wrote on last edited by
    #1

    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
    0
    • T Offline
      T Offline
      thisisbhaskar
      wrote on last edited by
      #2

      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
      0
      • T Offline
        T Offline
        thisisbhaskar
        wrote on last edited by
        #3

        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
        0
        • C Offline
          C Offline
          chax0
          wrote on last edited by
          #4

          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
          0
          • T Offline
            T Offline
            thisisbhaskar
            wrote on last edited by
            #5

            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
            0

            • Login

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