【问题标题】:Calling a function in the main window from a widget (PySide)从小部件(PySide)调用主窗口中的函数
【发布时间】:2013-11-12 16:08:10
【问题描述】:

基本上我想要的是:

  1. 在主窗口中显示一个小部件,其中包含一个打开 QFileDialog 的按钮
  2. 选择文件后,包含按钮的小部件应切换到新的小部件,该小部件显示基于文件内容的一些可视化。

在下面的代码示例中,这意味着从showFileSelectionDialog() 方法调用open_file() 方法。

问题是如何做到这一点?我尝试在初始化小部件时将父级作为参数,然后将按钮连接到 self.parent.open_file。但这变得复杂了,我不喜欢小部件被硬编码为主窗口的子窗口。

据我所知,更好的方法是使用Communicate() 来发出事件。但是后来我不知道如何将文件名信息获取到open_file()方法。

#Code greatly inspired by the ZetCode PySide tutorial (http://zetcode.com/gui/pysidetutorial/)

import sys
from PySide import QtGui

class MainApplicationWindow(QtGui.QMainWindow):

    def __init__(self):
        super(MainApplicationWindow, self).__init__()

        self.initUI()

    def initUI(self):

        self.setWindowTitle('<Application title>')
        self.setCentralWidget(FileSelectWidget())
        self.statusBar()

        self.resize(250, 200)
        self.center()
        self.show()

    def center(self):
        qr = self.frameGeometry()
        cp = QtGui.QDesktopWidget().availableGeometry().center()
        qr.moveCenter(cp)
        self.move(qr.topLeft())

    def open_file(self, file_name):
        f = open(file_name, 'r')
        with f:
            data = f.read()
            #TODO: Do something with the data and visualize it!
            print data

class FileSelectWidget(QtGui.QWidget):

    def __init__(self):
        super(FileSelectWidget, self).__init__()

        self.initUI()

    def initUI(self):

        selectLogFilesButton = QtGui.QPushButton('Select log files', self)
        selectLogFilesButton.clicked.connect(self.showFileSelectionDialog)

        hbox = QtGui.QHBoxLayout()
        hbox.addStretch()
        hbox.addWidget(selectLogFilesButton)
        hbox.addStretch()

        vbox = QtGui.QVBoxLayout()
        vbox.addStretch()
        vbox.addLayout(hbox)
        vbox.addStretch()

        self.setLayout(vbox)

    def showFileSelectionDialog(self):

        file_name, _ = QtGui.QFileDialog.getOpenFileName(self, 'Open file', '/home')


def main():

    app = QtGui.QApplication(sys.argv)
    window = MainApplicationWindow()
    sys.exit(app.exec_())


if __name__ == '__main__':
    main()

【问题讨论】:

    标签: python pyside signals-slots qfiledialog


    【解决方案1】:

    FileSelectWidget 类应该定义一个可以连接到open_file 插槽的自定义信号。

    为此,请确保导入 QtCore 模块,然后像这样定义自定义信号:

    class FileSelectWidget(QtGui.QWidget):
        fileSelected = QtCore.Signal(object)
    

    然后在选择文件时发出信号:

        def showFileSelectionDialog(self):
            file_name, _ = QtGui.QFileDialog.getOpenFileName(
                               self, 'Open file', '/home')
            if file_name:
                self.fileSelected.emit(file_name)
    

    最后,将信号连接到插槽:

        widget = FileSelectWidget()
        widget.fileSelected.connect(self.open_file)
        self.setCentralWidget(widget)
    

    【讨论】:

      猜你喜欢
      • 2018-07-10
      • 1970-01-01
      • 1970-01-01
      • 2012-01-04
      • 2020-01-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多