I have a table which is being populated by the Database (MySQL) at the backend.
I am trying to resize the Columns according to the contents of each column (as much the stretch is possible), however I can't seem to do it.
I looked it up on Stackoverflow and found that I can do it by setting the QScrollAbstractArea.sizeAdjustPolicy to AdjustToContents in the Qt Designer.
I did this but it still did not work.
I further looked it up and found different solutions. One of them was this as well but it also did not work for me,
self.ui.course_table.horizontalHeader().setSectionResizeMode(0, QtWidgets.QHeaderView.Stretch)
self.ui.course_table.horizontalHeader().setSectionResizeMode(1, QtWidgets.QHeaderView.ResizeToContents)
self.ui.course_table.horizontalHeader().setSectionResizeMode(2, QtWidgets.QHeaderView.ResizeToContents)
I have shared a picture of my current table right now and I want to expand each column according to the content I have.
As you can see, there is a lot of space that can be stretched but it is not doing it despite having enough space. Even the Column names are not fully visible and some characters are cut off
In not maximized window form, this is what I get
As for my data, it is coming from MySQL database and it is attached to Button Click (whenever I click on the "Courses" button on the side menu, it will populate the data on the table in the Courses Page)
The function that is populating the table is as follows,
def get_course_data(self):
mycursor = self.DB.cursor() # DB is an object of class
Subquery = "Select course_name, course_code, course_type from tbl_course"
mycursor.execute(Subquery)
numcols = len(mycursor.fetchall()[0])
mycursor.execute(Subquery)
numrows = len(mycursor.fetchall())
self.ui.course_table.setRowCount(numrows)
self.ui.course_table.setColumnCount(numcols+1)
mycursor.execute(Subquery)
tablerow = 0
for row in mycursor.fetchall():
tablecol = 0
layout = QHBoxLayout()
layout.setContentsMargins(0, 0, 0, 0)
layout.setSpacing(0)
delete_button = QPushButton()
icon_delete = QIcon()
icon_delete.addFile(u":/icons/icons/Delete-button.png", QSize(), QIcon.Normal, QIcon.Off)
delete_button.setIcon(icon_delete)
layout.addWidget(delete_button)
edit_button = QPushButton()
index = PySide2.QtCore.QPersistentModelIndex(self.ui.course_table.model().index(tablerow, tablecol))
edit_button.clicked.connect(lambda *args, index=index: self.on_course_edit_click())
icon_edit = QIcon()
icon_edit.addFile(u":/icons/icons/edit-button.png", QSize(), QIcon.Normal, QIcon.Off)
edit_button.setIcon(icon_edit)
layout.addWidget(edit_button)
cellWidget = QWidget()
cellWidget.setLayout(layout)
self.ui.course_table.setCellWidget(tablerow, 3, cellWidget)
while tablecol < numcols:
self.ui.course_table.setItem(tablerow, tablecol, PySide2.QtWidgets.QTableWidgetItem(str(row[tablecol])))
tablecol += 1
tablerow += 1
self.ui.course_table.horizontalHeader().setSectionResizeMode(0, QtWidgets.QHeaderView.Stretch)
self.ui.course_table.horizontalHeader().setSectionResizeMode(1, QtWidgets.QHeaderView.ResizeToContents)
self.ui.course_table.horizontalHeader().setSectionResizeMode(2, QtWidgets.QHeaderView.ResizeToContents)
The layout of this page of mine is as follows, (it basically has three frames who are in horizontal layout and the third frame contains the QTableWidget. The third frame's sizePolicy is set to Expanding so that it covers the whole main body while the two other performs are aligned to the top of the main body)
The .ui file (for only the course widget) is as follows
<widget class="QWidget" name="courses_page">
<layout class="QVBoxLayout" name="verticalLayout_12">
<item alignment="Qt::AlignLeft|Qt::AlignTop">
<widget class="QFrame" name="frame_13">
<property name="frameShape">
<enum>QFrame::StyledPanel</enum>
</property>
<property name="frameShadow">
<enum>QFrame::Raised</enum>
</property>
<layout class="QHBoxLayout" name="horizontalLayout_12">
<item alignment="Qt::AlignLeft">
<widget class="QLabel" name="label_4">
<property name="font">
<font>
<family>Arial</family>
<pointsize>12</pointsize>
<weight>75</weight>
<bold>true</bold>
</font>
</property>
<property name="text">
<string>Courses Page</string>
</property>
</widget>
</item>
</layout>
</widget>
</item>
<item alignment="Qt::AlignTop">
<widget class="QFrame" name="frame_14">
<property name="frameShape">
<enum>QFrame::StyledPanel</enum>
</property>
<property name="frameShadow">
<enum>QFrame::Raised</enum>
</property>
<layout class="QHBoxLayout" name="horizontalLayout_13">
<item alignment="Qt::AlignLeft">
<widget class="QFrame" name="frame_16">
<property name="frameShape">
<enum>QFrame::StyledPanel</enum>
</property>
<property name="frameShadow">
<enum>QFrame::Raised</enum>
</property>
<layout class="QHBoxLayout" name="horizontalLayout_14">
<item>
<widget class="QPushButton" name="pushButton_2">
<property name="font">
<font>
<family>Calibri</family>
<pointsize>10</pointsize>
</font>
</property>
<property name="styleSheet">
<string notr="true">:hover
{
color: red;
font-size: 150%;
}</string>
</property>
<property name="text">
<string>Add New</string>
</property>
<property name="icon">
<iconset resource="icons.qrc">
<normaloff>:/icons/icons/icons8-add-48.png</normaloff>:/icons/icons/icons8-add-48.png</iconset>
</property>
<property name="iconSize">
<size>
<width>24</width>
<height>24</height>
</size>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="pushButton_3">
<property name="font">
<font>
<family>Calibri</family>
<pointsize>10</pointsize>
</font>
</property>
<property name="text">
<string>Delete All</string>
</property>
<property name="icon">
<iconset resource="icons.qrc">
<normaloff>:/icons/icons/icons8-remove-48.png</normaloff>:/icons/icons/icons8-remove-48.png</iconset>
</property>
<property name="iconSize">
<size>
<width>24</width>
<height>24</height>
</size>
</property>
</widget>
</item>
</layout>
</widget>
</item>
<item alignment="Qt::AlignLeft">
<widget class="QFrame" name="frame_17">
<property name="frameShape">
<enum>QFrame::StyledPanel</enum>
</property>
<property name="frameShadow">
<enum>QFrame::Raised</enum>
</property>
<layout class="QHBoxLayout" name="horizontalLayout_15">
<item>
<widget class="QLineEdit" name="lineEdit">
<property name="font">
<font>
<family>Arial</family>
<pointsize>10</pointsize>
</font>
</property>
<property name="styleSheet">
<string notr="true">background-color: white;
border-bottom: 1px solid;
color: black;</string>
</property>
<property name="placeholderText">
<string>Search Course</string>
</property>
</widget>
</item>
<item alignment="Qt::AlignLeft">
<widget class="QPushButton" name="pushButton">
<property name="text">
<string/>
</property>
<property name="icon">
<iconset resource="icons.qrc">
<normaloff>:/icons/icons/icons8-search-50.png</normaloff>:/icons/icons/icons8-search-50.png</iconset>
</property>
<property name="iconSize">
<size>
<width>24</width>
<height>24</height>
</size>
</property>
</widget>
</item>
</layout>
</widget>
</item>
</layout>
</widget>
</item>
<item>
<widget class="QFrame" name="frame_15">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Expanding">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="frameShape">
<enum>QFrame::StyledPanel</enum>
</property>
<property name="frameShadow">
<enum>QFrame::Raised</enum>
</property>
<layout class="QHBoxLayout" name="horizontalLayout_16">
<item>
<widget class="QTableWidget" name="course_table">
<property name="font">
<font>
<family>Arial</family>
<weight>50</weight>
<bold>false</bold>
<kerning>true</kerning>
</font>
</property>
<property name="styleSheet">
<string notr="true">:item
{
color: black;
}
QHeaderView:section
{
background-color: white;
font-weight: bold;
border: 0px;
}</string>
</property>
<property name="sizeAdjustPolicy">
<enum>QAbstractScrollArea::AdjustToContents</enum>
</property>
<property name="editTriggers">
<set>QAbstractItemView::NoEditTriggers</set>
</property>
<property name="alternatingRowColors">
<bool>true</bool>
</property>
<property name="gridStyle">
<enum>Qt::NoPen</enum>
</property>
<property name="sortingEnabled">
<bool>true</bool>
</property>
<property name="wordWrap">
<bool>true</bool>
</property>
<attribute name="horizontalHeaderMinimumSectionSize">
<number>47</number>
</attribute>
<attribute name="horizontalHeaderHighlightSections">
<bool>true</bool>
</attribute>
<attribute name="horizontalHeaderStretchLastSection">
<bool>false</bool>
</attribute>
<attribute name="verticalHeaderVisible">
<bool>false</bool>
</attribute>
<attribute name="verticalHeaderCascadingSectionResizes">
<bool>false</bool>
</attribute>
<attribute name="verticalHeaderMinimumSectionSize">
<number>28</number>
</attribute>
<attribute name="verticalHeaderHighlightSections">
<bool>true</bool>
</attribute>
<attribute name="verticalHeaderShowSortIndicator" stdset="0">
<bool>true</bool>
</attribute>
<attribute name="verticalHeaderStretchLastSection">
<bool>false</bool>
</attribute>
<row>
<property name="text">
<string>1</string>
</property>
</row>
<row>
<property name="text">
<string>2</string>
</property>
</row>
<column>
<property name="text">
<string>Course Name</string>
</property>
</column>
<column>
<property name="text">
<string>Course Code</string>
</property>
</column>
<column>
<property name="text">
<string>Course Type</string>
</property>
</column>
<column>
<property name="text">
<string>Action</string>
</property>
</column>
<item row="0" column="0">
<property name="text">
<string>Ha</string>
</property>
</item>
<item row="0" column="1">
<property name="text">
<string>111</string>
</property>
</item>
<item row="0" column="2">
<property name="text">
<string>1</string>
</property>
</item>
<item row="0" column="3">
<property name="text">
<string>aaa</string>
</property>
</item>
<item row="1" column="0">
<property name="text">
<string>assa</string>
</property>
</item>
<item row="1" column="1">
<property name="text">
<string>Programming Fundamentals</string>
</property>
</item>
<item row="1" column="2">
<property name="text">
<string>55</string>
</property>
</item>
<item row="1" column="3">
<property name="text">
<string>55</string>
</property>
</item>
</widget>
</item>
</layout>
</widget>
</item>
</layout>
</widget>
The complete interface.ui code is given here: https://pastebin.com/bSEmRACT
(As an example, I have added two sample rows in the data)
The complete interface.py code is given here: https://pastebin.com/AA79ndcw
The code of main.py is also shared here: https://pastebin.com/EWNAmH6K


