PyQt4:将QtMessageBox.information功能添加到自定义窗口

时间:2010-02-25 15:21:18

标签: python pyqt4

我需要的是一些非常相似的QtMessageBox.information方法,但我需要它来自我的自定义窗口。

我需要一个标签很少的窗口,一个QtTreeViewWidget,一个QButtonGroup ......这个窗口将从主窗口调用。如果我们将实现被调用窗口的类称为SelectionWindow,那么我需要的是:

class MainWindow(QtGui.QMainWindow):
    ...
    def method2(self):
        selWin = SelectionWindow()
        tempSelectionValue = selWin.getSelection()
        # Blocked until return from getSelection
        self.method1(tempSelectionValue)
        ...

class SelectionWindow(QtGui.QMainWindow):
    ...
    def getSelection(self):
        ...
        return selectedRow
    ...

SelectionWindow的方法getSelection应弹出选择窗口,并在QTreeViewWidget中选择最后返回行。我希望主窗口保持阻塞,直到用户在选择窗口中选择一行并通过按钮确认它。我希望你能理解我的需要。

我将不胜感激任何帮助!

谢谢, Tiho

1 个答案:

答案 0 :(得分:0)

我会做这样的事情:

  • 带按钮框的对话框窗口 - > 连接到accept()和的事件 拒绝()对话框本身的插槽
  • 将对话框模态设置为类似应用程序模式
  • 调用对话框的exec_()方法,使其保持阻塞,直到用户选择确定/取消
  • 执行exec_()方法终止后,您可以从对话框小部件中读取所需内容。

这样的事情应该符合你的需要:

class SelectionWindow(QtGui.QMainWindow):
    ...
    def getSelection(self):
        result = self.exec_()
        if result:
            # User clicked Ok - read currentRow
            selectedRow = self.ui.myQtTreeViewWidget.currentIndex()
        else:
            # User clicked Cancel
            selectedRow = None
        return selectedRow
    ...