1

I have the following class:

class MySqlTableModel : public QSqlRelationalTableModel
{
    //Q_OBJECT

public:
    MySqlTableModel();

    QSqlQuery add;
    QSqlQuery login;
    QSqlQuery logout;

    void addUser(QString text);
    virtual Qt::ItemFlags flags(const QModelIndex &index) const;
    bool adding = false;

public slots:
    void loginSlot();
    void logoutSlot();
};

Which derives from QSqlRelationalTableModel. I want to use some extra slots. My issue is, that if I comment the Q_OBJECT tag out, the slots are not recognized, and not connected(QObject::connect: No such slot QSqlRelationalTableModel::MySqlTableModel::loginSlot()). If I add the Q_OBJECT tag, however, I get an error: /mysqltablemodel.cpp:5: error: undefined reference to vtable for MySqlTableModel', marking my constructor:

MySqlTableModel::MySqlTableModel()
{
}

[EDIT] Running qmake solved the compile error, but connect still doesn't seem to work. Am I doing something wrong here? Thank you!

lte__
  • 7,175
  • 25
  • 74
  • 131
  • 2
    Try running qmake. – thuga May 03 '16 at 11:14
  • What build system are you using? Is it set up correctly for calling the moc over your classes? If you are using qmake, run it again. – Matteo Italia May 03 '16 at 11:14
  • I'm using Qt Creator, I've tried compiling multiple times. – lte__ May 03 '16 at 11:17
  • Select `Build` => `Run qmake` from the menubar. – thuga May 03 '16 at 11:18
  • Thanks, now I don't get the compile error, but it still cannot find the slots. What am I missing? – lte__ May 03 '16 at 11:20
  • Oops, yes, duplicate. Deleted my answer. Indeed, you need to do "Build->Run qmake" every time you add `Q_OBJECT` somewhere. – Nikos C. May 03 '16 at 11:20
  • qmake isn't exactly the most solid build system out there. If your slots can't be found, make sure to do a full clean/rebuild and see if the issue persists. Also, avoid using `SIGNAL()/SLOT()` syntax. Instead, use the modern syntax: `connect(srcObject, &ClassName::signalName, targetObj, &ClassName::slotName)` (note: `signalName` and `slotName` don't have `()`, they're function pointers.) With this syntax, your signal/slot connections are checked at compile time, not at run time. Also, `slotName` can be any function or lambda, it does not have to be a slot. – Nikos C. May 03 '16 at 11:22
  • Thanks, that did it! Okay, I'll keep that in mind. – lte__ May 03 '16 at 12:21

0 Answers0