Reputation: 113
I'm fairly new to python and extremely new to PyQt.
I’m trying to embed a QTableWidget in my pyqt program when I click a file browse button. My goal is to have a class in order to make multiple tables, but to simplify things I made the class static so when it’s called it produces a predefined table. Right now I’m just trying to see how to embed the table.
I define the two methods “InitializeUI” and “import” in my class MainWindow. InitializeUI is responsible for the creation and layout of most of my widgets, and I want import to be able to create and embed my table using the layout I created in InitializeUI. How would I go about doing this?
Additionally the code I’ve been running just gives me the error “Process finished with exit code 1” so it has been hard to debug. Here is my current code.
code that connects my browse button to import method
self.BrowseBtn.clicked.connect(self.import1)
code for import method. I have three layouts in my main window, a verticalbox for the left, top right, and bottom right. I want my table to go into the bottom right corner of my window.
def import1(self):
self.Table = CreateTable()
vboxRightBottom.addWidget(self.Table)
class for creating tables
class CreateTable(QTableWidget):
def __init__(self):
self.tableWidget = QTableWidget()
self.tableWidget.setRowCount(4)
self.tableWidget.setColumnCount(2)
self.tableWidget.setItem(0,0, QTableWidgetItem("Cell (1,1)"))
self.tableWidget.setItem(0,1, QTableWidgetItem("Cell (1,2)"))
self.tableWidget.setItem(1,0, QTableWidgetItem("Cell (2,1)"))
self.tableWidget.setItem(1,1, QTableWidgetItem("Cell (2,2)"))
self.tableWidget.setItem(2,0, QTableWidgetItem("Cell (3,1)"))
self.tableWidget.setItem(2,1, QTableWidgetItem("Cell (3,2)"))
self.tableWidget.setItem(3,0, QTableWidgetItem("Cell (4,1)"))
self.tableWidget.setItem(3,1, QTableWidgetItem("Cell (4,2)"))
Intutively I know that I can't call the layout from one method in another method, but I'm not sure how I would embed my Table any other way.
sorry if the code isn't properly indented stack was being finicky when I copy/pasted my code.
If you need further reference here's the link to my full code
https://github.com/Silvuurleaf/Data-Analysis-and-Visualization-GUI/blob/master/Plotter3.1.py
Upvotes: 1
Views: 1011
Reputation: 13337
The vboxRightBottom
looks to be not hold in the class, first you should fix this everywhere it's called (in its declaration in __init__
and inside every methods) :
self.vboxRightBottom = QVBoxLayout()
def import1(self):
self.Table = CreateTable()
self.vboxRightBottom.addWidget(self.Table)
Then the way you handle the class is not handy, you should rather create an object QTableWidget directly as below :
class CreateTable(QTableWidget):
def __init__(self):
super(CreateTable, self).__init__()
self.setRowCount(4)
self.setColumnCount(2)
self.setItem(0,0, QTableWidgetItem("Cell (1,1)"))
self.setItem(0,1, QTableWidgetItem("Cell (1,2)"))
self.setItem(1,0, QTableWidgetItem("Cell (2,1)"))
self.setItem(1,1, QTableWidgetItem("Cell (2,2)"))
self.setItem(2,0, QTableWidgetItem("Cell (3,1)"))
self.setItem(2,1, QTableWidgetItem("Cell (3,2)"))
self.setItem(3,0, QTableWidgetItem("Cell (4,1)"))
self.setItem(3,1, QTableWidgetItem("Cell (4,2)"))
So when you instance the class, it returns the widget, otherwise you should give self.Table.tableWidget
to the addWidget
methods
self.table = CreateTable()
self.vboxRightBottom.addWidget(self.table)
Upvotes: 1