PDA

View Full Version : show() on parent widget doesn't show the child widgets



sraju
16th October 2013, 18:01
Hi All,

I have an application where in which I have a main widget which acts as parent for all the child widgets that are created on the fly.
I have been trying to control the window managment in my application such that child widgets are inserted in the order what I expect.


QWidget *pMainWid = new QWidget(NULL); // Independent Widget

QWidget* getQtWidget(QWidget *p_Parent)
{
// MyWidget is the customized widget from the QWidget
MyWidget *pchildWidget = new MyWidget(p_Parent);
// customize the widget as required
// .....
return pChildWidget;
}

void insert(int position)
{
QList<MyWidget *> winList = findDirectChild<MyWidget *>(pMain);
MyWidget *underWhichWin = NULL;

QWidget *pChildWidget = getQtWidget(m_pQtParentWidget);

if(position > 0) {
underWhichWin = winList.at(position - 1);
pChildWidget->stackUnder(underWhichWin);
}
//pChildWidget->setVisible(true); // <Why do we need to set Visibility of the Child Widget to True?>

pMain->show();

// Update the state of all windows
recomputeWidgetState();
}

void recomputeWidgetState()
{
WIDGET_STATE newState;

QList<MyWidget *> winList = findDirectChild<MyWidget *>(pMain);

for(int current = 0; current < winList.count(); ++current)
{
MyWidget *wdgt = winList.at(current);
std::cout << "\tVisible? " << wdgt->isVisible() << std::endl;
QRegion wdgtVisRegn = wdgt->visibleRegion();
QRect wdgtVisRegnBundRect = wdgtVisRegn.boundingRect();
QRect wdgtRect = wdgt->rect();

if(wdgtVisRegn.isEmpty()) {
newState = WIDGET_OBSCURED;
} else {
if( (wdgtVisRegnBundRect == wdgtRect) && (1 == wdgtVisRegn.rects().count()) ) {
newState = WIDGET_VISIBLE;
} else {
newState = WIDGET_PARTIAL;
}
}
wdgt->setCurState(newState);
} // end of for loop
}


Note: I am doing all insertion operation after the event loop starts (inside the Qt Event Loop) [ It is the project requirement]

Problem: During recomputing the Widget State I am observing folowing things:
a. If "pChildWidget->setVisible(true);" is enabled I will get the proper z-order what I expected and all child widgets are shown
b. If I exclude the "pChildWidget->setVisible(true);" statement Child widgets are not show and I will not be getting the proper z-order what I expect.

Query: As per my knowledge if we say show() on parnet widget it shows all its children. But in the above case, if we set the visibility of the child to true explicitly
the application displays the child widget otherwise not. What is wrong in my impmentation? Why do we need to set the visibility explicitly?

Please clarify.

Thanking You
SRaju

anda_skoa
16th October 2013, 18:33
You are saying that all insertions happen after the event loop has started. So the main window has already processed its show event.
Or are you creating a hidden window and calling show() when you have done all insertions?

Btw, it is very uncommon to handle placement and z-order of widgets manually. What do you want to achieve?

Cheers,
_

sraju
16th October 2013, 19:17
Thanks for the response.

We are not explicitly creating hidden windows. Just normal Widget Creation. Are these widgets hidden if we create using new operator ?

We have an old application and they window management has been written using list in C++. I am trying to port that application to Qt such that Qt can take over the window management. So, the order in which the windows are managed in old code should be done in Qt as well.

Is there a way to inform the Qt to process the show() event once after the event loop started.? Or Calling show() does is sufficient?

Please advice

Regards
SRaju

anda_skoa
17th October 2013, 12:09
We are not explicitly creating hidden windows. Just normal Widget Creation. Are these widgets hidden if we create using new operator ?

I meant the main window. Do you call show() somewhere in main() before starting the event loop or are you calling show() after creating the children?

By default all widgets are not yet visible and need to be shown. Children are shown with their parent unless they have been explicitly hidden.

So it depends if your main window has already been shown at the time of child creation. If it has, then the children will remain hidden (their parent didn't get shown after their creation).



We have an old application and they window management has been written using list in C++. I am trying to port that application to Qt such that Qt can take over the window management. So, the order in which the windows are managed in old code should be done in Qt as well.

Is this an MDI application? Are you using QMdiArea?



Is there a way to inform the Qt to process the show() event once after the event loop started.? Or Calling show() does is sufficient?


Calling show() is sufficient, it results in an event being sent to the respective widget, so it needs a running event loop to get processed.

Cheers,
_

sraju
18th October 2013, 17:15
I meant the main window. Do you call show() somewhere in main() before starting the event loop or are you calling show() after creating the children?

No. I will not be calling the show() I will call the show() on Main Widget After creating the child widgets and it is after the event loop starts.


By default all widgets are not yet visible and need to be shown. Children are shown with their parent unless they have been explicitly hidden.

So it depends if your main window has already been shown at the time of child creation. If it has, then the children will remain hidden (their parent didn't get shown after their creation).
It has answered my query and concludes that each child widgets needs to be shown when they are added after the event loop starts.


Is this an MDI application? Are you using QMdiArea?

No, it's not designed using QMdiArea. We are considering each QWidget as a Window and it is serving our purpose.

Thanks for the support.


Regards
SRaju