Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Qt: Update widgets that are hidden before they are then shown?

I have two QDialogs stacked on top of each other, with some data is shared between them shown in a QWidget in each dialog's layout (a widget showing a count graphically).

In the QDialog currently shown, the user can change that data. This change gets propagated to both widgets, and the correct count is displayed in the current QDialog. However, when the user closes that QDialog, and the one underneath becomes visible, the QWidget showing the count shows the old value for a split second before it then updates to the correct value.

I know that the QWidget underneath has the correct value before the other QDialog is shown, but I can't seem to get the Dialog to update before it's visible. I tried this while it was still hidden:

hiddenDialog->layout()->activate()

hiddenDialog->layout()->update()

But it still fails to actually update until after dialog is shown. My problem seems sort-of related to Qt: How to force a hidden widget to calculate its layout?, except I want to then show the QDialog after setting Qt::WA_DontShowOnScreen on. After turning on Qt::WA_DontShowOnScreen and updating the dialog, I tried setting:

dialog->setAttribute(Qt::WA_DontShowOnScreen, false);

dialog->show()

But the Dialog remains not shown on screen. Is there any way to make it visible again? Or is there some way to have the dialog and widgets update when they are hidden by the other dialog?


Here's some more details as requested: There are two classes, Dialog1 and Dialog2, each extending the same base class, BaseDialog, which extends the QDialog class.

BaseDialog has a slot that gets called whenever the GlobalCount is changed. This slot updates a countWidget which draws a picture of the count.

void BaseDialog::updateCountWidget()
{
    _countWidget->updateCount(globalCount);
    _countWidget->update();
}

Dialog1 has a slot (connected from a QPushButton click()) that creates and shows a Dialog2:

void Dialog1::showDialog2()
{
    Dialog2* dialogTwo = new Dialog2();
    dialog2->show();
}    

Dialog2 has two slots (connected to QPushButton signals), one subtract from the global count, the other closes the dialog. When the global count is subtracted, it emits a signal connected to the updateCountWidget() slot of both the Dialog1 and Dialog2.

void Dialog2::subtractCount()
{
    GlobalCount.subtractOne();
}

void Dialog2::userClosed()
{
    accept();
}

When I click the subtract button, I can see the _countWidget of Dialog2 update correctly. However, when I close Dialog2, and can once again see Dialog1, the _countWidget of Dialog1 shows the original count value for a split-second, then updates to the new value.

My goal is to prevent this momentary update, as it looks really funky to the user. This has gotten obscenely long, but I appreciate any insights!

like image 939
Danny Avatar asked Jan 06 '12 18:01

Danny


People also ask

How do I hide widgets in Qt?

Just add all your widgets into the layout and use QWidget::hide() , QWidget::show() when needed. For more complex situations you can use The State Machine Framework.

What are qt5 widgets?

Widgets are the primary elements for creating user interfaces in Qt. Widgets can display data and status information, receive user input, and provide a container for other widgets that should be grouped together. A widget that is not embedded in a parent widget is called a window.

What is QWidget * parent?

The QWidget *parent means which widget this object will be a child of. The child is drawn in that particular widget/window. For example, you can have a QWindow object, and a QCanvas widget. The canvas can be a child of that window, and it will be drawn in that window.

What is QWidget?

The QWidget class is the base class of all user interface objects. The widget is the atom of the user interface: it receives mouse, keyboard and other events from the window system, and paints a representation of itself on the screen. Every widget is rectangular, and they are sorted in a Z-order.


1 Answers

call

hide() 

on widget when you do

setAttribute(Qt::WA_DontShowOnScreen, true);

After that

setAttribute(Qt::WA_DontShowOnScreen, false);
show()

and it will work

I use Qt embedded on Linux

like image 144
Midnatten Avatar answered Sep 18 '22 13:09

Midnatten