【问题标题】:How to show a QDialog如何显示 QDialog
【发布时间】:2013-12-18 23:49:50
【问题描述】:

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

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

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,但现在它不起作用。如果您能告诉我哪里做得不对,我将不胜感激:

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()

【问题讨论】:

    标签: qt pyqt qtableview qtgui qdialog


    【解决方案1】:

    如果您希望对话框成为模态对话框,请致电findDialog.exec_()

    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_()
    

    【讨论】:

    • 非常感谢..它现在显示了!但是如何调整一切的位置呢?他们看起来不太好..还发现按钮被关闭按钮覆盖!
    • 你应该使用布局对象来布局小部件。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多