[SOLVED] Linking errors
-
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();
}
@ -
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.
-
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.@
-
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 ;-)!
-
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.