[英]Make Qt top-level window “owned” by other non-Qt window within a single process
[英]How to make a popped-up window a top-level window in QT?
在QT中,当单击一个按钮并弹出一个窗口时,用户仍然可以返回并单击相同的按钮(无限次)。 如何使从按钮弹出的窗口停留在其他窗口的顶部? 在这种情况下,弹出窗口的是“编辑”按钮。
这是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();
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;
}
这是因为QDialog
不是模态的,这意味着它不会阻止其他窗口的输入。
您可以通过使用QDialog
setWindowModality()
来设置此属性setWindowModality()
QT中的模式说明 )。 本质上,您只需执行以下操作:
setWindowModality(Qt::WindowModal);
如果EditWindow
是QDialog
,则可以调用exec方法而不是show。
从Qt文档中:
int QDialog :: exec()[插槽]
将对话框显示为模态对话框,直到用户将其关闭为止一直处于阻塞状态。 该函数返回DialogCode结果。
如果对话框是应用程序模式的,则用户在关闭对话框之前无法与同一应用程序中的任何其他窗口进行交互。 如果对话框是窗口模式的,则在打开对话框时仅阻止与父窗口的交互。 默认情况下,对话框为应用程序模式。
这样,当EditWindow打开时,用户无法与父窗口进行交互。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.