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. How to disable a button in the slot function?
Forum Update on Monday, May 27th 2025

How to disable a button in the slot function?

Scheduled Pinned Locked Moved General and Desktop
23 Posts 5 Posters 36.2k Views
  • 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
    giesbert
    wrote on last edited by
    #14

    you should use repaint in a different manner:

    @
    void on_button1_clicked()
    {
    ui->button2->setEnabled(false);
    ui->button2->repaint();
    // Do something
    ui->button2->setEnabled(true);
    }
    @

    perhaps then it also works on linux?
    You call repaint typically on the widget you want to repaint, not on yourself with a rectangle of a child. This could lead to no repaint (as your own area is hidden behind the child --> no redraw of your widget is needed).

    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
    • U Offline
      U Offline
      udrnrso
      wrote on last edited by
      #15

      [quote author="Gerolf" date="1301496355"]you should use repaint in a different manner: @ void on_button1_clicked() { ui->button2->setEnabled(false); ui->button2->repaint(); // Do something ui->button2->setEnabled(true); } @ perhaps then it also works on linux? You call repaint typically on the widget you want to repaint, not on yourself with a rectangle of a child. This could lead to no repaint (as your own area is hidden behind the child --> no redraw of your widget is needed).[/quote]
      Thx for your answer!
      Dear Gerolf , I tried your code, also, it perform the same way , unless I put a processEvents() behind ui->button2->repaint(); Is it a bug ?

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

        as per description (and my current usage) repaint should repaint emediatly. In the "docs":http://doc.qt.nokia.com/latest/qwidget.html#repaint there is stated:

        [quote]
        Repaints the widget directly by calling paintEvent() immediately, unless updates are disabled or the widget is hidden.

        We suggest only using repaint() if you need an immediate repaint, for example during animation. In almost all circumstances update() is better, as it permits Qt to optimize for speed and minimize flicker.

        Warning: If you call repaint() in a function which may itself be called from paintEvent(), you may get infinite recursion. The update() function never causes recursion.
        [/quote]

        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
        • A Offline
          A Offline
          andre
          wrote on last edited by
          #17

          According to the documentation, repaint() should trigger an immediate repaint. If it doesn't, then indeed I think you have a bug on your hands.

          1 Reply Last reply
          0
          • I Offline
            I Offline
            ivan.todorovich
            wrote on last edited by
            #18

            Couldn't it be a VMWare problem?
            I mean, from my own experience with VMWare, sometimes it repaints blocks just like it were a VNC window. And if it is trying to get the window's contents to paint it on the host when the event loop is blocked it might fail.

            Maybe we should try the same code on a linux host. Unfortunately I don't have it right now.

            o_o Sorry for my rusted english.

            1 Reply Last reply
            0
            • R Offline
              R Offline
              Revu
              wrote on last edited by
              #19

              Hi All,
              If there are some 'N' buttons that need to be disabled with the same scenario,then how that can be achieved? With the complexity that the slots are interconnected with the independent windows.Like button1 click activates an independent window that has a slot to activate another independent window.With the use of modal dialog this can be achieved only between two windows then how about the rest of the active window?

              Every fall is to raise higher than Before.

              1 Reply Last reply
              0
              • A Offline
                A Offline
                andre
                wrote on last edited by
                #20

                If you have multiple active windows in your application, and you want a modal dialog that is modal relative to all of them, you need an ApplicationModal dialog box. You can make that by using QDialog::setModel(true) or by using QWidget::setWindowModality(Qt::ApplicationModal).

                For disabling and enabling a whole host of widgets in one go, I would change the setup from the last page a bit. Instead of directly disabling and enabling your buttons, I would either simply use signals and slots or use a list of widgets. Using the signal/slot method, you would declare a signal like this:

                in the class declaration
                @
                signals:
                void enableButton(bool);
                @

                Then, of course, you need to hookup your buttons to this signal, for instance in your constructor:
                @
                //make sure this runs once, for instance by putting it in the constructor
                connect (this, SIGNAL(enableButton(bool)), m_ui->button1, SLOT(setEnabled(bool)));
                connect (this, SIGNAL(enableButton(bool)), m_ui->button2, SLOT(setEnabled(bool)));
                connect (this, SIGNAL(enableButton(bool)), m_ui->button3, SLOT(setEnabled(bool)));
                //...
                connect (this, SIGNAL(enableButton(bool)), m_ui->buttonN, SLOT(setEnabled(bool)));
                @

                In your method, you then do something like this:
                @
                #include <QApplication>

                void on_button1_clicked()
                {
                emit enableButton(false);
                QApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
                //do other things that may take serveral seconds

                emit enableButton(true);
                }
                @

                1 Reply Last reply
                0
                • R Offline
                  R Offline
                  Revu
                  wrote on last edited by
                  #21

                  Thank You.It works fine so far it is concerned with button1.When I use button2 I want rest of them to be inactive/disabled.But writing the same code repeatedly by making changes for button that is out of group is complicated.
                  Well I tried with window modality as you have mentioned but still could not get the required result.
                  Is it possible to check for any active window(by using isActiveWindow) before I click on a button?

                  Thank You

                  Every fall is to raise higher than Before.

                  1 Reply Last reply
                  0
                  • A Offline
                    A Offline
                    andre
                    wrote on last edited by
                    #22

                    So, simply disable all buttons like I show above, and re-enable the one button you like to keep enabled?

                    @
                    #include <QApplication>

                    void on_button1_clicked()
                    {
                    emit enableButton(false);
                    button1->setEnabled(true);
                    QApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
                    //do other things that may take serveral seconds

                    emit enableButton(true);
                    }

                    void on_button2_clicked()
                    {
                    emit enableButton(false);
                    button2->setEnabled(true);
                    QApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
                    //do something else that may also take serveral seconds

                    emit enableButton(true);
                    }
                    @

                    There are other solutions, of course.

                    I don't understand where you're going with your last question. What do you mean that you would like to check what the active window is (what do you mean by that, anyway?) when a button is clicked? That would obviously be the window that button is on then, would it not?

                    1 Reply Last reply
                    0
                    • R Offline
                      R Offline
                      Revu
                      wrote on last edited by
                      #23

                      Yes.Suppose If I have 'Nth' subwindow that is on click of 'Nth' button(All the 'N' buttons are on the same window).In that case,I want all the buttons to be disabled apart from the one I clicked.

                      Every fall is to raise higher than Before.

                      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 Extensions
                      • Unsolved