PDA

View Full Version : How to display a widget on top of other widgets in the same window



Cucumber
1st November 2010, 14:39
Hi there,
I have widget containing a layout. Sometimes I need to display another widget.
And I would like to do it on top of that layout. As well I would like to have a semi-transparent background of top widget.
To make things more clear I give you a photoshopped example:
http://cl.ly/b54feb55d96791ce662c/content
How can I do it with Qt?
Thanks in advance.

tbscope
1st November 2010, 15:53
Don't set a parent.

Cucumber
2nd November 2010, 15:52
I've managed to solve that problem:

MainWidget::MainWidget(QObject* parent)
{
mainLayout = new QVBoxLayout;
mainLayout->addWidget(widget1);
mainLayout->addWidget(widget2);
mainLayout->addWidget(widget3);
setLayout(mainLayout);
}


void MainWidget::addInFront()
{
if (mainLayout)
delete mainLayout;
newWidget = new NewWidget(this);
mainLayout = new QVBoxLayout;
mainLayout->addWidget(newWidget);
setLayout(mainLayout);
}

NewWidget::NewWidget(QObject* parent)
{
setAutoFillBackground(true);
//setAttribute(Qt::WA_OpaquePaintEvent);
QPalette palette = this->palette();
palette.setColor(QPalette::Window, QColor(255,255,255,192));
this->setPalette(palette);

newLayout = new QVBoxLayout;
newLayout->addWidget(widget0);
setLayout(newLayout);
}
That code does everything what I want.
As for placing widgets in front of parent's I've found two useful links:
http://www.qtcentre.org/wiki/index.php?title=Widget_Overlay
http://www.wiki.crossplatform.ru/index.php/Fading_Effects_with_Qt_4.1
But now I have to new questions:
As for Qt::WA_OpaquePaintEvent, am I right that I need that attribute only when re-implement paintEvent and want to draw background by myself?
And the big one for me. When I follow the described tactic it works only when the margin is -20, otherwise there is some space not covered with the background of NewWidget.
http://cl.ly/e9e5e8a2ed82059064b1/content
The widget and it's background are pink. How can I resize it to fill the whole window?