Mady
Mady

Reputation: 473

QtabWidget and QMainWindow in one class

Is there a way to define the menubar in a QtabWidget class ?

I wrote a code for a destopapplication with pyqt5 and python 3.6. I would like to set the manuBar in the same class as the Tabs but my code returns qtabwidget has no attribute QMainWindow.

Here is my code:

import sys
from PyQt5 import QtWidgets, QtCore, QtPrintSupport, QtGui
from PyQt5.QtWidgets import *

class main_window(QTabWidget):
    def __init__(self, parent=None):
        super(QTabWidget, self).__init__(parent)
        self.setGeometry(50, 50, 1100, 750)
        self.setWindowTitle("Programm")  #

        self.centralWidget = QtWidgets.QWidget()
        self.tabWidget = QtWidgets.QTabWidget(self.centralWidget)
        self.tabWidget.setGeometry(QtCore.QRect(10, 10, 1200, 1000))

        open_new_file = QAction('New', self)

        mainMenu = self.menuBar()
        fileMenu = mainMenu.addMenu('Projekt')


        fileMenu.addAction(open_new_file)
        self.table_widget = MyTableWidget(self)
        self.setCentralWidget(self.table_widget)

        self.show()

        self.tab_v1 = QtWidgets.QWidget()
        self.addTab(self.tab_v1, "Tab 1")

        self.tab_v2 = QtWidgets.QWidget()
        self.addTab(self.tab_v2, "Tab 2")

        self.openFile = QPushButton("Choose Tab ", self.tab_v1)
        self.openFile.setGeometry(QtCore.QRect(700, 25, 200, 30))


def main():
    app = QApplication(sys.argv)
    ex = main_window()
    ex.show()
    sys.exit(app.exec_())


if __name__ == '__main__':
    main()

Upvotes: 4

Views: 1643

Answers (1)

eyllanesc
eyllanesc

Reputation: 244282

QTabWidget can not have a QMenuBar, what you have to do is put the centralwidget of a QMainWindow to the QTabWidget.

import sys
from PyQt5 import QtCore, QtWidgets

class Main_window(QtWidgets.QMainWindow):
    def __init__(self, parent=None):
        super(Main_window, self).__init__(parent)
        self.setGeometry(50, 50, 1100, 750)
        self.setWindowTitle("Programm")  

        open_new_file = QtWidgets.QAction('New', self)
        mainMenu = self.menuBar()
        fileMenu = mainMenu.addMenu('Projekt')
        fileMenu.addAction(open_new_file)

        self.tabWidget = QtWidgets.QTabWidget()
        self.setCentralWidget(self.tabWidget)

        self.tab_v1 = QtWidgets.QWidget()
        self.tabWidget.addTab(self.tab_v1, "Tab 1")
        self.openFile =QtWidgets.QPushButton("Choose Tab ", self.tab_v1)
        self.openFile.setGeometry(QtCore.QRect(700, 25, 200, 30))

        self.tab_v2 = QtWidgets.QWidget()
        self.tabWidget.addTab(self.tab_v2, "Tab 2")

def main():
    app = QtWidgets.QApplication(sys.argv)
    ex = Main_window()
    ex.show()
    sys.exit(app.exec_())

if __name__ == '__main__':
    main()

enter image description here

Upvotes: 5

Related Questions