Important: Please read the Qt Code of Conduct -

Refresh QPushButton with mouse position

  • Hi.

    I create a new window that gets hidden and showed once every while. You open it with a button in the main window, and close it with it's own button that lies inside itself.

    The problem is, when I close it with that button inside itself and re-show it again with the button inside main window, it does appear, but the button to close the window ( the one inside itself ) still has focus ( like the user would still hover over it ).

    I've been trying to fix this with no success. Is there a way to refresh the button with mouse position so that it doesn't get on-hovered anymore? Or is there any other way?

    ( I cannot delete the window completely & spawn it again. I must hide & show it )

  • Lifetime Qt Champion


    do you mean you would like to have it lose its focus ?

  • It remains highlighted, as if mouse was still over it.

  • Lifetime Qt Champion

    You can try calling clearFocus on it

  • Nope, doesn't work.

    To be clear, when it is highlighted (like mouse was over it), and when I in fact hover my mouse over it, and then move my mouse outside it, it get's unhighlighted (as expected).

    Which means that when I press the button and the window is hidden, the button doesn't detect that the mouse was moved outside it, thus it doesn't change it's state to unhighlighted.

    I can't believe nobody had such problem before. It should be pretty common.

  • nope it isn't a common problem and I do not actually understand your problem.. are you saying that after you press the button and close the windows, after you show the window again the button remain presses in pressed status? can you show as some screen capture foe us to understand the problem? and what is the result that you are expecting? also witch Os are you using?

  • You'll have to unset focus before hiding. What happens is that you're hiding the button while it still has focus, so the button doesn't have a chance to loose focus. Technically speaking, the fact that you hide it doesn't imply that it should loose focus. So, it's not loosing focus - even if you'd wish it to.
    When you hide the window, try clearing the focus first. Clearing the focus after you're hiding it might not work because the object is not "focusable" when hidden, so the call might just get ignored.

  • Here's a preview of what's happening:


    Here's the code I am using when the window is closing, trying to clear the button's focus (doesn't work, as you can see)

    @void ConfigureWindow::closeEvent(QCloseEvent *event)

  • The QWidget::onClose() event is not triggered when you hide a widget. Instead,the "QWidget::onHide()": event is triggered. Put your code in this event and try again :-)

  • It is triggered. Here's what buttonClose does:

    @connect( buttonClose, SIGNAL( clicked() ), parent, SLOT( close() ) );@

    Just to make sure, I've placed a display message box code to see if it really got executed -> it did.

    As you can see in the code posted below, I am ignoring close event, and instead I call this->hide() from there.

  • The main issue is still the same, the button does not have a chance to loose focus before the window is hidden.
    As a workaround, can you move focus on another object before hiding?

  • I probably could, but I don't quite understand why it doesn't have a chance to lose focus before the window is hidden.


    this kinda gives it the chance, no? Or does it have to be inside onHide event? (the clearFocus)

  • I think the problem there is not focus at all, but actually, the widget painting. Seems like after hiding, closing or minimizing a widget, the paint event just stops drawing.
    consider this simple code:
    @#include <QApplication>
    #include <QWidget>
    #include <QPushButton>

    int main(int argc, char *argv[]){
    QApplication a(argc, argv);

    QPushButton button;
    button.resize(200, 100);;

    QObject::connect(&button, SIGNAL(clicked()), &button, SLOT(showMinimized()));

    return a.exec();
    When you press the button it will minimize the window. As you click on the icon in the taskbar the window is restored and the button looks normal. but if you look at the taskbar preview you can see the window is still painted with the pressed button state (right side of the image).

    I don't have a solution, but I may suggest to force repaint the widget by calling the painter or something....

  • Right, now tell me this isn't Qt's fault that there is no clear way to fix this, or even that it happens at all.

    I've tried to repaint the widget, but it didn't work.

  • you can write a bug about this.. for sure the developers will want to get rid of it in Qt 5.4. Indeed is looking like a bug - or maybe an optimisation for low memory... hm.. who know... but any way some window flags suppose to be to repaint before hiding or on showing. any way for me this is a strange behaviour.

  • I don't think it's literally a bug. When an object is hidden it doesn't make sense to try drawing it because there is nothing to be shown to the user. Altough, the fact that an object is not correctly updated when restoring it, might be a qt issue. You should check this more thoroughly, see if there is something in your code that causes this behavior. I'm sure it would have been noticed by more people if it was a qt issue.

    The pushbutton object you're using, is it a custom class derived from QPushButton? If so, can you share the code so people can check it?

  • It's a QPushButton with CSS style applied.

  • I've fixed it by doing this:

    @void ConfigureWindow::closeEvent(QCloseEvent *event)
    // You have to completely remove the widget, add it again and wait about 10ms, then hide window
    // to clear hover state on the close button nicely.
    m_titlebar->HideTitlebar(); // this removes and deletes all buttons
    m_titlebar->DisplayTitlebar(); // this initializes and adds them again
    QTimer::singleShot(10, this, SLOT(finalizeClose()));

    void ConfigureWindow::finalizeClose()

    This is an awkward, but working solution.

Log in to reply