Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Trouble with signals and slots
Forum Updated to NodeBB v4.3 + New Features

Trouble with signals and slots

Scheduled Pinned Locked Moved Solved General and Desktop
20 Posts 5 Posters 3.8k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    gabor53
    wrote on last edited by
    #1

    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.

    jsulmJ 1 Reply Last reply
    0
    • G gabor53

      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.

      jsulmJ Online
      jsulmJ Online
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @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.

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      2
      • G Offline
        G Offline
        gabor53
        wrote on last edited by
        #3

        It hangs.

        J.HilkJ 1 Reply Last reply
        0
        • G Offline
          G Offline
          Gmin Gnay
          wrote on last edited by
          #4

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

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

          1 Reply Last reply
          0
          • G gabor53

            It hangs.

            J.HilkJ Offline
            J.HilkJ Offline
            J.Hilk
            Moderators
            wrote on last edited by
            #5

            @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) ;
            

            ?


            Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


            Q: What's that?
            A: It's blue light.
            Q: What does it do?
            A: It turns blue.

            1 Reply Last reply
            0
            • G Offline
              G Offline
              gabor53
              wrote on last edited by
              #6

              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.

              jsulmJ VRoninV 2 Replies Last reply
              0
              • G gabor53

                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.

                jsulmJ Online
                jsulmJ Online
                jsulm
                Lifetime Qt Champion
                wrote on last edited by
                #7

                @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.

                https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                0
                • G gabor53

                  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.

                  VRoninV Offline
                  VRoninV Offline
                  VRonin
                  wrote on last edited by
                  #8

                  @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)

                  "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                  ~Napoleon Bonaparte

                  On a crusade to banish setIndexWidget() from the holy land of Qt

                  G 1 Reply Last reply
                  0
                  • VRoninV VRonin

                    @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)
                    G Offline
                    G Offline
                    gabor53
                    wrote on last edited by
                    #9

                    @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.

                    1 Reply Last reply
                    0
                    • VRoninV Offline
                      VRoninV Offline
                      VRonin
                      wrote on last edited by VRonin
                      #10

                      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

                      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                      ~Napoleon Bonaparte

                      On a crusade to banish setIndexWidget() from the holy land of Qt

                      G 1 Reply Last reply
                      0
                      • VRoninV VRonin

                        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

                        G Offline
                        G Offline
                        gabor53
                        wrote on last edited by
                        #11

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

                        1 Reply Last reply
                        0
                        • VRoninV Offline
                          VRoninV Offline
                          VRonin
                          wrote on last edited by
                          #12

                          Nothing gets printed in the debug console?!

                          "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                          ~Napoleon Bonaparte

                          On a crusade to banish setIndexWidget() from the holy land of Qt

                          G 1 Reply Last reply
                          0
                          • VRoninV VRonin

                            Nothing gets printed in the debug console?!

                            G Offline
                            G Offline
                            gabor53
                            wrote on last edited by
                            #13

                            @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>

                            1 Reply Last reply
                            0
                            • VRoninV Offline
                              VRoninV Offline
                              VRonin
                              wrote on last edited by VRonin
                              #14

                              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]

                              "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                              ~Napoleon Bonaparte

                              On a crusade to banish setIndexWidget() from the holy land of Qt

                              G 1 Reply Last reply
                              0
                              • VRoninV VRonin

                                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]

                                G Offline
                                G Offline
                                gabor53
                                wrote on last edited by
                                #15

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

                                1 Reply Last reply
                                0
                                • VRoninV Offline
                                  VRoninV Offline
                                  VRonin
                                  wrote on last edited by VRonin
                                  #16

                                  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) ;

                                  "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                                  ~Napoleon Bonaparte

                                  On a crusade to banish setIndexWidget() from the holy land of Qt

                                  G 1 Reply Last reply
                                  4
                                  • VRoninV VRonin

                                    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) ;

                                    G Offline
                                    G Offline
                                    gabor53
                                    wrote on last edited by
                                    #17

                                    @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

                                    1 Reply Last reply
                                    0
                                    • VRoninV Offline
                                      VRoninV Offline
                                      VRonin
                                      wrote on last edited by
                                      #18

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

                                      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                                      ~Napoleon Bonaparte

                                      On a crusade to banish setIndexWidget() from the holy land of Qt

                                      G 1 Reply Last reply
                                      0
                                      • VRoninV VRonin

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

                                        G Offline
                                        G Offline
                                        gabor53
                                        wrote on last edited by
                                        #19

                                        @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?

                                        jsulmJ 1 Reply Last reply
                                        0
                                        • G gabor53

                                          @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?

                                          jsulmJ Online
                                          jsulmJ Online
                                          jsulm
                                          Lifetime Qt Champion
                                          wrote on last edited by jsulm
                                          #20

                                          @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).

                                          https://forum.qt.io/topic/113070/qt-code-of-conduct

                                          1 Reply Last reply
                                          2

                                          • Login

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • Users
                                          • Groups
                                          • Search
                                          • Get Qt Extensions
                                          • Unsolved