Results 1 to 15 of 15

Thread: Problem with the Slot in QObject::connect(...)

  1. #1
    Join Date
    May 2011
    Posts
    9
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Problem with the Slot in QObject::connect(...)

    Hello everybody,
    I tried to make a little Wrapper-Class for the QPushButton, where I need to gather the Signal clicked() from the Button and rout it to a new written slot called "DoCommand()".


    But this slot is never called by the Button's clicked() signal.
    The compiler, linker run well. The Application itself runs "well", so without this functionality I told^^

    Here is the Headerfile:
    Qt Code:
    1. #pragma once
    2.  
    3. #include <QtGui\qpushbutton.h>
    4.  
    5.  
    6.  
    7. class MenuButton : QObject
    8. {
    9. Q_OBJECT
    10. public:
    11. MenuButton(std::string &title);
    12. ~MenuButton()
    13. {
    14. //delete pButton;
    15. }
    16.  
    17. std::string command;
    18.  
    19. QPushButton *pButton;
    20.  
    21. public slots:
    22. void DoCommand(void);
    23. };
    To copy to clipboard, switch view to plain text mode 
    And here the soure file:
    Qt Code:
    1. #include "MenuButton.h"
    2. #include "../Kernel/ActionHandler.h"
    3.  
    4.  
    5. MenuButton::MenuButton(std::string &title) : QObject()
    6. {
    7. pButton = new QPushButton(title.c_str());
    8. pButton->setFixedSize(80, 45);
    9. //QAction *action = new QAction(title.c_str(), this);
    10. //action->connect(action, SIGNAL(triggered), this, SLOT(Clicked()));
    11. //pButton->addAction(action);
    12. bool success = QObject::connect(pButton, SIGNAL(clicked()), this, SLOT(DoCommand()));
    13. Q_UNUSED(success);
    14. Q_ASSERT(success);
    15. }
    16.  
    17.  
    18. void MenuButton::DoCommand(void)
    19. {
    20. LocalActionHandler->DoScript(command);
    21. LocalActionHandler->DoScript(QString("print \"DoCommand was called\""));
    22. }
    To copy to clipboard, switch view to plain text mode 
    Ehm, I forgott to say: success is always true in this case.

    When I used an other slot from an other class' instance, for testing, everything worked well.


    Does someone know what could be wrong ?
    Maybe I made an stupid mistake anywhere..

    Thanks a lot,


    Matthias

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Problem with the Slot in QObject::connect(...)

    What if you put the following at the beginning of DoCommand():
    Qt Code:
    1. qDebug() << Q_FUNC_INFO;
    To copy to clipboard, switch view to plain text mode 

    Does the message get printed to the console?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    May 2011
    Posts
    9
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problem with the Slot in QObject::connect(...)

    Quote Originally Posted by wysota View Post
    What if you put the following at the beginning of DoCommand():
    Qt Code:
    1. qDebug() << Q_FUNC_INFO;
    To copy to clipboard, switch view to plain text mode 

    Does the message get printed to the console?
    No the methode "DoCommand()" is never ever called, I saw by the debugger.
    And I would see it at the fuction:
    Qt Code:
    1. LocalActionHandler->DoScript(QString("print \"DoCommand was called\""));
    To copy to clipboard, switch view to plain text mode 
    if this methode is called.

    But I have also disabled the console.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Problem with the Slot in QObject::connect(...)

    Does the meta-object for your class get built by moc?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Jan 2011
    Location
    Sri Lanaka
    Posts
    64
    Thanks
    39
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60

    Default Re: Problem with the Slot in QObject::connect(...)

    it should be like this know??
    Qt Code:
    1. bool success = QObject::connect(&pButton, SIGNAL(clicked()), this, SLOT(DoCommand()));
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Problem with the Slot in QObject::connect(...)

    Your signal and slot signatures does not match, use released() instead of clicked()
    Qt Code:
    1. connect(pButton, SIGNAL(released()), this, SLOT(DoCommand()));
    To copy to clipboard, switch view to plain text mode 


    Added after 6 minutes:


    Quote Originally Posted by deepal_de View Post
    it should be like this know??
    Qt Code:
    1. bool success = QObject::connect(&pButton, SIGNAL(clicked()), this, SLOT(DoCommand()));
    To copy to clipboard, switch view to plain text mode 
    pButton is a pointer to QPushButton, using &pButton will not work (It will not even compile), because you are supplying a QPushbutton** where as it expects QPushButton* (actually it expects anything which is acceptable in place of QObject*)
    Last edited by Santosh Reddy; 31st May 2011 at 07:55.

  7. #7
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Problem with the Slot in QObject::connect(...)

    Your signal and slot signatures does not match, use released() instead of clicked()
    What's wrong with clicked() ? It will work with SLOTs that don't expects any parameters ( declared as void method(), or void method(void), no difference ).
    Have you tried clean rebuild ? (make clean, qmake, make)

  8. #8
    Join Date
    May 2011
    Posts
    9
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problem with the Slot in QObject::connect(...)

    You could be right stampede.

    The chnage to released() does not matter. I have tried it out.

    I think there must be somiting wrong with the SLOT.
    Yes of course does the meta-object for my class get built by moc.

    Oh I have no Idea what could be wrong. Maybe have done one very stupid mistake...

    I have also cleaned the projekts in visual studio and build them again.

  9. #9
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Problem with the Slot in QObject::connect(...)

    Are you sure Visual Studio removes moc_* files when you "clean" the project ? I'd suggest using the cmd, this way you'll know for sure if project is cleaned properly. I can't see anything wrong in this code.
    Btw. if you want to provide custom button class, consider subclassing the QPushButton, rather than QObject, and connect the "clicked()" signal in your derived class' constructor to some slot where you'll handle the command execution.
    It is more sensible, IMHO, to call for example
    Qt Code:
    1. MenuButton * button = ...
    2. ...
    3. layout->addWidget( button );
    To copy to clipboard, switch view to plain text mode 
    than
    Qt Code:
    1. MenuButton * button = ...
    2. ...
    3. layout->addWidget( button->pButton );
    To copy to clipboard, switch view to plain text mode 
    Furthermore, you can use "promote to" feature of the designer this way.

  10. #10
    Join Date
    May 2011
    Posts
    9
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problem with the Slot in QObject::connect(...)

    You are right stampede, but in later times only this wrapper-class will be used directly in this context, so it should be no matter if, it is a subclass or it has a QPushbutton as mamber. In my way it is easier to encapsulate the QPushButton class.

    I forgot to mention- that this wrapperclass is only instanced by a running python-interpreter.

  11. #11
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Problem with the Slot in QObject::connect(...)

    Yeah, should work no matter of composition or inheritance.
    In that case, are you sure this is not some kind of python-related issue ? You should be able to use this class in "regular" Qt app without any problems.

  12. #12
    Join Date
    May 2011
    Posts
    9
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problem with the Slot in QObject::connect(...)

    Hm ... I can not see the difference between instancing a QObject by python or by the c++ programm itself ?
    Is there an other nor to complex way of listing to events/signals of a QObject, like QPushButton ??

    Thanks

  13. #13
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Problem with the Slot in QObject::connect(...)

    If you call QObject::dumpObjectInfo() in debug build, it will print a list of currently connected signals for an object.

  14. #14
    Join Date
    May 2011
    Posts
    9
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problem with the Slot in QObject::connect(...)

    It prints out:


    OBJECT MenuButton::unnamed
    SIGNALS OUT
    <None>
    SIGNALS IN
    <-- QPushButton::unnamed DoCommand()
    Do you know where the failing is ?
    Last edited by d'Matthias; 3rd June 2011 at 23:25.

  15. #15
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Problem with the Slot in QObject::connect(...)

    Here is small test app, try if you can use this class in python interpreter, and in regular build:
    Qt Code:
    1. // Test.h
    2. #include <QtGui>
    3. #include <QDebug>
    4.  
    5. class Test : public QObject{
    6. Q_OBJECT
    7. public:
    8. Test( QObject * parent = NULL ) : QObject(parent){
    9. setObjectName("test");
    10. btn = new QPushButton();
    11. btn->setObjectName("button");
    12. connect(btn,SIGNAL(clicked()), this, SLOT(test()));
    13. this->dumpObjectInfo();
    14. }
    15. ~Test(){
    16. delete btn;
    17. }
    18.  
    19. QPushButton * btn;
    20. public slots:
    21. void test(){
    22. qDebug() << "clicked";
    23. }
    24. };
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. // main.cpp
    2. #include <QApplication>
    3. #include "Test.h"
    4.  
    5. int main( int argc, char ** argv ){
    6. QApplication app(argc,argv);
    7. Test t; t.btn->show();
    8. return app.exec();
    9. }
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. // Test.pro
    2. TEMPLATE = app
    3. TARGET =
    4. DEPENDPATH += .
    5. INCLUDEPATH += .
    6.  
    7. SOURCES += main.cpp
    8. HEADERS += Test.h
    To copy to clipboard, switch view to plain text mode 
    Here is the output for dumpObjectInfo for this class:
    OBJECT Test::test

    SIGNALS OUT
    <None>

    SIGNALS IN
    <-- QPushButton::button clicked()
    As you can see, the button is connected with "clicked()" signal.
    Try it and post your results, from python-interpreter, and "normal" application.

Similar Threads

  1. problem connect signal - slot
    By jaca in forum Newbie
    Replies: 13
    Last Post: 9th March 2010, 20:38
  2. Problem in QObject::connect( ... ) with smart_ptr ( Boost )
    By kunalnandi in forum General Programming
    Replies: 1
    Last Post: 15th October 2008, 08:44
  3. QObject::connect: No such slot !?!
    By Mystical Groovy in forum Qt Programming
    Replies: 3
    Last Post: 18th September 2008, 19:31
  4. Replies: 2
    Last Post: 24th March 2008, 17:59
  5. Replies: 4
    Last Post: 10th November 2006, 16:38

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.