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. QPushButton staying pressed too long
Qt 6.11 is out! See what's new in the release blog

QPushButton staying pressed too long

Scheduled Pinned Locked Moved Unsolved General and Desktop
16 Posts 2 Posters 5.6k Views 3 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.
  • M MaxDevI

    @Wieland I don't really find the QThread to be simple. Instead tried to do

    QFuture<void> future = QtConcurrent::run(aFunction, arg1, arg2, ...);
    

    but it gave me many errors saying "QtConcurrent::run expects X arguments - Y provided". I tried for a while to fix this but could never overcome the error,=

    ? Offline
    ? Offline
    A Former User
    wrote on last edited by
    #6

    @MaxDevI Can you please show us the actual declaration of your "aFunction" and the actual line with "QFuture.." ?

    1 Reply Last reply
    1
    • M Offline
      M Offline
      MaxDevI
      wrote on last edited by
      #7

      @Wieland

      I just got the Qtconcurrent to run, but it gives me Microsoft Visual C++ Runtime Library Debug Error saying

      "ASSERT failure in QCoreApplication::sendEvent: Cannot send events to objects owned by a different thread..."

      1 Reply Last reply
      0
      • ? Offline
        ? Offline
        A Former User
        wrote on last edited by
        #8

        Seems like your aFunction is interacting with objects that live in the GUI thread.

        1 Reply Last reply
        0
        • M Offline
          M Offline
          MaxDevI
          wrote on last edited by
          #9

          @Wieland yes it is, can I do anything to fix this other than rewriting?

          1 Reply Last reply
          0
          • ? Offline
            ? Offline
            A Former User
            wrote on last edited by
            #10

            Might be easy to fix without rewriting it. What exactly do you do that causes the problem?

            1 Reply Last reply
            0
            • M Offline
              M Offline
              MaxDevI
              wrote on last edited by
              #11

              @Wieland There is so much going on in the function it is pretty hard to explain. It goes through many loops looking through widgets and checking their values, and updating other widgets on other tabs that correspond.

              Let's say I have tab A, B, C with information on each tab. If I press the button on tab A, it will scan tab A for relative information (predetermined) to send over the tab B and C. Then it goes to tab B and C and enters the information from A, then returns to tab A.

              This is why I get problems when using processEvents() because of the tab switching.

              ? 1 Reply Last reply
              0
              • M MaxDevI

                @Wieland There is so much going on in the function it is pretty hard to explain. It goes through many loops looking through widgets and checking their values, and updating other widgets on other tabs that correspond.

                Let's say I have tab A, B, C with information on each tab. If I press the button on tab A, it will scan tab A for relative information (predetermined) to send over the tab B and C. Then it goes to tab B and C and enters the information from A, then returns to tab A.

                This is why I get problems when using processEvents() because of the tab switching.

                ? Offline
                ? Offline
                A Former User
                wrote on last edited by
                #12

                @MaxDevI Ok, that's a problem that actually cannot be fixed simply because one must never directly interact with GUI elements (widgets) from a thread other then the main thread (aka GUI thread) in Qt. That's simply not supported. But you still can send signals from other threads to the widgets on the GUI thread. They will be processed asynchronously by default. So, yeah, looks like you'll have to refactor your code a bit :-/

                M 1 Reply Last reply
                0
                • ? A Former User

                  @MaxDevI Ok, that's a problem that actually cannot be fixed simply because one must never directly interact with GUI elements (widgets) from a thread other then the main thread (aka GUI thread) in Qt. That's simply not supported. But you still can send signals from other threads to the widgets on the GUI thread. They will be processed asynchronously by default. So, yeah, looks like you'll have to refactor your code a bit :-/

                  M Offline
                  M Offline
                  MaxDevI
                  wrote on last edited by
                  #13

                  @Wieland Can I send a signal from the other thread to the GUI thread which will set the button to unpressed immediately after it is pressed? Seems like it would work somehow?

                  1 Reply Last reply
                  0
                  • M Offline
                    M Offline
                    MaxDevI
                    wrote on last edited by
                    #14

                    @Wieland Maybe I could set the pushbutton to unpressed before making the computations which cause the delay???

                    1 Reply Last reply
                    0
                    • M Offline
                      M Offline
                      MaxDevI
                      wrote on last edited by MaxDevI
                      #15

                      @Wieland I did it!

                      It was a workaround, no doubt about it, but it seems to work! Here is what I had to do.

                      1. I connect GUI object to the class object which was performing the long operation. I create my own signal and slot. I call something like this in my GUI class when I instantiate the longOperationObject.
                      connect(longOperationObject, SIGNAL(myLongOpSignal()), this, SLOT(myGUISlot());
                      
                      1. BEFORE performing the long operation in the longOperationObject, I emit myLongOpSignal() which calls myGUISlot(). Then, right BEFORE the long operation, I call QCoreApplication::processEvents(). Then I perform the long operation.
                      emit myLongOpSignal();
                      QCoreApplication::processEvents();
                      longOpFunction(...);
                      
                      1. Now, the magic happens in myGUISlot. In order to make the push button look pressed and released as normal, I set the stylesheet of the button to be pressed. Then I call processEvents again. I wait any amount of time I want using std::clock_t, then I set the button stylesheet back to pressed! In this example the button stays pressed for .5 seconds.
                      void GUI::myGUISlot(){
                          button->setStyleSheet("pressed format...");
                          QCoreApplication::processEvents();
                          std::clock_t start;
                          double duration;
                          start = std::clock();
                          duration = ( std::clock() - start ) / (double) CLOCKS_PER_SEC;
                          while(duration <= .5){
                      
                              duration = ( std::clock() - start ) / (double) CLOCKS_PER_SEC;
                          }
                          button->setStyleSheet("normal un-pressed format...");
                      }
                      

                      Quite the work around...but hey, it works for now. Thanks @Wieland for your help.

                      ? 1 Reply Last reply
                      1
                      • M MaxDevI

                        @Wieland I did it!

                        It was a workaround, no doubt about it, but it seems to work! Here is what I had to do.

                        1. I connect GUI object to the class object which was performing the long operation. I create my own signal and slot. I call something like this in my GUI class when I instantiate the longOperationObject.
                        connect(longOperationObject, SIGNAL(myLongOpSignal()), this, SLOT(myGUISlot());
                        
                        1. BEFORE performing the long operation in the longOperationObject, I emit myLongOpSignal() which calls myGUISlot(). Then, right BEFORE the long operation, I call QCoreApplication::processEvents(). Then I perform the long operation.
                        emit myLongOpSignal();
                        QCoreApplication::processEvents();
                        longOpFunction(...);
                        
                        1. Now, the magic happens in myGUISlot. In order to make the push button look pressed and released as normal, I set the stylesheet of the button to be pressed. Then I call processEvents again. I wait any amount of time I want using std::clock_t, then I set the button stylesheet back to pressed! In this example the button stays pressed for .5 seconds.
                        void GUI::myGUISlot(){
                            button->setStyleSheet("pressed format...");
                            QCoreApplication::processEvents();
                            std::clock_t start;
                            double duration;
                            start = std::clock();
                            duration = ( std::clock() - start ) / (double) CLOCKS_PER_SEC;
                            while(duration <= .5){
                        
                                duration = ( std::clock() - start ) / (double) CLOCKS_PER_SEC;
                            }
                            button->setStyleSheet("normal un-pressed format...");
                        }
                        

                        Quite the work around...but hey, it works for now. Thanks @Wieland for your help.

                        ? Offline
                        ? Offline
                        A Former User
                        wrote on last edited by
                        #16

                        @MaxDevI The main thing is that it works ;-)

                        1 Reply Last reply
                        1

                        • Login

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