Results 1 to 5 of 5

Thread: Qt signals slots again

  1. #1
    Join Date
    Jul 2010
    Posts
    7
    Thanks
    2
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Windows Symbian S60

    Default Qt signals slots again

    Hi,
    A really newbie question.
    Qt Code:
    1. QPushButton* buttonRun = new QPushButton(QString("Run"));
    2.  
    3. QGridLayout* layout_ = new QGridLayout;
    4. layout_->addWidget(buttonRun,1,0);
    5. widget->setLayout(layout_);
    6.  
    7. connect(buttonRun,SIGNAL(clicked()),this,SLOT(runAlgorithm()));
    To copy to clipboard, switch view to plain text mode 

    connect() returns true, but the slot is never called. I'm pretty sure the signal is emitted, because

    Qt Code:
    1. connect(buttonRun,SIGNAL(clicked()),qApp,SLOT(aboutQt()));
    To copy to clipboard, switch view to plain text mode 
    works. I also think that the slot is defined properly, because if I do something like:
    Qt Code:
    1. QObject::connect(this,SIGNAL(runThisBastard()),this,SLOT(runAlgorithm()));
    2. emit runThisBastard();
    To copy to clipboard, switch view to plain text mode 
    the slot is called properly.

    Just in case here's my definition of the class:
    Qt Code:
    1. class A: public QObject, public IHasPropertiesPage
    2. {
    3. Q_OBJECT
    4. public:
    5. ....
    6. public slots:
    7. void runAlgorithm ();
    8. ...
    9. };
    To copy to clipboard, switch view to plain text mode 

    Any help would be greatly appreciated.

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Qt signals slots again

    Based on what you wrote I can't see an error. So could you please make a minimal, compilable example reproducing your problem.

  3. #3
    Join Date
    Jul 2010
    Posts
    7
    Thanks
    2
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Windows Symbian S60

    Default Re: Qt signals slots again

    Here you go. The full solution (VS 2010) is attached, I paste the code here for convenience. Thanks for help!

    Qt Code:
    1. //A.h
    2. #ifndef _A_H_
    3. #define _A_H_
    4.  
    5. #include <qobject>
    6. #include <qlayout.h>
    7. #include <qpushbutton.h>
    8. #include <qlineedit.h>
    9. #include <QtGlobal>
    10. #include <qapplication.h>
    11.  
    12. #include "IHasPropertiesPage.h"
    13.  
    14. class A: public QObject, public IHasPropertiesPage
    15. {
    16. Q_OBJECT
    17. public:
    18. A() {};
    19. void showProperties(QWidget* widget);
    20. signals:
    21. void runThisBastard();
    22. public slots:
    23. void run();
    24. private:
    25. QLineEdit* edit_;
    26. };
    27.  
    28. #endif
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. //A.cpp
    2. #include "A.h"
    3.  
    4. void A::showProperties(QWidget* widget)
    5. {
    6. edit_ = new QLineEdit(QString::number(0.1));
    7. QPushButton* buttonRun = new QPushButton(QString("Run"));
    8.  
    9.  
    10. //connect(this,SIGNAL(runThisBastard()),this,SLOT(run()));
    11. //emit runThisBastard();
    12. QGridLayout* layout_ = new QGridLayout;
    13. layout_->addWidget(edit_,0,0);
    14. layout_->addWidget(buttonRun,1,0);
    15. widget->setLayout(layout_);
    16. //connect(buttonRun,SIGNAL(clicked()),qApp,SLOT(aboutQt()));
    17. connect(buttonRun,SIGNAL(clicked()),this,SLOT(run()));
    18. }
    19.  
    20. void A::run()
    21. {
    22. int i = 0; //just do something, I set a breakpoint here
    23. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. //tempqt.cpp
    2. #include "tempqt.h"
    3. #include "A.h"
    4. tempQt::tempQt(QWidget *parent, Qt::WFlags flags)
    5. : QMainWindow(parent, flags)
    6. {
    7. ui.setupUi(this);
    8.  
    9. A a;
    10. a.showProperties(ui.centralWidget);
    11. }
    12.  
    13. tempQt::~tempQt()
    14. {}
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. //tempqt.h
    2. #ifndef TEMPQT_H
    3. #define TEMPQT_H
    4.  
    5. #include <QtGui/QMainWindow>
    6. #include "ui_tempqt.h"
    7.  
    8. class tempQt : public QMainWindow
    9. {
    10. Q_OBJECT
    11. public:
    12. tempQt(QWidget *parent = 0, Qt::WFlags flags = 0);
    13. ~tempQt();
    14. private:
    15. Ui::tempQtClass ui;
    16. };
    17.  
    18. #endif // TEMPQT_H
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. //IHasPropertiesPage.h
    2. #ifndef _IHASPROPERTIESPAGE_H_
    3. #define _IHASPROPERTIESPAGE_H_
    4.  
    5. #include <qwidget.h>
    6.  
    7. class IHasPropertiesPage
    8. {
    9. public:
    10. virtual void showProperties(QWidget* widget) = 0;
    11. };
    12.  
    13. #endif
    To copy to clipboard, switch view to plain text mode 

    tempQt.zip

  4. #4
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Qt signals slots again

    Well A::run() could not be reached, since it is long go... It is deleted after the c-tor (Basic C++ )! So make A a private member or create it on the heap.

  5. #5
    Join Date
    Jul 2010
    Posts
    7
    Thanks
    2
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Windows Symbian S60

    Default Re: Qt signals slots again

    Wow, this is stupid of me. Thanks!

Similar Threads

  1. QT SIGNALS and SLOTS
    By beginQT in forum Newbie
    Replies: 7
    Last Post: 23rd September 2011, 15:40
  2. about signals and slots
    By Sandip in forum Qt Programming
    Replies: 9
    Last Post: 15th July 2008, 17:02
  3. Signals and Slots
    By 83.manish in forum Qt Programming
    Replies: 3
    Last Post: 30th June 2008, 11:31
  4. Signals and slots
    By sylvarant in forum Newbie
    Replies: 4
    Last Post: 11th September 2007, 16:48
  5. Signals and Slots
    By merry in forum Qt Programming
    Replies: 4
    Last Post: 22nd February 2007, 09:11

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