Results 1 to 2 of 2

Thread: Link method to Qpushbutton

  1. #1
    Join Date
    Feb 2017
    Posts
    1
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11

    Default Link method to Qpushbutton

    I have these 3 files below. Basically, I made a method in file LeptonTemp.cpp which I would like to link to a QPushButton. I instantiated the class into my main.cpp file, but when I try to place my method (doSomething) in SLOT, Im getting an error at compilation. Can anyone tell me what I am doing wrong?

    LeptonTemp.cpp
    Qt Code:
    1. LeptonTemp::LeptonTemp(){}
    2. LeptonTemp::~LeptonTemp(){}
    3. void LeptonTemp::doSomething(){
    4. qDebug<<"Hello World";
    5. }
    To copy to clipboard, switch view to plain text mode 
    LeptonTemp.h
    Qt Code:
    1. class LeptonTemp
    2. {
    3. public:
    4. LeptonTemp();
    5. ~LeptonTemp();
    6. public slots:
    7. void doSomething();
    8. };
    To copy to clipboard, switch view to plain text mode 

    main.cpp
    Qt Code:
    1. int main (int argc, char **argv)
    2. {
    3. QApplication a(argc, argv);
    4. QWdiget *myWidget = new QWidget;
    5. myWidget->setGeometry(400,300,340,290);
    6.  
    7. QPushButton *internalTmp = new PushButton("Internal temp", myWidget);
    8. internalTmp->setGeometry(320,290,100,30);
    9.  
    10. LeptonTemp *temp = new LeptonTemp();
    11. QObject::connect(internalTmp, SIGNAL(clicked()), temp, SLOT(doSomething)));
    12.  
    13. myWidget->show();
    14. return a.exec;
    15. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by anda_skoa; 5th February 2017 at 08:03. Reason: missing [code] tags

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts

    Default Re: Link method to Qpushbutton

    If you are using macro based connect(), i.e. the connect() overloads for the SIGNAL and SLOT macros, then the receiver needs to be a QObject derived class and have the Q_OBJECT marker

    Qt Code:
    1. class LeptonTemp : public QObject
    2. {
    3. Q_OBJECT
    4. public:
    To copy to clipboard, switch view to plain text mode 

    Or use "pointer to member function" connect() overload.

    Cheers,
    _

Similar Threads

  1. Replies: 3
    Last Post: 24th July 2011, 22:13
  2. Replies: 11
    Last Post: 5th May 2011, 14:05
  3. Replies: 1
    Last Post: 25th November 2010, 11:37
  4. Replies: 11
    Last Post: 26th May 2010, 05:33
  5. Replies: 2
    Last Post: 20th May 2010, 09:40

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.