开发者

How to make a popped-up window a top-level window in QT?

In QT, when a button is clicked and a window pops up, the user can still go back and click the same button (infinite times). How can I make it so that the window that pops up from the button stays on top of the other windows? In this case, it is the "Edit" button that pops up a window.

Here is window.cpp

#include "window.h"
#include "editwindow.h"
#include "ui_window.h"
#include <QtGui/QApplication>
#include <QtGui>

Window::Window(QWidget *parent) :
QDialog(parent),
ui(new Ui::Window)
{
ui->setupUi(this);

ShowEdit = new QPushButton(tr("Edit"));
ShowEdit -> show();
connect(ShowEdit, SIGNAL(clicked()), this, SLOT(popup()));

Remove = new QPushButton(tr("Remove"));
Remove -> show();
connect(Remove, SIGNAL(clicked()), this, SLOT(ProgramRemove()));

OK = new QPushButton(tr("OK"));
OK -> show();
connect(OK, SIGNAL(clicked()), this, SLOT(Saved()));

Quit = new QPushButton(tr("Quit"));
Quit -> show();开发者_StackOverflow中文版
connect(Quit, SIGNAL(clicked()), this, SLOT(close()));

QLabel *tableLabel = new QLabel(tr("All Programs"));

QVBoxLayout *buttonLayout2 = new QVBoxLayout;
buttonLayout2 -> addWidget(ShowEdit);
buttonLayout2 -> addWidget(Remove);
//buttonLayout2 -> addStretch();

QHBoxLayout *buttonLayout2_2 = new QHBoxLayout;
buttonLayout2_2 -> addWidget(Quit);
buttonLayout2_2 -> addWidget(OK);

/*******************************************************************************/
/***************************Below is for Table**********************************/
/*******************************************************************************/

PTable = new QTableWidget(10, 10);

//PTable ->setHorizontalHeader(tr("Program Names"));
//inputs->setText(QString::number(row));
//PTable->setItem(row, column, inputs);

QHBoxLayout *PTableLayout = new QHBoxLayout;
PTableLayout ->addWidget(PTable);

/*------------------------------------------------------------------------------*/
/*------------------------construct window--------------------------------------*/
/*------------------------------------------------------------------------------*/

QGridLayout *SecondLayout = new QGridLayout;
SecondLayout -> addWidget(tableLabel, 0, 0);
SecondLayout -> addLayout(PTableLayout, 1, 0);
SecondLayout -> addLayout(buttonLayout2, 0, 1);
SecondLayout -> addLayout(buttonLayout2_2, 2, 0);
setLayout(SecondLayout);
setWindowTitle(tr("Settings"));
}

Window::~Window()
{
delete ui;
}

void Window :: popup()
{
EditWindow* window_3 = new EditWindow(this);
window_3->move(QPoint(550, 100));
window_3->show();
window_3->raise();
}

void Window :: closeBoth()
{
return;
}

void Window :: Saved()
{

return;
}

void Window :: ProgramRemove()
{

return;
} 


This is because the QDialog is not modal which means that it does not block the input to other windows.

You can set this property by using setWindowModality() in QDialog (Description of Modality in QT). Essentially you just do the following:

setWindowModality(Qt::WindowModal);


If EditWindow is a QDialog you can call the exec method instead of show.

From the Qt documentation:

int QDialog::exec () [slot]

Shows the dialog as a modal dialog, blocking until the user closes it. The function returns a DialogCode result.

If the dialog is application modal, users cannot interact with any other window in the same application until they close the dialog. If the dialog is window modal, only interaction with the parent window is blocked while the dialog is open. By default, the dialog is application modal.

This way the user can't interact with the parent window while EditWindow is open.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜