Results 1 to 10 of 10

Thread: errors when using Q_OBJECT and slots

  1. #1
    Join Date
    Oct 2010
    Posts
    58
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    26

    Default errors when using Q_OBJECT and slots

    Hi,
    I'm new to qt programming and I'm trying to make some custom signals and slots but I keep getting errors when I try to do some simple examples.

    I can get this program to run by commenting out Q_OBJECT, but if I leave it in i get errors, any idea why that is?

    Qt Code:
    1. #include <QtGui>
    2. #include <QApplication>
    3. #include <QVBoxLayout>
    4. #include <QLineEdit>
    5. #include <QPushButton>
    6. #include "qobjectdefs.h"
    7.  
    8. class myCanvas : public QWidget{
    9. Q_OBJECT //this program runs fine if i comment out Q_OBJECT
    10. public:
    11. myCanvas (QWidget * parent = 0);
    12. public Q_SLOTS:
    13. void copyText(const QString & text);
    14. };
    15.  
    16. myCanvas::myCanvas(QWidget *parent):QWidget(parent){
    17. QLineEdit * lineedit = new QLineEdit;
    18. QPushButton * pushbutton = new QPushButton("Enter");
    19.  
    20. QVBoxLayout * layout = new QVBoxLayout;
    21. layout->addWidget(lineedit);
    22. layout->addWidget(pushbutton);
    23. setLayout(layout);
    24. }
    25.  
    26. int main (int argc, char * argv[])
    27. {
    28. QApplication app (argc, argv);
    29. myCanvas bob;
    30. bob.show();
    31. return app.exec();
    32. }
    To copy to clipboard, switch view to plain text mode 

    errors with Q_OBJECT uncommented:

    Qt Code:
    1. 1>Linking...
    2. 1>connectWorking.obj : error LNK2001: unresolved external symbol "public: virtual struct QMetaObject const * __thiscall myCanvas::metaObject(void)const " (?metaObject@myCanvas@@UBEPBUQMetaObject@@XZ)
    3. 1>connectWorking.obj : error LNK2001: unresolved external symbol "public: virtual void * __thiscall myCanvas::qt_metacast(char const *)" (?qt_metacast@myCanvas@@UAEPAXPBD@Z)
    4. 1>connectWorking.obj : error LNK2001: unresolved external symbol "public: virtual int __thiscall myCanvas::qt_metacall(enum QMetaObject::Call,int,void * *)" (?qt_metacall@myCanvas@@UAEHW4Call@QMetaObject@@HPAPAX@Z)
    5. 1>...\Debug\funcLoadData.exe : fatal error LNK1120: 3 unresolved externals
    6. 1>funcLoadData - 4 error(s), 0 warning(s)
    7. ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Sep 2010
    Posts
    145
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    1
    Thanked 18 Times in 17 Posts

    Default Re: errors when using Q_OBJECT and slots

    Looks like you didn't implement your copyText() slot.

  3. #3
    Join Date
    Oct 2010
    Posts
    58
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    26

    Default Re: errors when using Q_OBJECT and slots

    Thanks for the fast response.

    I'm sorry if this is obvious, but even if i only put

    Qt Code:
    1. class myCanvas : public QWidget{
    2. Q_OBJECT
    3. public:
    4. myCanvas (QWidget * parent = 0);
    5. };
    To copy to clipboard, switch view to plain text mode 

    and don't try to make my own slot, simply using a default slot like clear()
    Qt Code:
    1. connect(pushbutton, SIGNAL(clicked()), lineedit, SLOT(clear()));
    To copy to clipboard, switch view to plain text mode 

    I still get the same three errors.

    Are these errors coming because Q_OBJECT needs me to have my own slots or should it be able to exist in the class without any custom slots?

    thanks

  4. #4
    Join Date
    Sep 2010
    Posts
    145
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    1
    Thanked 18 Times in 17 Posts

    Default Re: errors when using Q_OBJECT and slots

    Hmm. Is all of your code in connectWorking.cpp? Moc has to see the slots declared in a header (with the Q_OBJECT macro in the class) in order for moc to generate the proper code.

  5. #5
    Join Date
    Oct 2010
    Posts
    58
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    26

    Default Re: errors when using Q_OBJECT and slots

    Yes I did have all of my code in connectWorking.cpp. So I moved it like you said and put the class with Q_OBJECT into a header file then included it in my .cpp but I still get those pesky three errors.

    header file:

    [CODE
    #ifndef SLOTS_H
    #define SLOTS_H

    #include <QtGui>
    #include <QApplication>
    #include <QVBoxLayout>
    #include <QLineEdit>
    #include <QPushButton>

    class myCanvas : public QWidget{
    Q_OBJECT
    QLineEdit *lineedit;
    QPushButton *pushbutton;
    public:
    myCanvas (QWidget * parent = 0);
    public Q_SLOTS:
    void slotButtonClicked();
    };

    #endif SLOTS_H[/CODE]

    .cpp file:

    Qt Code:
    1. #include "slots.h"
    2.  
    3. myCanvas::myCanvas(QWidget *parent):QWidget(parent){
    4. lineedit = new QLineEdit(this);
    5. pushbutton = new QPushButton("Enter", this);
    6.  
    7. connect(pushbutton, SIGNAL(clicked()), this, SLOT(slotButtonClicked()));
    8. QVBoxLayout * layout = new QVBoxLayout;
    9. layout->addWidget(lineedit);
    10. layout->addWidget(pushbutton);
    11. setLayout(layout);
    12. }
    13. void myCanvas::slotButtonClicked()
    14. {
    15. QString theText = lineedit->text();
    16. }
    17.  
    18. int main (int argc, char * argv[])
    19. {
    20. QApplication app (argc, argv);
    21. myCanvas bob;
    22. bob.show();
    23. return app.exec();
    24. }
    To copy to clipboard, switch view to plain text mode 

    Some additional information:
    So now that I implemented my slot, if I comment out Q_OBJECT I get an error in the output window

    "Object::connect: No such slot QWidget::slotButtonClicked() int c:\...\connectworking.cpp:114"

    thanks for helping me out.

  6. #6
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanked 268 Times in 268 Posts
    Wiki edits
    20

    Default Re: errors when using Q_OBJECT and slots

    Just use "public slots:" instead of "public Q_SLOTS:"
    And clean the project completely and rebuild it.

  7. #7
    Join Date
    Oct 2010
    Posts
    58
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    26

    Default Re: errors when using Q_OBJECT and slots

    when I do that I what look like syntax errors:

    Qt Code:
    1. public slots:
    2. void slotButtonClicked();
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. 1>------ Rebuild All started: Project: funcLoadData, Configuration: Debug Win32 ------
    2. 1>Deleting intermediate and output files for project 'funcLoadData', configuration 'Debug|Win32'
    3. 1>Compiling...
    4. 1>connectWorking.cpp
    5. 1>c:\...\slots.h(16) : error C2146: syntax error : missing ':' before identifier 'slots'
    6. 1>c:\...\slots.h(17) : error C2062: type 'void' unexpected
    7. 1>c:\...\slots.h(17) : error C2238: unexpected token(s) preceding ';'
    8. 1>c:\...\connectworking.cpp(120) : error C2039: 'slotButtonClicked' : is not a member of 'myCanvas'
    9. 1> c:\...slots.h(10) : see declaration of 'myCanvas'
    10. 1>c:\...\connectworking.cpp(122) : error C2065: 'lineedit' : undeclared identifier
    11. 1>c:\...\connectworking.cpp(122) : error C2227: left of '->text' must point to class/struct/union/generic type
    12. 1> type is ''unknown-type''
    13. 1>Build log was saved at "file://c:\...\BuildLog.htm"
    14. 1>funcLoadData - 6 error(s), 0 warning(s)
    15. ========== Rebuild All: 0 succeeded, 1 failed, 0 skipped ==========
    To copy to clipboard, switch view to plain text mode 

  8. #8
    Join Date
    Sep 2010
    Posts
    145
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    1
    Thanked 18 Times in 17 Posts

    Default Re: errors when using Q_OBJECT and slots

    I just constructed a project out of your code, and it compiles, links and executes as expected. I did make some minor adjustments (some formatting and streamlining the includes for a bit faster build). Here's what I have:
    Qt Code:
    1. #ifndef SLOTS_H
    2. #define SLOTS_H
    3.  
    4. #include <QWidget>
    5. class QLineEdit;
    6.  
    7. class myCanvas : public QWidget
    8. {
    9. Q_OBJECT
    10. QLineEdit *lineedit;
    11. QPushButton *pushbutton;
    12. public:
    13. myCanvas (QWidget * parent = 0);
    14. public slots:
    15. void slotButtonClicked();
    16. };
    17.  
    18.  
    19. #endif //SLOTS_H
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. /#include "slots.h"
    2. #include <QPushButton>
    3. #include <QLineEdit>
    4. #include <QVBoxLayout>
    5. #include <QApplication>
    6. myCanvas::myCanvas(QWidget *parent):QWidget(parent){
    7. lineedit = new QLineEdit(this);
    8. pushbutton = new QPushButton("Enter", this);
    9.  
    10. connect(pushbutton, SIGNAL(clicked()), this, SLOT(slotButtonClicked()));
    11. QVBoxLayout * layout = new QVBoxLayout;
    12. layout->addWidget(lineedit);
    13. layout->addWidget(pushbutton);
    14. setLayout(layout);
    15. }
    16. void myCanvas::slotButtonClicked()
    17. {
    18. QString theText = lineedit->text();
    19. }
    20.  
    21. int main (int argc, char * argv[])
    22. {
    23. QApplication app (argc, argv);
    24. myCanvas bob;
    25. bob.show();
    26. return app.exec();
    27. }
    To copy to clipboard, switch view to plain text mode 

    It worked before and after the alterations. What does your .pro look like?
    Qt Code:
    1. TEMPLATE = app
    2. CONFIG += qt
    3. QT += gui
    4.  
    5. SOURCES += \
    6. connect.cpp
    7.  
    8. HEADERS += \
    9. slots.h
    To copy to clipboard, switch view to plain text mode 

  9. The following user says thank you to Timoteo for this useful post:

    kja (16th November 2010)

  10. #9
    Join Date
    Oct 2010
    Posts
    58
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    26

    Default Re: errors when using Q_OBJECT and slots

    my .pro file is the same as yours
    Qt Code:
    1. TEMPLATE = app
    2. CONFIG += qt
    3. QT += gui
    4.  
    5. SOURCES += \
    6. connectWorking.cpp
    7.  
    8. HEADERS += \
    9. slots.h
    To copy to clipboard, switch view to plain text mode 

    I just don't understand why I get syntax errors when I try to declare public slots...

    All of the examples that came with Qt use "public slots:" and Q_OBJECT and they all run fine. Could the problem have something to do with my project settings or something like that?

    Thanks for all of your help

  11. #10
    Join Date
    Oct 2010
    Posts
    58
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    26

    Default Re: errors when using Q_OBJECT and slots

    Yea! I got it!

    the problem was that my .moc file was not being generated because I didn't have the correct properties for my .h file.

    I needed to edit the command line and additional dependencies.

    Thanks for all your help Timoteo!

Similar Threads

  1. Linker errors with Q_OBJECT
    By dustin034 in forum Qt Programming
    Replies: 2
    Last Post: 13th May 2010, 19:14
  2. Q_OBJECT macro and link errors
    By pscheven in forum Qt Programming
    Replies: 4
    Last Post: 21st March 2008, 13:23
  3. Creating slots with Q_OBJECT
    By VireX in forum Newbie
    Replies: 14
    Last Post: 31st January 2007, 09:18
  4. When do I need Q_OBJECT?
    By Morea in forum Newbie
    Replies: 1
    Last Post: 24th February 2006, 08:15
  5. Q_object
    By Mariane in forum Newbie
    Replies: 5
    Last Post: 7th February 2006, 17:39

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.