![](/img/trans.png)
[英]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.