Trouble with signals and slots



  • Hi,
    I have the following button:

    QPushButton *m_mainButton = new QPushButton("Click to \n &Select Image",parent) ;
        m_mainButton->setMaximumWidth (100);
        m_mainButton->setStyleSheet ("background-color: rgba(255, 255, 255, 0);");
        m_mainButton->setStyleSheet("font-size: 16px");
        qDebug() <<"m_mainButton clicked!";
        connect(m_mainButton,&QPushButton::clicked,this,&ImageCorrectButton::selectImageFile);
    

    It all works great, but the signal-slot in the last line doesn't work. After clicking on the button execution stops, but the m_mainButton clicked message is displayed.
    selectImageFile looks like this:

    .h file:

    protected slots:
        void selectImageFile();
    

    .cpp:

    void ImageCorrectButton::selectImageFile()
    {
        qDebug() <<"Entered selectImageFile!";
    }
    

    What did I do incorrectly? Thank you.


  • Moderators

    @gabor53 What do you mean by "execution stops"? Crash? Does it hang?
    If the slot was called then there is no issue with the connect.
    You need to debug your app to see what else happens and where it stops. From the code you provided it is impossible to say what the problem is.



  • It hangs.



  • connect(m_mainButton,&QPushButton::clicked,this,&ImageCorrectButton::selectImageFile);

    Dose "this" class contain "ImageCorrectButton::selectImageFile" slot?



  • @gabor53

    how about giving us a bit more information? E.g. what does the crashreport say?

    anyway, what is parent in

    QPushButton *m_mainButton = new QPushButton("Click to \n &Select Image",parent) ;
    

    does it also crash if you change it to

    QPushButton *m_mainButton = new QPushButton("Click to \n &Select Image",nullptr) ;
    

    ?



  • Hi,
    Yes, the "this" class contains "ImageCorrectButton::selectImageFile" slot.
    Switching from parent to nullptr doesn't change the behavior.

    The reports:
    Locals
    m_mainButton @0x12395cc QPushButton
    parent @0x12395f0 QWidget
    this @0x123957c ImageCorrectButton
    The program stops at the connect.


  • Moderators

    @gabor53 In the first post you said that the slot was called ("but the m_mainButton clicked message is displayed), now you say it is hanging on the connect. Can you please describe the issue clearly?
    If the slot was called then the connect was successful and the problem is somewhere else. You should debug your app to see where exactly it hangs.



  • @gabor53 said in Trouble with signals and slots:

    The program stops at the connect.

    Define "stops"

    • crashes
    • doesn't compile
    • hangs (i.e. looks like an infinite loop)


  • @VRonin
    Hangs describes it the best. Nothing happens even when I click the button again. It feels like it doesn't want to go to the slot. I can click on other buttons after it hangs and does what ever it is supposed to do.



  • ok, try replacing connect(m_mainButton,&QPushButton::clicked,this,&ImageCorrectButton::selectImageFile); with connect(m_mainButton,&QPushButton::clicked,[]()->void{qDebug("Entered selectImageFile!");}); and see if that works



  • @VRonin
    Nothing new happens. "Hangs" at the same location.



  • Nothing gets printed in the debug console?!



  • @VRonin
    Entered createEditor
    Entered image editor
    Entered ImageCorrectButton!
    m_mainButton clicked!

    Locals		
    	m_mainButton	@0x12395cc	QPushButton
    	parent	@0x12395f0	QWidget
    	this	@0x123957c	ImageCorrectButton
    

    1 ImageCorrectButton::ImageCorrectButton imagecorrectbutton.cpp 16 0x41e080
    2 myDelegate::createEditor mydelegate.cpp 49 0x41c61a
    3 QAbstractItemViewPrivate::editor qabstractitemview.cpp 4199 0x15154af1
    4 QAbstractItemViewPrivate::openEditor qabstractitemview.cpp 4384 0x15155895
    5 QAbstractItemView::edit qabstractitemview.cpp 2676 0x1514fa8e
    6 QAbstractItemView::mouseDoubleClickEvent qabstractitemview.cpp 1954 0x1514cf86
    7 QWidget::event qwidget.cpp 8789 0x14f26e82
    8 QFrame::event qframe.cpp 550 0x1503c188
    9 QAbstractScrollArea::viewportEvent qabstractscrollarea.cpp 1213 0x150c1a09
    10 QAbstractItemView::viewportEvent qabstractitemview.cpp 1747 0x1514bd6a
    11 QAbstractScrollAreaPrivate::viewportEvent qabstractscrollarea_p.h 111 0x1528ad14
    12 QAbstractScrollAreaFilter::eventFilter qabstractscrollarea_p.h 127 0x15289b35
    13 QCoreApplicationPrivate::sendThroughObjectEventFilters qcoreapplication.cpp 1099 0x1e4ca0e
    14 QApplicationPrivate::notify_helper qapplication.cpp 3795 0x14eefb50
    15 QApplication::notify qapplication.cpp 3273 0x14eed8b8
    16 QCoreApplication::notifyInternal2 qcoreapplication.cpp 988 0x1e4c6e7
    17 QCoreApplication::sendSpontaneousEvent qcoreapplication.h 234 0x1526c865
    18 QApplicationPrivate::sendMouseEvent qapplication.cpp 2769 0x14eec563
    19 QWidgetWindow::handleMouseEvent qwidgetwindow.cpp 617 0x14f3fb2a
    20 QWidgetWindow::event qwidgetwindow.cpp 239 0x14f3e75e
    ... <More>



  • ok, let's bring out the guns:
    use connect(m_mainButton,&QPushButton::clicked,[]()->void{Q_ASSERT(false);}); this should crash your program (actually assert but stay with me here) as soon as you press m_mainButton

    [Make sure you compile in debug mode and not release]



  • @VRonin
    Nothing happened. Behaves the same way as before.



  • shame on me, it was so obvious and I missed it: QPushButton *m_mainButton = new QPushButton("Click to \n &Select Image",parent) ; you are shadowing your member, use m_mainButton = new QPushButton("Click to \n &Select Image",this) ;



  • @VRonin said in Trouble with signals and slots:

    m_mainButton = new QPushButton("Click to \n &Select Image",this) ;

    This is the message I get in output:

    Entered createEditor
    Entered image editor
    Entered ImageCorrectButton!
    m_mainButton clicked!
    ASSERT: "false" in file ..\Folkfriends_1_0\imagecorrectbutton.cpp, line 18

    This application has requested the Runtime to terminate it in an unusual way.
    Please contact the application's support team for more information.
    C:\Programming\Projects\build-Folkfriends_1_0-Desktop_Qt_5_7_0_MinGW_32bit-Debug\debug\Folkfriends_1_0.exe exited with code 3



  • we found the problem then.
    you can now go back to connect(m_mainButton,&QPushButton::clicked,this,&ImageCorrectButton::selectImageFile); and it should work



  • @VRonin said in Trouble with signals and slots:

    connect(m_mainButton,&QPushButton::clicked,this,&ImageCorrectButton::selectImageFile)

    Thank you. It works now. What was actually wrong with it?


  • Moderators

    @gabor53 As @VRonin said you created another button with same name as in your class:

    // m_mainButton is a new local variable with same name as in the class
    QPushButton *m_mainButton = new QPushButton("Click to \n &Select Image",parent) ;
    

    In your class you have

    QPushButton *m_mainButton;
    

    right?
    But then you create a new one with same name. You just need to remove QPushButton* like this to use m_mainButton from your class:

    // m_mainButton from the class
    m_mainButton = new QPushButton("Click to \n &Select Image",parent) ;
    

    So, you did the connection with another button (not the one which is visible).


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.