Solved How to create a resizable table widget?
-
I want to create a resizable table widget, and this table should pop-up after clicking a button. How can it be done using python code? Or I have to do it using Qt designer only?
-
@Prathamesh
I don't know what you're doing. This says nothing about which approach you are taking to process your.ui
file --- running auic
to generate a class or loading it at runtime. I thought you said you had designed theQTableWidget
in the Designer? Yet you create it in code. Plus you create two of them, one in__init__()
and another one inadd_table_widget()
. And do not callQMessageBox.exec_()
insideQMessageBox.__init__()
, callexec_()
from the outside world when you actually want to display the message box. And do not do resizing unconditionally inevent()
, that's far too frequent. And I don't see that the table widget is put on any kind of layout, which should be needed for resizing.I leave it to someone else, because there is a lot to do here.
-
@Prathamesh Simply put the table into a layout, then it will resize if widgets is resized...
-
This post is deleted! -
@jsulm I have created a layout and a put a tree widget inside it (I have been asked to do so). Then I have written a code for adding data to the table as follows -->
def add_table_widget():
"""
Method to create a table having short cuts information
"""
table_widget = QtWidgets.QTableWidget()
table_widget.setGeometry(QtCore.QRect(0, 0, 300, 200))
row_count = (len(SHORT_CUTS))
column_count = (len(SHORT_CUTS[0]))
table_widget.setColumnCount(column_count)
table_widget.setRowCount(row_count)
table_widget.setHorizontalHeaderLabels((list(SHORT_CUTS[0].keys())))
for row in range(row_count):
for column in range(column_count):
item = (list(SHORT_CUTS[row].values())[column])
table_widget.setItem(row, column, QtWidgets.QTableWidgetItem(item))Here SHORT_CUTS is a dictionary which I have created which is containg data to be filled in table.
Now how do I call or connect my newly created ui file in my function?And please point out if there is any mistake in a code as I am new to qt5. Thank you
-
@Prathamesh said in How to create a resizable table widget?:
Now how do I call or connect my newly created ui file in my function?
What ui file and what function?
-
@Prathamesh
As @jsulm has just said. At a guess about your situation:-
Either you are using a UI file from Designer, in which case you don't want to create your own
table_widget = QtWidgets.QTableWidget()
, you want to use and fill the one created from the UI. -
Or you are not using a Designer one, in which case you need put the one you create in your own code somewhere onto whatever widget you are displaying.
-
-
@jsulm In this ui design file I have created a layout and put a blank tree widget inside it? Now I want to fill that table with data using python code ,so I have written a function as above.
-
@Prathamesh
So did you read what I answered? You are in the first of the two bullet point situations. -
@JonB Yeah so I don't have to use QTableWidget here then. Then what can be my code? I have created a layout in designer ,and I will be filling data from python code. This is what I understood.
-
@Prathamesh
You want to access aQTableWidget
here. But you don't want to create your own viatable_widget = QtWidgets.QTableWidget()
, you want to access the one which you have already put into your UI.We don't know the code for that, because we don't know which approach you are taking to using or loading that UI file. Don't you already access any other widgets in the UI elsewhere in your code so you know how to get at the table widget? Or you must tell us how your code loads/uses the UI file you created: you don't say whether you are using PyQt or PySide, which is relevant, e.g. see the various approaches possible in, say, https://www.riverbankcomputing.com/static/Docs/PyQt5/designer.html.
-
@JonB I am using PyQt. I have 2 approaches for my table widget. One is directly writing a code .
And the other one is creating layout in designer.I have created a message-box and put a table inside it using following code -->
class ShortCutsInfo(QtWidgets.QMessageBox):
"""
Class for displaying a pop up for showing information about short cuts
"""def __init__(self): QtWidgets.QMessageBox.__init__(self) self.table_widget = QtWidgets.QTableWidget() self.setWindowTitle('Short Cuts') self.setStandardButtons(QtWidgets.QMessageBox.Ok) self.setWindowIcon(QtGui.QIcon(os.path.abspath(os.path.join(os.path.dirname( os.path.realpath(__file__)), 'gui_images', 'logo_icon.png')))) self.add_table_widget(self) self.exec_() def add_table_widget(self, parent_item): """ Method to create a table having short cuts information :param parent_item: used for adding table widget to a QMessageBox """ self.table_widget = QtWidgets.QTableWidget(parent_item) self.table_widget.setGeometry(QtCore.QRect(0, 0, 300, 200)) row_count = (len(SHORT_CUTS)) column_count = (len(SHORT_CUTS[0])) self.table_widget.setColumnCount(column_count) self.table_widget.setRowCount(row_count) self.table_widget.setHorizontalHeaderLabels((list(SHORT_CUTS[0].keys()))) for row in range(row_count): for column in range(column_count): item = (list(SHORT_CUTS[row].values())[column]) self.table_widget.setItem(row, column, QtWidgets.QTableWidgetItem(item)) def event(self, e): """ This virtual function receives events to an object and should return true if the event e was recognized and processed. """ result = QtWidgets.QMessageBox.event(self, e) self.setMinimumWidth(50) self.setMaximumWidth(380) self.setMinimumHeight(50) self.setMaximumHeight(250) self.setSizePolicy( QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding ) self.resize(550, 300) return result
This way I can see a table after clicking a button. But by this approch, the message-box is not getting resized manually by dragging it. So in future if we need to add any new entries in table we have to resize message-box in code. So is there any way to resize a message-box in code itself so that the table present inside it will also get adapted according to the size of message-box?
-
@Prathamesh
I don't know what you're doing. This says nothing about which approach you are taking to process your.ui
file --- running auic
to generate a class or loading it at runtime. I thought you said you had designed theQTableWidget
in the Designer? Yet you create it in code. Plus you create two of them, one in__init__()
and another one inadd_table_widget()
. And do not callQMessageBox.exec_()
insideQMessageBox.__init__()
, callexec_()
from the outside world when you actually want to display the message box. And do not do resizing unconditionally inevent()
, that's far too frequent. And I don't see that the table widget is put on any kind of layout, which should be needed for resizing.I leave it to someone else, because there is a lot to do here.
-
This post is deleted!