Results 1 to 3 of 3

Thread: Help in creating and monitoring widget all in code.

  1. #1
    Join Date
    May 2018
    Posts
    13
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Windows

    Default Help in creating and monitoring widget all in code.

    I found a tutorial about creating widgets and layouts without using the Qt designer:
    http://www.bogotobogo.com/Qt/Qt5_Lay...ngDesigner.php

    What I'm stuck on is how to connect the buttons to slots.
    The following code compiles, but it gets all sorts of linking errors.I've commented out the code that causes the linking errors.

    Any help getting this to work would be appreciated.

    .pro file is:
    Qt Code:
    1. QT += widgets
    2.  
    3. SOURCES += \
    4. main.cpp
    To copy to clipboard, switch view to plain text mode 

    Source is:
    Qt Code:
    1. #include <QApplication>
    2. #include <QPushButton>
    3. #include <QtCore>
    4. #include <QtGui>
    5. #include <QDebug>
    6. #include <QObject>
    7.  
    8. //class ButtonWatcher : public QObject{
    9. // Q_OBJECT
    10. //public:
    11. // ButtonWatcher(QObject *parent = 0);
    12. //public slots:
    13. // void buttonPressed();
    14.  
    15. //};
    16. //ButtonWatcher::ButtonWatcher(QObject *parent) : QObject(parent){
    17.  
    18. //}
    19. //void ButtonWatcher::buttonPressed(){
    20. // qDebug()<<"Button clicked";
    21. //}
    22.  
    23. int main(int argc, char* argv[])
    24. {
    25. QApplication app(argc, argv);
    26. QPushButton *b = new QPushButton("Click me");
    27. // ButtonWatcher *button = new ButtonWatcher();
    28. // QObject::connect(b,SIGNAL(clicked(bool)),button,SLOT(buttonPressed()));
    29. b->show();
    30. return app.exec();
    31.  
    32. }
    To copy to clipboard, switch view to plain text mode 


    Here are the linking errors:
    main.obj:-1: error: LNK2001: unresolved external symbol "public: virtual struct QMetaObject const * __cdecl ButtonWatcher::metaObject(void)const " (?metaObject@ButtonWatcher@@UEBAPEBUQMetaObject@@X Z)
    main.obj:-1: error: LNK2001: unresolved external symbol "public: virtual void * __cdecl ButtonWatcher::qt_metacast(char const *)" (?qt_metacast@ButtonWatcher@@UEAAPEAXPEBD@Z)
    main.obj:-1: error: LNK2001: unresolved external symbol "public: virtual int __cdecl ButtonWatcher::qt_metacall(enum QMetaObject::Call,int,void * *)" (?qt_metacall@ButtonWatcher@@UEAAHW4Call@QMetaObje ct@@HPEAPEAX@Z)
    debug\WithoutDesigner.exe:-1: error: LNK1120: 3 unresolved externals
    Last edited by shawnlau; 18th June 2018 at 21:26.

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Help in creating and monitoring widget all in code.

    The problem is that you didn't run moc on your ButtonWhatcher.
    Put the ButtonWatcher in its own header and include that header in main.
    Then run qmake again, and rebuild.
    It should link then.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. The following user says thank you to high_flyer for this useful post:

    shawnlau (18th June 2018)

  4. #3
    Join Date
    May 2018
    Posts
    13
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Help in creating and monitoring widget all in code.

    That worked. Thank you.

Similar Threads

  1. Directory monitoring
    By mouni in forum Newbie
    Replies: 17
    Last Post: 22nd June 2015, 16:40
  2. Replies: 4
    Last Post: 9th June 2010, 07:46
  3. Creating Qgraphics View code
    By amagdy.ibrahim in forum Qt Programming
    Replies: 9
    Last Post: 15th June 2008, 15:43
  4. Creating simple text editor from the eamples code
    By overcast in forum Qt Programming
    Replies: 4
    Last Post: 14th February 2007, 15:46
  5. Creating a global array in my code???
    By therealjag in forum General Programming
    Replies: 5
    Last Post: 13th March 2006, 11:13

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.