首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何显示QDialog

如何显示QDialog
EN

Stack Overflow用户
提问于 2013-12-18 23:49:50
回答 1查看 2.3K关注 0票数 1

我需要在QWidget中按下QWidget后显示一个查找对话框,其中包含一个QTableView。“查找”对话框将在表的第一列中搜索以找到匹配项。

我可以在使用以下代码按下QMessageBox之后显示Ctrl+F

代码语言:javascript
运行
复制
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,但现在它无法工作。如果你能告诉我哪里做得不对,我将不胜感激:

代码语言:javascript
运行
复制
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()
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-12-19 01:20:18

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

代码语言:javascript
运行
复制
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_()
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20670914

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档