PDA

View Full Version : show widget over other widgets



maston
22nd June 2010, 11:32
Hi.

how can I show one widget over others in MainWindow class?

i have vertical layout with three lists. When I click item i would like to show() widget with some info and button "Close". When i click it widget is Hide();

I write addWidget() but it's change layout and all moving up.

Thanks.

Zlatomir
22nd June 2010, 11:38
You need to create QDialog (http://doc.qt.nokia.com/4.6/qdialog.html) and instead of show(); you need exec(); (witch will make the dialog modal)

maston
22nd June 2010, 22:44
Thank You :) it is ok but....

When I write :



QDialog *ms;

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
....

ms = new QDialog();
ms->setWindowOpacity(0.5);
ms->setWindowFlags(Qt::Popup);
ms->exec(); // or show() it's no matter


in my function, I got error -1073741819 while build

also for code :



QDialog ms;

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
....

ms.setWindowOpacity(0.5);
ms.setWindowFlags(Qt::Popup);
ms.exec(); // or show() it's no matter


it's runtime C++ error while run application.

I am newbie and i don't know how to do it, to show dialog when i click button :(

edit:
I need to have global variable ms :)
when i write QDialog ms; inside function it's ok. but it's not satisfied for me.

Zlatomir
23rd June 2010, 09:31
Try this: from Qt Creator, create an Empty Project, add a C++ source file to the project, and write this code:


#include <QApplication>
#include <QtGui>

int main(int argc, char** argv){
QApplication ap(argc, argv);

QWidget *w = new QWidget(0);
QVBoxLayout *l = new QVBoxLayout(w);
QPushButton *b1 = new QPushButton("Open Dialog");
QPushButton *b2 = new QPushButton("Close");
l->addWidget(b1);
l->addWidget(b2);

QDialog *dlg = new QDialog(w);
QPushButton *b3 = new QPushButton("Close", dlg);

QObject::connect(b1, SIGNAL(clicked()), dlg, SLOT(exec())); //here you can "play" with the show or exec to see the difference
QObject::connect(b2, SIGNAL(clicked()), &ap, SLOT(quit()));
QObject::connect(b3, SIGNAL(clicked()), dlg, SLOT(close()));

w->show();

return = ap.exec();
}


And to not be "off-topic" in your little code that you posted there are two problems
first: ms = new QDialog(this); (to give the dialog a parent) and make sure in the mainwindow.h you have declared the QDialog *ms;
second: do not call ms.show() (or exec) in the mainwindow constructor, here you will need to connect the clicked signal from the button to the show (or exec) slot of the dialog (read more on signals and slots if you didn't understood them enough)