Solved: Q_OBJECT didn't work



  • Hi...
    I made two dialogs, one of them using signal-slot. Before I wrote Q_OBJECT on the class, there are messages:

    Object::connect: No such slot QDialog::on_buttonCamera_clicked()
    Object::connect: (sender name: 'buttonCamera')
    Object::connect: (receiver name: 'DialogSetup')

    I put the Q_OBJECT on the class but there are many errors after that, the error message are:

    LNK2001: unresolved external symbol "public virtual struct QMetaObject const...
    LNK2001: unresolved external symbol "public virtual void*__thiscall DialogSetup....
    LNK2001: unresolved external symbol "public virtual int*__thiscall DialogSetup....
    LNK1120: 3 unresolved externals

    Would you help me and give some suggestion? Here is the code:

    dialogsetup.h:
    @
    #ifndef DIALOGSETUP_H
    #define DIALOGSETUP_H

    #include <QDialog>
    #include <QTime>

    #include "ui_dialogsetup.h"

    #include <opencv2/core/core.hpp>
    #include <opencv2/highgui/highgui.hpp>

    using namespace cv;
    using namespace std;

    class DialogSetup : public QDialog
    {
    Q_OBJECT //???

    public:
    DialogSetup( QWidget *parent=0 );

    protected:
    void timerEvent(QTimerEvent *event);

    private slots:
    void on_buttonCamera_clicked();

    private:
    Ui::DialogSetup ui;

    };

    #endif // DIALOGSETUP_H
    @

    dialogsetup.cpp

    @
    #include "dialogsetup.h"

    DialogSetup::DialogSetup( QWidget *parent ) : QDialog( parent )
    {
    ui.setupUi(this);
    connect(ui.buttonCamera, SIGNAL(clicked()),
    this, SLOT(on_buttonCamera_clicked()) );
    }

    void DialogSetup::on_buttonCamera_clicked()
    {
    ui.label_info->setText(QString("test button click"));
    }

    void DialogSetup::timerEvent(QTimerEvent *event)
    {
    }
    @

    Thanks



  • Add

    @namespace Ui {
    class DialogSetup;
    }@

    In the private section you need to declare the Ui like

    .h
    @Ui::DialogSetup *ui;@

    cpp
    @DialogSetup::DialogSetup( QWidget *parent ) : QDialog( parent ) ,
    ui(new Ui::DialogSetup)
    {
    ui->setupUi(this);
    connect(ui->buttonCamera, SIGNAL(clicked()), this, SLOT(on_buttonCamera_clicked()) );
    }@



  • Hi there,
    When using the designer you get a ui pointer to your form. So the connect should be made with ui->buttonCamera.
    AFAIK the Q_OBJECT isn't needed in your class definition because you inherit from QDialog which already is a Q_OBJECT.
    You better check the connect option again. Somethings gets wrong there. A good way is to comment the connect. See if it compiles, runs and shows the correct dialog. Then start the connect options.
    greetz



  • Hi,

    Could you post .pro file. I think that reason of unresolved symbols is missing line like

    @
    HEADERS += DialogSetup.h //file name of header class
    @

    In case when you are using signals and slots in your derivative class I think that you need to add Q_OBJECT macro. In other case you do not need it.



  • Thanks for reply...it still cannot work, there are 4 error messages like before. May be is there another suggestion?
    Thanks.



  • Ok then the best thing is that you can zip your project and upload it somewhere and share the link here. So that we can have a look and do the needful. :)



  • In the .pro file, there is already dialogsetup.h on the HEADER section.



  • This seems to work

    .h

    @#include <QDialog>
    #include <QString>
    #include <QTimerEvent>

    namespace Ui {
    class DialogSetup;
    }

    class DialogSetup : public QDialog
    {
    Q_OBJECT

    public:
    explicit DialogSetup(QWidget *parent = 0);
    ~DialogSetup();

    protected:
    void timerEvent(QTimerEvent *event);

    private slots:
    void onButtonCameraClicked();

    private:
    Ui::DialogSetup *ui;
    };@

    .cpp

    @DialogSetup::DialogSetup(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::DialogSetup)
    {
    ui->setupUi(this);
    connect(ui->pushButtonCamera,SIGNAL(clicked()),SLOT(onButtonCameraClicked()));

    }

    DialogSetup::~DialogSetup()
    {
    delete ui;
    }

    void DialogSetup::timerEvent(QTimerEvent *event)
    {

    }

    void DialogSetup::onButtonCameraClicked()
    {
    ui->labelInfo->setText(QString("test button click"));
    }@

    Also make sure that when you write your own slots try not to use "on_" prefix. Otherwise it give
    " QObject::no such slot on....."



  • [quote author="romeo.rbw" date="1343892052"]
    I put the Q_OBJECT on the class but there are many errors after that, the error message are:

    LNK2001: unresolved external symbol "public virtual struct QMetaObject const...
    LNK2001: unresolved external symbol "public virtual void*__thiscall DialogSetup....
    LNK2001: unresolved external symbol "public virtual int*__thiscall DialogSetup....
    LNK1120: 3 unresolved externals

    [/quote]

    Do not forget to run "qmake" again after you adding Q_OBJECT.



  • Thank you for every reply and your suggestion, it works now. The signal-slot works. The code become:

    .h
    @
    #ifndef DIALOGSETUP_H
    #define DIALOGSETUP_H

    #include <QDialog>

    namespace Ui {
    class DialogSetup;
    }

    class DialogSetup : public QDialog
    {
    Q_OBJECT

    public:
    explicit DialogSetup(QWidget *parent = 0);
    ~DialogSetup();

    private:
    Ui::DialogSetup *ui;

    private slots:
    void doButtonCameraClick();
    };

    #endif // DIALOGSETUP_H
    @

    .cpp

    @
    #include "dialogsetup.h"
    #include "ui_dialogsetup.h"

    DialogSetup::DialogSetup(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::DialogSetup)
    {
    ui->setupUi(this);
    connect(ui->buttonCamera, SIGNAL(clicked()),
    this, SLOT(doButtonCameraClick()) );
    }

    DialogSetup::~DialogSetup()
    {
    delete ui;
    }

    void DialogSetup::doButtonCameraClick(){
    ui->labelTestSetup->setText(QString("Camera On"));
    }

    @

    cheers,
    romeo-


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.