2016-09-13 107 views
0

我正在學習qt,並試圖從書中獲取示例,我們必須從主窗口應用程序調用預製對話框。Qt顯示從主窗口調用時無框對話框

我已經做了一個對話框,用於輸入一個字符串(我們稱之爲findDialog),輸入的字符串被傳遞給QTableWidget中的自定義槽以搜索輸入的字符串。

只要觸發一個名爲「find」的操作,我就可以調用對話框,但問題是對話框顯示爲無框。

我打電話給這樣的對話框,請指出任何錯誤。

void MainWindow::find() 
{ 
    // Check whether findDialog is already created 
    if(!finddialog) 
    { 
     // create find dialog as a child to main window 
     finddialog = new findDialog(this); 
     // Connecting signals and slots 
    connect(finddialog,SIGNAL(forwardSearch(QString,Qt::CaseSensitivity)), 
     sheetObject,SLOT(forwardSearch(QString,Qt::CaseSensitivity))); 
     connect(finddialog,SIGNAL(backwardSearch(QString,Qt::CaseSensitivity)), 
     sheetObject,SLOT(backwardSearch(QString,Qt::CaseSensitivity))); 
    } 
    // Show dialog 
    finddialog->show(); 

    finddialog->move(200,200); 
    // Raise if minimised 
    finddialog->raise(); 
    // Activate it 
    finddialog->activateWindow(); 
} 

我不能使用exec因爲我沒有將findDialog中的accept()槽連接到任何東西。

我也附上一張照片,顯示對話時它已被調用。請告訴我我已經完成的錯誤以及如何使用框架顯示對話框。 findDialog without frame

回答

0

您必須創建finddialog與適當的Qt::WindowFlags。請參閱文檔:Qt::WindowFlags。例如:

finddialog = new findDialog(this, Qt::Window); 

Indicates that the widget is a window, usually with a window system frame and a title bar, irrespective of whether the widget has a parent or not. Note that it is not possible to unset this flag if the widget does not have a parent.

而且useful example

+0

我編輯的代碼,但它是表示作爲 '錯誤:對呼叫「FindDialog的不匹配的函數:: findDialog(MainWindow *,Qt :: WindowType)' finddialog = new findDialog(this,Qt :: Dialog);' –

+0

即使做了這個---> public: 顯式的findDialog(QWidget * parent = 0, Qt :: WindowType = Qt :: Window); 〜findDialog();' 它仍然沒有顯示框架。 –

+0

如果'FindDialog'繼承了'QDialog',它將自動產生'Qt :: Window'標誌,參見docs [here](https://doc.qt.io/qt-5/qdialog.html#QDialog)和代碼[here](https://code.qt.io/cgit/qt/qtbase.git/tree/src/widgets/dialogs/qdialog.h#n60)。每次你想實例化一個FindDialog時,請不要傳遞這樣的內容。相反,將你想要的行爲設爲**默認的**行爲,並且只是實例化你的FindDialog.you你將不得不通過標誌**當你想要標誌而不是'Qt :: Window'時。 – Mike

1

明白了,

我必須在顯示對話框之前使用setWindowFlags設置窗口標誌。

finddialog->setWindowFlags(Qt::Dialog); 

來顯示它作爲一個對話框

    or 

finddialog->setWindowFlags(Qt::Window); 

到它顯示爲一個窗口

+0

每次你實例化一個FindDialog時,請不要'setWindowFlags'。如果'FindDialog'已經繼承了'QDialog',它將默認具有'Qt :: Window'標誌,參見docs [here](https://doc.qt.io/qt-5/qdialog.html#QDialog)和代碼[here](https://code.qt.io/cgit/qt/qtbase.git/tree/src/widgets/dialogs/qdialog.h#n60)。 – Mike

+1

如果您正在使用「使用Qt 4進行C++ GUI編程」一書中的示例,請按照原樣進行操作。 'FindDialog'繼承'QDialog'(參見第2章創建對話框),你將看到'class FindDialog:public QDialog'。 – Mike

+0

你能解釋一下如果我設置窗口標誌會發生什麼?這只是一個不好的做法或發生了什麼?... 我應該從QDialog繼承它,但我使用QWidget,只是爲了檢查它是如何工作的。 –