2011-03-03 113 views
1

在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; 
} 

回答

3

如果EditWindowQDialog可以調用exec方法,而不是表演。

從Qt文檔:

INT :: QDialog的EXEC()[時隙]

顯示對話框作爲模態對話框, 阻擋直到用戶關閉它。 函數返回一個DialogCode結果。

如果對話框是應用程序模式,則用戶無法與同一應用程序中的任何其他 窗口交互,直到 他們關閉對話框。如果對話框 是窗口模式,則只有在與 的交互時,父窗口會被阻止,而對話框 處於打開狀態。默認情況下,對話框 是應用程序模式。

這樣,當EditWindow打開時,用戶無法與父窗口進行交互。

4

這是因爲QDialog不是模態,這意味着它不會阻止其他窗口的輸入。

您可以使用setWindowModality()QDialogDescription of Modality in QT)中設置此屬性。本質上,你只是做以下幾點:

setWindowModality(Qt::WindowModal); 
+0

我把它放在'void Window :: popup()',它最終崩潰的程序。我也評論了'window_3 - > show();' – Chris 2011-03-03 00:48:50