Issue
I am working on a GUI using pyqt and the QTableWidget. Some rows in the table widget have text which is quite lengthy and I've been struggling to find a way to set a fixed column length for specific columns and then wrap the text to fit that column size no matter the text length.
I searched for solutions online, and while I saw one example addressing such an issue I was unable to implement it into my table widget. I had hoped there'd be a table widget function which does this but there isn't.
Help with this would be appreciated.
The 'Description' column in the table below should be shortened and the text made to wrap and fit a fixed size.
Thank you.
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(800, 600)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.gridLayout = QtWidgets.QGridLayout(self.centralwidget)
self.gridLayout.setObjectName("gridLayout")
self.tableWidget = QtWidgets.QTableWidget(self.centralwidget)
self.tableWidget.setObjectName("tableWidget")
self.tableWidget.setColumnCount(3)
self.tableWidget.setRowCount(6)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setVerticalHeaderItem(0, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setVerticalHeaderItem(1, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setVerticalHeaderItem(2, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setVerticalHeaderItem(3, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setVerticalHeaderItem(4, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setVerticalHeaderItem(5, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setHorizontalHeaderItem(0, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setHorizontalHeaderItem(1, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setHorizontalHeaderItem(2, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setItem(0, 0, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setItem(0, 1, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setItem(0, 2, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setItem(1, 0, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setItem(1, 1, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setItem(1, 2, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setItem(2, 0, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setItem(2, 1, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setItem(2, 2, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setItem(3, 0, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setItem(3, 1, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setItem(3, 2, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setItem(4, 0, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setItem(4, 1, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setItem(4, 2, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setItem(5, 0, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setItem(5, 1, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setItem(5, 2, item)
self.tableWidget.horizontalHeader().setCascadingSectionResizes(True)
self.tableWidget.horizontalHeader().setStretchLastSection(True)
self.gridLayout.addWidget(self.tableWidget, 0, 0, 1, 1)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 21))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
item = self.tableWidget.verticalHeaderItem(0)
item.setText(_translate("MainWindow", "1"))
item = self.tableWidget.verticalHeaderItem(1)
item.setText(_translate("MainWindow", "2"))
item = self.tableWidget.verticalHeaderItem(2)
item.setText(_translate("MainWindow", "3"))
item = self.tableWidget.verticalHeaderItem(3)
item.setText(_translate("MainWindow", "4"))
item = self.tableWidget.verticalHeaderItem(4)
item.setText(_translate("MainWindow", "5"))
item = self.tableWidget.verticalHeaderItem(5)
item.setText(_translate("MainWindow", "6"))
item = self.tableWidget.horizontalHeaderItem(0)
item.setText(_translate("MainWindow", "Name"))
item = self.tableWidget.horizontalHeaderItem(1)
item.setText(_translate("MainWindow", "Quantity"))
item = self.tableWidget.horizontalHeaderItem(2)
item.setText(_translate("MainWindow", "Description"))
__sortingEnabled = self.tableWidget.isSortingEnabled()
self.tableWidget.setSortingEnabled(False)
item = self.tableWidget.item(0, 0)
item.setText(_translate("MainWindow", "Pawn"))
item = self.tableWidget.item(0, 1)
item.setText(_translate("MainWindow", "8"))
item = self.tableWidget.item(0, 2)
item.setText(_translate("MainWindow", "The pawn is the least powerful piece and is worth one point. If it is a pawn\'s first move, it can move forward one or two squares. If a pawn has already moved, then it can move forward just one square at a time."))
item = self.tableWidget.item(1, 0)
item.setText(_translate("MainWindow", "Knight"))
item = self.tableWidget.item(1, 1)
item.setText(_translate("MainWindow", "2"))
item = self.tableWidget.item(1, 2)
item.setText(_translate("MainWindow", "The knight is considered a minor piece (like a bishop) and is worth three points. The knight is the only piece in chess that can jump over another piece!"))
item = self.tableWidget.item(2, 0)
item.setText(_translate("MainWindow", "Bishop"))
item = self.tableWidget.item(2, 1)
item.setText(_translate("MainWindow", "2"))
item = self.tableWidget.item(2, 2)
item.setText(_translate("MainWindow", "The bishop is considered a minor piece (like a knight) and is worth three points. A bishop can move diagonally as many squares as it likes, as long as it is not blocked by its own pieces or an occupied square. "))
item = self.tableWidget.item(3, 0)
item.setText(_translate("MainWindow", "Rook"))
item = self.tableWidget.item(3, 1)
item.setText(_translate("MainWindow", "2"))
item = self.tableWidget.item(3, 2)
item.setText(_translate("MainWindow", "The rook is considered a major piece (like the queen) and is worth five points. It can move as many squares as it likes left or right horizontally, or as many squares as it likes up or down vertically (as long as it isn\'t blocked by other pieces)."))
item = self.tableWidget.item(4, 0)
item.setText(_translate("MainWindow", "Queen"))
item = self.tableWidget.item(4, 1)
item.setText(_translate("MainWindow", "1"))
item = self.tableWidget.item(4, 2)
item.setText(_translate("MainWindow", "The queen is considered a major piece (like a rook) and is worth nine points. It can move as many squares as it likes left or right horizontally, or as many squares as it likes up or down vertically (like a rook). "))
item = self.tableWidget.item(5, 0)
item.setText(_translate("MainWindow", "King"))
item = self.tableWidget.item(5, 1)
item.setText(_translate("MainWindow", "1"))
item = self.tableWidget.item(5, 2)
item.setText(_translate("MainWindow", "The king is not a very powerful piece, as it can only move (or capture) one square in any direction. Please note that the king cannot be captured! When a king is attacked, it is called \"check.\""))
self.tableWidget.setSortingEnabled(__sortingEnabled)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
Solution
You may need to force your table widget to resize its rows by calling QTableWidget.resizeRowsToContent
.
You can do it automatically whenever the column has been resized with:
self.tableWidget.horizontalHeader().sectionResized.connect(self.tableWidget.resizeRowsToContents)
Answered By - Dimitry Ernot
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.