填充tableview覆盖其他ui元素

问题描述 投票:0回答:2

我面临我的QTableView问题,因为在用变量填充它后,它阻止了其他Ui元素。这是我的预期UI设计

“”

表视图被填充后(带有测试变量)的作用

“”

在Designer应用程序中设置ui时,我会错过什么吗?抱歉,新手问题是我第一次处理tableview。

用于填充表格的代码:

    def createTable(self):
        # Create table
        header_labels = ['Username', 'Password', 'Account Type']
        profiles = [["test", "pokemon","nanana"], ["dreams", "nani","hikari"]]
        self.tableWidget = QTableWidget()
        self.tableWidget.setRowCount(len(profiles))
        self.tableWidget.setColumnCount(len(header_labels))

        self.tableWidget.setHorizontalHeaderLabels(header_labels)
        self.tableWidget.verticalHeader().setVisible(False)
        for i,profile in enumerate(profiles):
            self.tableWidget.setItem(i, 0, QTableWidgetItem(profile[0]))
            self.tableWidget.setItem(i, 1, QTableWidgetItem(profile[1]))
            self.tableWidget.setItem(i, 2, QTableWidgetItem(profile[2]))
        self.tableWidget.move(0, 0)

我的Ui码(如果需要参考)

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>Form</class>
 <widget class="QWidget" name="Form">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>618</width>
    <height>463</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>Form</string>
  </property>
  <widget class="QLabel" name="label">
   <property name="geometry">
    <rect>
     <x>20</x>
     <y>20</y>
     <width>161</width>
     <height>16</height>
    </rect>
   </property>
   <property name="font">
    <font>
     <family>Arial</family>
     <pointsize>12</pointsize>
    </font>
   </property>
   <property name="text">
    <string>Account Management</string>
   </property>
  </widget>
  <widget class="QWidget" name="horizontalLayoutWidget">
   <property name="geometry">
    <rect>
     <x>20</x>
     <y>50</y>
     <width>301</width>
     <height>194</height>
    </rect>
   </property>
   <layout class="QHBoxLayout" name="horizontalLayout">
    <property name="sizeConstraint">
     <enum>QLayout::SetFixedSize</enum>
    </property>
    <item>
     <widget class="QTableView" name="profileView">
      <property name="sizePolicy">
       <sizepolicy hsizetype="MinimumExpanding" vsizetype="MinimumExpanding">
        <horstretch>0</horstretch>
        <verstretch>0</verstretch>
       </sizepolicy>
      </property>
      <property name="sizeAdjustPolicy">
       <enum>QAbstractScrollArea::AdjustToContents</enum>
      </property>
      <property name="showGrid">
       <bool>false</bool>
      </property>
     </widget>
    </item>
   </layout>
  </widget>
  <widget class="QWidget" name="verticalLayoutWidget">
   <property name="geometry">
    <rect>
     <x>430</x>
     <y>300</y>
     <width>158</width>
     <height>112</height>
    </rect>
   </property>
   <layout class="QVBoxLayout" name="verticalLayout">
    <item>
     <widget class="QPushButton" name="addProfileButton">
      <property name="text">
       <string>Add Profile</string>
      </property>
     </widget>
    </item>
    <item>
     <widget class="QPushButton" name="deleteProfileButton">
      <property name="text">
       <string>Delete Profile</string>
      </property>
     </widget>
    </item>
    <item>
     <widget class="QPushButton" name="updateProfileButton">
      <property name="text">
       <string>Update Profile</string>
      </property>
     </widget>
    </item>
    <item>
     <widget class="QPushButton" name="pushButton_3">
      <property name="text">
       <string>Logout</string>
      </property>
     </widget>
    </item>
   </layout>
  </widget>
 </widget>
 <resources/>
 <connections/>
</ui>
python pyqt qtableview
2个回答
0
投票

我建议不要使用固定尺寸。这样的约束仅在非常特定的情况下才有用(例如,您的应用程序应该在具有非常特定的尺寸和分辨率的显示器上显示,例如嵌入式系统)。通常,无论分辨率和屏幕大小如何,都应适当调整其大小并允许以正确的方式呈现它。

您的问题是小部件的布局不足:

enter image description here

您需要添加它,以便顶层布局可以调整表视图和按钮的大小:

enter image description here

我也不知道为什么您将表格视图本身全部放在布局中。除非您打算将其与其他小部件组合在一起,就像使用按钮组一样,否则这没有任何意义。仅当内部有多个小部件时,布局才有用。否则,您可以使用窗口小部件的调整大小策略并丢弃布局。


0
投票

不确定QtDesigner是否没有创建固定的宽度和高度,但是使用.setFixedWidth().setFixedHeight()起作用。

© www.soinside.com 2019 - 2024. All rights reserved.