关于qt:如何显示QDialog

How to show a QDialog

在包含QTableViewQWidget中按Ctrl+F后,我需要显示一个查找对话框。查找对话框将在表的第一列中搜索以找到匹配项。

在按下Ctrl+F并显示以下代码后,我可以显示QMessageBox:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
class Widget(QWidget):
    def __init__(self,md,parent=None):
        QWidget.__init__(self,parent)
        layout=QVBoxLayout(self)

        # initially construct the visible table
        tv = QTableView()
        # uncomment this if the last column shall cover the rest
        tv.horizontalHeader().setStretchLastSection(True)
        tv.show()

        # set black grid lines
        self.setStyleSheet("gridline-color: rgb(39, 42, 49)")

        # construct the Qt model belonging to the visible table
        model = NvmQtModel(md)
        tv.setModel(model)
        tv.resizeRowsToContents()
        tv.resizeColumnsToContents()

        # set the shortcut ctrl+F for find in menu
        shortcut = QShortcut(QKeySequence('Ctrl+f'), self)
        shortcut.activated.connect(self.handleFind)

        # delegate for decimal
        delegate = NvmDelegate()
        tv.setItemDelegate(delegate)
        self.setGeometry(200,200,600,600) # adjust this later
        layout.addWidget(tv)

        # set window title
        self.setWindowTitle("TITLE")

        # find function: search in the first column of the table
        def handleFind(self):
            reply = QMessageBox.question(
                self, 'Find', 'Find Dialog',
                QMessageBox.Yes | QMessageBox.No)
            if reply == QMessageBox.Yes:
                print('Yes')
            else:
                print('No')

然后我将QMessageBox更改为QDialog,但是现在不起作用。如果您能告诉我我在哪里做不正确,我将不胜感激:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
class Widget(QWidget):
    def __init__(self,md,parent=None):
        QWidget.__init__(self,parent)
        layout=QVBoxLayout(self)

        # initially construct the visible table
        tv = QTableView()
        # uncomment this if the last column shall cover the rest
        tv.horizontalHeader().setStretchLastSection(True)
        tv.show()

        # set black grid lines
        self.setStyleSheet("gridline-color: rgb(39, 42, 49)")

        # construct the Qt model belonging to the visible table
        model = NvmQtModel(md)
        tv.setModel(model)
        tv.resizeRowsToContents()
        tv.resizeColumnsToContents()

        # set the shortcut ctrl+F for find in menu
        shortcut = QShortcut(QKeySequence('Ctrl+f'), self)
        shortcut.activated.connect(self.handleFind)

        # delegate for decimal
        delegate = NvmDelegate()
        tv.setItemDelegate(delegate)
        self.setGeometry(200,200,600,600) # adjust this later
        layout.addWidget(tv)

        # set window title
        self.setWindowTitle("TITLE")

    # find function: search in the first column of the table
    def handleFind(self):
        findDialog = QDialog()
        findLabel = QLabel("Find what", findDialog)
        findField = QLineEdit(findDialog)
        findButton = QPushButton("Find", findDialog)
        closeButton = QPushButton("Close", findDialog)
        findDialog.show()

如果希望对话框为模式对话框,请调用findDialog.exec_()

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
from PyQt4.QtGui import *

def handleFind():
    findDialog = QDialog()
    #findDialog.setModal(True)
    findLabel = QLabel("Find what", findDialog)
    findField = QLineEdit(findDialog)
    findButton = QPushButton("Find", findDialog)
    closeButton = QPushButton("Close", findDialog)
    #findDialog.show()
    findDialog.exec_()


app = QApplication([])

b = QPushButton("click me")    
b.clicked.connect(handleFind)
b.show()

app.exec_()