問題描述
我正在 QT Designer 在 pyqt5 中開發一個小界面,其中包括一個 QTableWidget 但我想分配不同的列的寬度,我找到了談論相同的主題,但我不知道在哪里插入他們提供的代碼,我不知道是不是因為版本,我很新在 QT 設計器中.
I'm developing a small interface in pyqt5 from QT Designer, this includes a QTableWidget but I want to assign different widths to the columns, I have found topics that talk about the same but I don't know exactly where to insert the code they provide, I don't know if it's because of the version, I'm quite new in QT Designer.
我會留下我提到的問題.
PyQt:如何設置不同的標題大小對于單個標題?
PyQt 設置列寬
我的文件結構如下:
app.py:存儲應用程序的功能
SGS.py: .ui文件轉換成.py
SGS.ui
我將生成表頭的 SGS.py 部分保留,因為它的價值.
I leave the SGS.py part where the table headers are generated for what it's worth.
item = self.TableDocs.horizontalHeaderItem(0)
item.setText(_translate("MainWindow", "IDsystem"))
item = self.TableDocs.horizontalHeaderItem(2)
item.setText(_translate("MainWindow", "IDpeople"))
item = self.TableDocs.horizontalHeaderItem(3)
item.setText(_translate("MainWindow", "Work"))
item = self.TableDocs.horizontalHeaderItem(4)
item.setText(_translate("MainWindow", "Hours"))
我也留下了填寫表格的代碼
result = Cur.execute("SELECT idsystem,IDpeople,work,hours FROM workers")
self.TableDocs.setRowCount(0)
for row_number, row_data in enumerate(result):
self.TableDocs.insertRow(row_number)
for column_number, data in enumerate(row_data):
self.TableDocs.setItem(row_number, column_number, QtWidgets.QTableWidgetItem(str(data)))
推薦答案
從 ui 生成的 python 文件永遠不要被編輯.將其視為用于創建"接口的資源文件(如圖像或 json 文件).您無法通過 Designer 完成的所有操作都必須在您的應用程序代碼文件中實現.
The python file generated from the ui should never be edited. Consider it as a resource file (like an image or a json file) that is used to "create" the interface. Everything that you can't do from Designer you will have to implement in your application code files.
每當模型應用于項目視圖時,您都可以設置大小(或調整大小模式,例如自動調整大小,或拉伸"到可用寬度).由于您使用的是 QTableWidget(它具有其內部私有模型),因此您可以在界面中創建小部件后立即執行此操作,即在 setupUi
(或 loadUi
) 如果使用設計器文件.
You can set the size (or resize mode, for example automatic resizing, or the "stretching" to the available width) whenever a model is applied to the item view. Since you're using a QTableWidget (which has its internal private model), you can do that as soon as the widget is created in the interface, which is right after setupUi
(or loadUi
) if using a designer file.
為了設置部分的大小和行為,您需要訪問表格標題:列的水平標題,行的垂直標題.
In order to set the size and behavior of the sections, you need to access the table headers: the horizontal one for the columns, the vertical for the rows.
class MyWindow(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self):
super(MyWindow, self).__init__()
self.setupUi(self)
horizontalHeader = self.TableDocs.horizontalHeader()
# resize the first column to 100 pixels
horizontalHeader.resizeSection(0, 100)
# adjust the second column to its contents
horizontalHeader.setSectionResizeMode(
1, QtWidgets.QHeaderView.ResizeToContents)
# adapt the third column to fill all available space
horizontalHeader.setSectionResizeMode(
2, QtWidgets.QHeaderView.Stretch)
請注意,如果您刪除一列并在同一位置插入另一列,則需要再次設置其部分大小或模式.
Note that if you remove a column and insert another one in the same place, you'll need to set its section size or mode once again.
這篇關于為 QTableWidget 的列分配不同的寬度的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!