The Cedar Prince
The Cedar Prince

Reputation: 21

How to Embed a Table in PyQt5

I have been writing a GUI in Python 3 using PyQt5.

I am having trouble with trying to embed a table in PyQt's QMainWindow Class.

In the code below, I have two classes - one controlling the window (with object type QMainWindow) and another (with object type QWidget) controlling the table. I know the code is generating my main window and table as they pop up in two separate windows. What I am trying to do is embed the table in the main window with the button.

Like in the image below:

Floating Window Issue

What am I forgetting to do or not including? Thank you for the help.

Here is the code:

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

class UserInterface(QMainWindow):

    def __init__(self):
        super(UserInterface, self).__init__()
        self.TaskBar()
        self.Buttons()
        self.tabler = MyTable()

    def TaskBar(self):
        self.setWindowTitle("MyWindow")
        self.setGeometry(QDesktopWidget().screenGeometry())

    def Buttons(self):
        close_button = QPushButton("Close", self)
        close_button.resize(100, 50)
        close_button.move(100, 900)
        close_button.setStyleSheet("QPushButton {font-size : 24px}")
        close_button.clicked.connect(sys.exit)

class MyTable(QWidget):

    def __init__(self):
        super(MyTable, self).__init__()
        self.Table()

    def Table(self):
        self.mytable()
        self.layout = QVBoxLayout()
        self.layout.addWidget(self.tableWidget)
        self.setLayout(self.layout)
        self.show()

    def mytable(self):
        self.tableWidget = QTableWidget()
        self.tableWidget.setRowCount(1)
        self.tableWidget.setColumnCount(1)
        self.tableWidget.setItem(0, 0 , QTableWidgetItem("Hello"))
        self.tableWidget.move(300, 300)


def Main():
    qapplication_constructor = QApplication(sys.argv)
    gui = UserInterface()
    gui.show()
    sys.exit(qapplication_constructor.exec_())

if __name__ == "__main__":
    Main()

Upvotes: 1

Views: 3951

Answers (1)

MalloyDelacroix
MalloyDelacroix

Reputation: 2293

Every QMainWindow must have a central widget. You can set another widget as the central widget of the QMainWindow class. You will however need to arrange the button differently so that it can be added to the correct layout. Something like this:

class UserInterface(QMainWindow):

    def __init__(self):
        super(UserInterface, self).__init__()
        self.TaskBar()
        self.Buttons()

        self.table_widget = MyTable()

        close_button = QPushButton("Close", self)
        close_button.resize(100, 50)
        close_button.move(100, 900)
        close_button.setStyleSheet("QPushButton {font-size : 24px}")
        close_button.clicked.connect(sys.exit)

        self.widget = QWidget(self)
        layout = QGridLayout()
        self.widget.setLayout(layout)
        layout.addWidget(self.table_widget)
        layout.addWidget(close_button)

        self.setCentralWidget(self.widget)

This can be arranged differently depending on how you want to set the grid layout. If the grid layout does not suite your needs, there are several available.

Upvotes: 1

Related Questions