<small id='cmLFc'></small><noframes id='cmLFc'>

    <i id='cmLFc'><tr id='cmLFc'><dt id='cmLFc'><q id='cmLFc'><span id='cmLFc'><b id='cmLFc'><form id='cmLFc'><ins id='cmLFc'></ins><ul id='cmLFc'></ul><sub id='cmLFc'></sub></form><legend id='cmLFc'></legend><bdo id='cmLFc'><pre id='cmLFc'><center id='cmLFc'></center></pre></bdo></b><th id='cmLFc'></th></span></q></dt></tr></i><div id='cmLFc'><tfoot id='cmLFc'></tfoot><dl id='cmLFc'><fieldset id='cmLFc'></fieldset></dl></div>
      <bdo id='cmLFc'></bdo><ul id='cmLFc'></ul>

    1. <tfoot id='cmLFc'></tfoot>
    2. <legend id='cmLFc'><style id='cmLFc'><dir id='cmLFc'><q id='cmLFc'></q></dir></style></legend>

      为 QTableWidget 的列分配不同的宽度

      时间:2023-08-04
      • <bdo id='zFzf9'></bdo><ul id='zFzf9'></ul>
        <tfoot id='zFzf9'></tfoot>

          <legend id='zFzf9'><style id='zFzf9'><dir id='zFzf9'><q id='zFzf9'></q></dir></style></legend>

            <small id='zFzf9'></small><noframes id='zFzf9'>

              • <i id='zFzf9'><tr id='zFzf9'><dt id='zFzf9'><q id='zFzf9'><span id='zFzf9'><b id='zFzf9'><form id='zFzf9'><ins id='zFzf9'></ins><ul id='zFzf9'></ul><sub id='zFzf9'></sub></form><legend id='zFzf9'></legend><bdo id='zFzf9'><pre id='zFzf9'><center id='zFzf9'></center></pre></bdo></b><th id='zFzf9'></th></span></q></dt></tr></i><div id='zFzf9'><tfoot id='zFzf9'></tfoot><dl id='zFzf9'><fieldset id='zFzf9'></fieldset></dl></div>
                  <tbody id='zFzf9'></tbody>
                本文介绍了为 QTableWidget 的列分配不同的宽度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                我正在 QT Designerpyqt5 中开发一个小界面,其中包括一个 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模板网!

                上一篇:线程时出现 msgbox 错误,GUI 块 下一篇:QCompleter 给 pyqt5 中的 lineEdit 带来奇怪的错误

                相关文章

                最新文章

                <legend id='3MpKp'><style id='3MpKp'><dir id='3MpKp'><q id='3MpKp'></q></dir></style></legend>

                <tfoot id='3MpKp'></tfoot>
                    <bdo id='3MpKp'></bdo><ul id='3MpKp'></ul>
                1. <small id='3MpKp'></small><noframes id='3MpKp'>

                  <i id='3MpKp'><tr id='3MpKp'><dt id='3MpKp'><q id='3MpKp'><span id='3MpKp'><b id='3MpKp'><form id='3MpKp'><ins id='3MpKp'></ins><ul id='3MpKp'></ul><sub id='3MpKp'></sub></form><legend id='3MpKp'></legend><bdo id='3MpKp'><pre id='3MpKp'><center id='3MpKp'></center></pre></bdo></b><th id='3MpKp'></th></span></q></dt></tr></i><div id='3MpKp'><tfoot id='3MpKp'></tfoot><dl id='3MpKp'><fieldset id='3MpKp'></fieldset></dl></div>