PDA

View Full Version : Setting a Widget inside the MainWindow



De-Wayne
29th August 2017, 22:28
Hello,

I am currently using PyQt5 with Python3.
I'm having trouble getting a QTabWidget to be placed inside the main screen when called from a separate class.
Currently when the button "single" from the view menu option is selected a QTabWidget is produced but in a seperate window from my QMainWindow widget.
I believe the issue lies in my OOP but I was hoping for some input or direction to documentation that could provide me with a solution.

The structure is as follows:

__main__.py


mainscreen.App()

mainwindow.py


class App(QMainWindow):
def __init__(self):
appl = QApplication(sys.argv)

super().__init__() #is this line necessary
self.title = "PyQt EVE-Application"
self.showMaximized()
self.initUI()

sys.exit(appl.exec_())

def menu(self):
mainMenu = self.menuBar()
fileMenu = mainMenu.addMenu('File')
openMenu = mainMenu.addMenu('Open')
viewMenu = mainMenu.addMenu('View')
helpMenu = mainMenu.addMenu('Help')

#FILE
exitButton = QAction('Exit', self)
exitButton.setShortcut('Ctrl + E')
exitButton.setStatusTip('Exit application')
exitButton.triggered.connect(self.close)
fileMenu.addAction(exitButton)

#OPEN

#VIEW
singleview = QAction('Single View', self)
singleview.setShortcut ('Ctrl + S')
singleview.setStatusTip('Change view to one widget.')
singleview.triggered.connect(self.singlecall)
viewMenu.addAction(singleview)


# doubleview
doubleview = QAction('Double View', self)
doubleview.setShortcut('Ctrl + D')
doubleview.setStatusTip('Change view to two wigets.')
doubleview.triggered.connect(widgetviews.double)
viewMenu.addAction(doubleview)


# tripleview
tripleview = QAction('Triple View', self)
tripleview.setShortcut('Ctrl + T')
tripleview.setStatusTip('Change view to three widgets.')
tripleview.triggered.connect(widgetviews.triple)
viewMenu.addAction(tripleview)

# quadview
quadview = QAction('Quad View', self)
quadview.setShortcut('Ctrl + Q')
quadview.setStatusTip('Change view to four widgets.')
quadview.triggered.connect(widgetviews.quad)
viewMenu.addAction(quadview)
#HELP
def singlecall(self):
self.t = widgetviews.single(self)
self.t.show()

def initUI(self):
self.setWindowTitle(self.title)
self.menu()
#default to quad view
self.show()
if __name__ == "__main__":
pass

tabwidget.py


class single(QWidget):
def __init__(self, App):
super().__init__()

App.blankwidget = QWidget()
App.tab1 = QTabWidget()
App.tab1.addTab(App.blankwidget, "Tab 1")
App.tab2 = QTabWidget()
App.tab3 = QTabWidget()
App.tab4 = QTabWidget()

grid = QGridLayout()
grid.addWidget(App.tab1,0,0)
grid.addWidget(App.tab2,0,1)
grid.addWidget(App.tab3,1,0)
grid.addWidget(App.tab4,1,1)
self.setLayout(grid)



Thank you in Advance

high_flyer
29th August 2017, 23:24
Fair disclosure: I am not a Python guy.

QTabWidget is produced but in a seperate window from my QMainWindow widget.
The simple, short answer is that you have to parent your QTabWidget to the MainWindow if you want it to be a child of your MainWindow.
That said, your code is very poorly encapsulated.
From what I can tell from your code, it seems you are allocating a QApplication in your QMainWindow.
Why should a UI class be responsivle for the application object creation?
Can't you have that in your main?

Why should class single be allocating stuff in another class?
That is looking for trouble.
Let the MainWindows create its own children.
You can have single signal the MainWindow when to do it, but leave that task to the MainWindow.