Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. I have two buttons, every button has one slot function, i clike two buttons, i want the frist slot function to be executed?
Qt 6.12 LTS is out! See what's new in the release blog

I have two buttons, every button has one slot function, i clike two buttons, i want the frist slot function to be executed?

Scheduled Pinned Locked Moved General and Desktop
19 Posts 7 Posters 9.9k 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.
  • D Offline
    D Offline
    dbzhang800
    wrote on last edited by
    #5

    You can simply disable your second button when the first slot is running. Or you can set a flag to let the second slot exit when entered.

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

      i hope to do it by event or message.

      1 Reply Last reply
      0
      • JKSHJ Offline
        JKSHJ Offline
        JKSH
        Moderators
        wrote on last edited by
        #7

        [quote author="foxgod" date="1382602114"]i hope to do it by event or message.[/quote]Why? Event/message is more complicated.

        Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

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

          i need to do it by event or message. i wrote just now is only a example,it's not my real project.my project need use event or message.please tell me.

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

            if i have three button or more ,whether i need set many flags??

            1 Reply Last reply
            0
            • JKSHJ Offline
              JKSHJ Offline
              JKSH
              Moderators
              wrote on last edited by
              #10

              Try "QObject::blockSignals()":http://qt-project.org/doc/qt-5.1/qtcore/qobject.html#blockSignals

              Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

              1 Reply Last reply
              0
              • G Offline
                G Offline
                giesbert
                wrote on last edited by
                #11

                Hi,

                first of all, slots for UI are typically executed by the main thread. This means, If you have:
                button1 --> slot1
                button2 --> slot2
                button3 --> slot3

                and the user clicks
                button1 --> button3 --> button2
                the slots are executed in this order
                slot1 --> slot3 --> slot2

                if the slots are of objects from different threads, you have no control, which is executed first...

                Nokia Certified Qt Specialist.
                Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

                1 Reply Last reply
                0
                • N Offline
                  N Offline
                  NicuPopescu
                  wrote on last edited by
                  #12

                  bq. if the slots are of objects from different threads, you have no control, which is executed first…

                  I think if signals are from diffrent thread than the receiving object by default with Qt::AutoConnection they are queued, meaning that they wait till the app returns to the receiver's thread but the execution is in the same order of their sending ... but with Qt::DirectConnection the slot is invoked imediatelly

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

                    That's why Gerolf wrote:

                    bq. if the slots are of objects from different threads

                    (emphasis mine)

                    If the objects' event queues run in different threads, you have absolutely no control over the order of execution.

                    1 Reply Last reply
                    0
                    • N Offline
                      N Offline
                      NicuPopescu
                      wrote on last edited by
                      #14

                      bq. If the objects’ event queues run in different threads, you have absolutely no control over the order of execution.

                      I think you missed the part with Qt::DirectConnection: when this happens, the signals and slots mechanism is totally independent of any thread loop, and slot is executed immediately as any function call ... and anyway even the case when slots are in different threads and signals are queued and the slot function is executed later in an undetermined order, this is reduced to the case of a synchronization case between threads ...

                      :)

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

                        [quote author="foxgod" date="1382600959"]i have two buttons, every button has one slot function,the frist
                        button's slot function's execution needs some time,so i want to prevent the second buuton's slot function's execution,when i clike the second button quickly after the frist buttons cliked.[/quote]

                        You want each button to operate completely on its own before another button gets clicked? That's what it sounds like you're wanting

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

                          connect(button, SIGNAL(clicked()), signalMapper, SLOT(map()),Qt::DirectConnection);

                          connect(signalMapper,SIGNAL(mapped(QObject*)),this,SLOT(setBackground(QObject*)),Qt::DirectConnection);
                          

                          i set a flag to prevent the second button to execute its slot function ,but it's no effect。

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

                            [quote author="Gerolf" date="1382615946"]Hi,

                            first of all, slots for UI are typically executed by the main thread. This means, If you have:
                            button1 --> slot1
                            button2 --> slot2
                            button3 --> slot3

                            and the user clicks
                            button1 --> button3 --> button2
                            the slots are executed in this order
                            slot1 --> slot3 --> slot2

                            if the slots are of objects from different threads, you have no control, which is executed first...[/quote]

                            connect(button, SIGNAL(clicked()), signalMapper, SLOT(map()),Qt::DirectConnection);

                            connect(signalMapper,SIGNAL(mapped(QObject*)),this,SLOT(setBackground(QObject*)),Qt::DirectConnection);
                            

                            i set a flag to prevent the second button to execute its slot function ,but it's no effect。

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

                              connect(button, SIGNAL(clicked()), signalMapper, SLOT(map()),Qt::DirectConnection);

                              connect(signalMapper,SIGNAL(mapped(QObject*)),this,SLOT(setBackground(QObject*)),Qt::DirectConnection);
                              

                              i set a flag to prevent the second button to execute its slot function ,but it's no effect。
                              [quote author="SchwenLarson" date="1382641312"]

                              [quote author="foxgod" date="1382600959"]i have two buttons, every button has one slot function,the frist
                              button's slot function's execution needs some time,so i want to prevent the second buuton's slot function's execution,when i clike the second button quickly after the frist buttons cliked.[/quote]

                              You want each button to operate completely on its own before another button gets clicked? That's what it sounds like you're wanting[/quote]

                              1 Reply Last reply
                              0
                              • JKSHJ Offline
                                JKSHJ Offline
                                JKSH
                                Moderators
                                wrote on last edited by
                                #19

                                foxgod, how about you disable all the buttons when you enter your slot, and then re-enable them when your slot finishes? This also tells the user that the program is busy.

                                @
                                QList<QPushButton*> allButtons;

                                void slot1() {
                                for (int i = 0; i < allButtons.size(); ++i)
                                allButtons[i]->setEnabled(false);

                                // Do work
                                
                                for (int i = 0; i < allButtons.size(); ++i)
                                    allButtons[i]->setEnabled(true);
                                

                                }

                                void slot2() {
                                for (int i = 0; i < allButtons.size(); ++i)
                                allButtons[i]->setEnabled(false);

                                // Do work
                                
                                for (int i = 0; i < allButtons.size(); ++i)
                                    allButtons[i]->setEnabled(true);
                                

                                }
                                @

                                [quote author="NicuPopescu" date="1382630065"]I think you missed the part with Qt::DirectConnection: when this happens, the signals and slots mechanism is totally independent of any thread loop, and slot is executed immediately as any function call ... [/quote]...which usually means your slot is now executing in the wrong thread.

                                Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                                1 Reply Last reply
                                0

                                • Login

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