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: Q_OBJECT didn't work
QtWS25 Last Chance

Solved: Q_OBJECT didn't work

Scheduled Pinned Locked Moved General and Desktop
10 Posts 5 Posters 6.4k Views
  • 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.
  • R Offline
    R Offline
    romeo.rbw
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0
    • S Offline
      S Offline
      Sam
      wrote on last edited by
      #2

      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()) );
      }@

      1 Reply Last reply
      0
      • JeroentjehomeJ Offline
        JeroentjehomeJ Offline
        Jeroentjehome
        wrote on last edited by
        #3

        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

        Greetz, Jeroen

        1 Reply Last reply
        0
        • G Offline
          G Offline
          guziemic
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0
          • R Offline
            R Offline
            romeo.rbw
            wrote on last edited by
            #5

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

            1 Reply Last reply
            0
            • S Offline
              S Offline
              Sam
              wrote on last edited by
              #6

              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. :)

              1 Reply Last reply
              0
              • R Offline
                R Offline
                romeo.rbw
                wrote on last edited by
                #7

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

                1 Reply Last reply
                0
                • S Offline
                  S Offline
                  Sam
                  wrote on last edited by
                  #8

                  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....."

                  1 Reply Last reply
                  0
                  • D Offline
                    D Offline
                    dbzhang800
                    wrote on last edited by
                    #9

                    [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.

                    1 Reply Last reply
                    0
                    • R Offline
                      R Offline
                      romeo.rbw
                      wrote on last edited by
                      #10

                      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-

                      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