I'm using a QTabWidget for some postprocessing. Initially, the content of the tab is a button to open a txt file. Once this button is clicked, the content of the tab changes and a plot is made based on the data inside the txt file. In addition, there is a button to close the plot.
With QStackedWidget, the "openFileWidget" is a CentralWidget, which is replaced by the "newWindow" widget after opening the txt file. If I close the "newWindow" widget, I get back to the (empty) tab.
If I want to have the "openFileWidget" re-appear, what is the best way to do so? As the newWindow is closed, is it still necessary to use takeAt
?
import os, sys
from PySide import QtCore, QtGui
class MainStartTabWindow(QtGui.QWidget):
def __init__(self, parent=None):
super(MainStartTabWindow, self).__init__(parent)
tabWidget = QtGui.QTabWidget()
tabWidget.addTab(tab1Tab(), self.tr("tab 1"))
mainLayout = QtGui.QVBoxLayout()
mainLayout.addWidget(tabWidget)
self.setLayout(mainLayout)
class tab1Tab(QtGui.QMainWindow):
def __init__(self, parent=None):
super(tab1Tab, self).__init__(parent)
self.initTab1Content()
def initTab1Content(self):
self.centralOpenFile = QtGui.QStackedWidget()
self.setCentralWidget(self.centralOpenFile)
widgetOpenFile = openFileWidget(self)
widgetOpenFile.openFileButton.clicked.connect(self.setOpenFile)
widgetOpenFile.openFileButton.clicked.connect(self.FileOpened)
self.centralOpenFile.addWidget(widgetOpenFile)
def FileOpened(self):
FileOpened_widget = newWindow(self.path)
self.centralOpenFile.addWidget(FileOpened_widget)
self.centralOpenFile.setCurrentWidget(FileOpened_widget)
def setOpenFile(self):
options = QtGui.QFileDialog.Options()
fileChoice = "txt (*.txt)"
self.path, filtr = QtGui.QFileDialog.getOpenFileName(self, "Open file", "", fileChoice, "", options)
return self.path
class openFileWidget(QtGui.QWidget):
def __init__(self, parent=None):
super(openFileWidget, self).__init__(parent)
self.openFileButton = QtGui.QPushButton("click to open")
self.openFileButton.setFixedSize(150,150)
layoutNoFileYet = QtGui.QGridLayout()
layoutNoFileYet.addWidget(self.openFileButton, 1, 1)
self.setLayout(layoutNoFileYet)
class newWindow(QtGui.QMainWindow):
def __init__(self, PathToPlanFile, parent=None):
super(newWindow, self).__init__(parent)
self.createFrameUI()
def createFrameUI(self):
self.frameUI = QtGui.QWidget()
self.buttonClose = QtGui.QPushButton("close")
self.buttonClose.clicked.connect(self.close)
self.label = QtGui.QLabel("plot based on openend file")
layoutFileLoaded = QtGui.QGridLayout()
layoutFileLoaded.addWidget(self.buttonClose, 1, 1)
layoutFileLoaded.addWidget(self.label, 2, 1)
self.frameUI.setLayout(layoutFileLoaded)
self.setCentralWidget(self.frameUI)
def main():
app = QtGui.QApplication(sys.argv)
testPlot = MainStartTabWindow()
testPlot.show()
sys.exit(app.exec_())
if __name__ == "__main__":
main()