开发者

Qt - no such signal error

开发者 https://www.devze.com 2022-12-26 01:32 出处:网络
I\'m trying to trigger a signal when a double click happens in one of the draggable widgets on the fridge magnets example. Here\'s the changes I made to the example source:

I'm trying to trigger a signal when a double click happens in one of the draggable widgets on the fridge magnets example. Here's the changes I made to the example source:

DragLabel:

class DragLabel : public QLabel
{
public:
    DragLabel(const QString &text, QWidget *parent);
    QString labelText() const;

public slots:
    void testSlot(){qDebug()<<"testSlot";}    //<-- implemented this slot

protected:
    void mouseDoubleClickEvent(QMouseEvent *ev){emit testSignal();}    //<-- overriden this method

private:
    QString m_labelText;

signals:
    void testSignal();    //<-- added this signal

};

The only thing I changed in the implementation file is adding connect(this,SIGNAL(testSignal()),this,SLOT(testSlot())); to DragLabel's co开发者_如何学Pythonnstructor.

Trying to compile the project resulted in 'undefined reference to `DragLabel::testSignal()' and 'collect2: ld returned 1 exit status' errors.

When I comment out the call to the signal, it compiles and runs, but gives off 'Object::connect: No such signal QLabel::testSignal() in draglabel.cpp' warning in the application output. Apparently testSignal() isn't being recognized as a signal.

I've tried to add the Q_OBJECT macro to DragLabel but it results in 4 'undefined reference to `vtable for DragLabel'' warnings and a 'collect2: ld returned 1 exit status' error.

What am I missing?


Put the Q_OBJECT macro at the top, (must be first thing in the class and no ";" )

Make sure you do a full rebuild, the VS-add-in especially doesn't always notice that a file has become qt-aware without a rebuild.

More good advice 20 ways to debug Qt signals and slots


It WAS the macro in the end. I had to reboot my PC for it to work though, cleaning and rebuilding the project didn't work out. Before rebooting Qt Creator kept giving the 'ld returned 1 exit status' error and the vtable warnings. Really weird. – David McDavidson

It's not weird, it's stupid. I got the same error, but I make it after rearrange the .h files. Say:

1 classA.h include calssB.h ;

2 classB.h declared two class, classB and classC, (classB.h declared signals & slot)

I do three things,

  1. seperate classC to another .h file

  2. eliminate all forward class declaration about classB

  3. classB.h included by classA.cpp, other than by classA.h

after that, QT compiled it. I'll test if it is working.

0

精彩评论

暂无评论...
验证码 换一张
取 消