繼承至QWidget的類都能使用show()將自身顯示出來,QDialog也不例外。然而QDialog除了能使用繼承至QWidget的show()方法顯示自身之外,它還有另一個方法exec()同樣也能將自身顯示出來。那么這兩個方法之間有什么區別呢?請看下面的表格對比:
原型 | 返回值 | 說明 |
void QWidget::show() | 無 | Shows the widget and its child widgets. |
int QDialog::exec() | 有 | 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. |
從表格對比中可以看出這兩者的區別。首先這兩個方法返回值不同。exec()有返回值,show()沒有返回值。其次這兩個方法的作用也不同。調用show()的作用僅僅是將widget及其上的內容都顯示出來。而調用exec()后,調用線程將會被阻塞,直到Dialog關閉。