Issue
Basicly what I want is:
- Show a widget in the main window containing a button that opens a QFileDialog
- When a file is selected the widget containing the button should be switched to a new widget that shows some visualizations which are based on the file content.
In the code example below, this means invoking the open_file()
method from the showFileSelectionDialog()
method.
The problem is how to do this? I tried taking the parent as argument when initializing the widget and then connecting the button to self.parent.open_file. But this gets complicated and I do not like that the widget is hardcoded to be a child of the main window.
As fair as I can understand, a better approach would be to use Communicate()
to emit an event. But then I do not know how to get the file name information to the open_file()
method.
#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()
Solution
The FileSelectWidget
class should define a custom signal that can be connected to the open_file
slot.
In order to do that, make sure the QtCore module is imported, and then define the custom signal like this:
class FileSelectWidget(QtGui.QWidget):
fileSelected = QtCore.Signal(object)
Then emit the signal whenever a file is selected:
def showFileSelectionDialog(self):
file_name, _ = QtGui.QFileDialog.getOpenFileName(
self, 'Open file', '/home')
if file_name:
self.fileSelected.emit(file_name)
And finally, connect the signal to the slot:
widget = FileSelectWidget()
widget.fileSelected.connect(self.open_file)
self.setCentralWidget(widget)
Answered By - ekhumoro
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.