python - QtDesigner在保存到ui文件时重新排序单选按钮
问题描述
我正在使用 QtDesigner 为 PyQT5 应用程序创建一个 uifile。我在 ButtonGroup 中有一组 QRadioButtons。我遇到的问题是,当我保存到 uifile 时,正在保存的按钮的顺序会影响布局。当我打印每个按钮的 QRadioButton.geometry() 时,值是正确的,但除非我编辑 uifile 并重新排序小部件,否则它们不会在最终的 GUI 中以这种方式布局。
这是所需的布局:
rb6 rb7 rb8
rb3 rb4 rb5 rb9
rb0 rb1 rb2
在 Notepad++ 中搜索“QRadioButton”会显示按钮的保存顺序:
Search "RadioButton" (10 hits in 1 file)
E:\FileNamingWidget.ui (10 hits)
Line 543: <widget class="QRadioButton" name="Rb8">
Line 565: <widget class="QRadioButton" name="Rb0">
Line 590: <widget class="QRadioButton" name="Rb9">
Line 618: <widget class="QRadioButton" name="Rb7">
Line 640: <widget class="QRadioButton" name="Rb3">
Line 662: <widget class="QRadioButton" name="Rb4">
Line 687: <widget class="QRadioButton" name="Rb5">
Line 709: <widget class="QRadioButton" name="Rb2">
Line 731: <widget class="QRadioButton" name="Rb6">
Line 753: <widget class="QRadioButton" name="Rb1">
然后单选按钮的布局如下:
rb8 rb3 rb0
rb4 rb5 rb6 rb2
rb1 rb9 rb7
为什么?除了在我将它加载到我的 Python 程序之前手动编辑 uifile 之外,我该uic.loadUI()
如何解决这个问题?
谢谢你的帮助。
以下是 uifile 中的 QRadioButton 小部件,按保存顺序排列:
<widget class="QRadioButton" name="Rb8">
<property name="geometry">
<rect>
<x>107</x>
<y>121</y>
<width>16</width>
<height>13</height>
</rect>
</property>
<property name="toolTip">
<string>Right Rear</string>
</property>
<property name="text">
<string/>
</property>
<property name="autoExclusive">
<bool>false</bool>
</property>
<attribute name="buttonGroup">
<string notr="true">buttonGroup</string>
</attribute>
</widget>
<widget class="QRadioButton" name="Rb0">
<property name="enabled">
<bool>true</bool>
</property>
<property name="geometry">
<rect>
<x>62</x>
<y>159</y>
<width>16</width>
<height>13</height>
</rect>
</property>
<property name="toolTip">
<string>Left Front</string>
</property>
<property name="text">
<string/>
</property>
<property name="autoExclusive">
<bool>false</bool>
</property>
<attribute name="buttonGroup">
<string notr="true">buttonGroup</string>
</attribute>
</widget>
<widget class="QRadioButton" name="Rb9">
<property name="geometry">
<rect>
<x>130</x>
<y>140</y>
<width>13</width>
<height>13</height>
</rect>
</property>
<property name="toolTip">
<string>Multi Aspect</string>
</property>
<property name="text">
<string/>
</property>
<property name="shortcut">
<string/>
</property>
<property name="checked">
<bool>false</bool>
</property>
<property name="autoExclusive">
<bool>false</bool>
</property>
<attribute name="buttonGroup">
<string notr="true">buttonGroup</string>
</attribute>
</widget>
<widget class="QRadioButton" name="Rb7">
<property name="geometry">
<rect>
<x>84</x>
<y>121</y>
<width>17</width>
<height>13</height>
</rect>
</property>
<property name="toolTip">
<string>Rear</string>
</property>
<property name="text">
<string/>
</property>
<property name="autoExclusive">
<bool>false</bool>
</property>
<attribute name="buttonGroup">
<string notr="true">buttonGroup</string>
</attribute>
</widget>
<widget class="QRadioButton" name="Rb3">
<property name="geometry">
<rect>
<x>62</x>
<y>140</y>
<width>16</width>
<height>13</height>
</rect>
</property>
<property name="toolTip">
<string>Left Side</string>
</property>
<property name="text">
<string/>
</property>
<property name="autoExclusive">
<bool>false</bool>
</property>
<attribute name="buttonGroup">
<string notr="true">buttonGroup</string>
</attribute>
</widget>
<widget class="QRadioButton" name="Rb4">
<property name="geometry">
<rect>
<x>84</x>
<y>140</y>
<width>17</width>
<height>13</height>
</rect>
</property>
<property name="toolTip">
<string>Center</string>
</property>
<property name="text">
<string/>
</property>
<property name="checked">
<bool>true</bool>
</property>
<property name="autoExclusive">
<bool>false</bool>
</property>
<attribute name="buttonGroup">
<string notr="true">buttonGroup</string>
</attribute>
</widget>
<widget class="QRadioButton" name="Rb5">
<property name="geometry">
<rect>
<x>107</x>
<y>140</y>
<width>16</width>
<height>13</height>
</rect>
</property>
<property name="toolTip">
<string>Right Side</string>
</property>
<property name="text">
<string/>
</property>
<property name="autoExclusive">
<bool>false</bool>
</property>
<attribute name="buttonGroup">
<string notr="true">buttonGroup</string>
</attribute>
</widget>
<widget class="QRadioButton" name="Rb2">
<property name="geometry">
<rect>
<x>107</x>
<y>159</y>
<width>16</width>
<height>13</height>
</rect>
</property>
<property name="toolTip">
<string>Right Front</string>
</property>
<property name="text">
<string/>
</property>
<property name="autoExclusive">
<bool>false</bool>
</property>
<attribute name="buttonGroup">
<string notr="true">buttonGroup</string>
</attribute>
</widget>
<widget class="QRadioButton" name="Rb6">
<property name="geometry">
<rect>
<x>62</x>
<y>121</y>
<width>16</width>
<height>13</height>
</rect>
</property>
<property name="toolTip">
<string>Left Rear</string>
</property>
<property name="text">
<string/>
</property>
<property name="autoExclusive">
<bool>false</bool>
</property>
<attribute name="buttonGroup">
<string notr="true">buttonGroup</string>
</attribute>
</widget>
<widget class="QRadioButton" name="Rb1">
<property name="geometry">
<rect>
<x>84</x>
<y>159</y>
<width>17</width>
<height>13</height>
</rect>
</property>
<property name="toolTip">
<string>Front</string>
</property>
<property name="text">
<string/>
</property>
<property name="autoExclusive">
<bool>false</bool>
</property>
<attribute name="buttonGroup">
<string notr="true">buttonGroup</string>
</attribute>
</widget>
</widget>
分块添加整个 FileNamingWidget.ui:
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Filename_Folder_Widget</class>
<widget class="QTabWidget" name="Filename_Folder_Widget">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>261</width>
<height>550</height>
</rect>
</property>
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="minimumSize">
<size>
<width>260</width>
<height>550</height>
</size>
</property>
<property name="windowTitle">
<string>TabWidget</string>
</property>
<property name="currentIndex">
<number>0</number>
</property>
<widget class="QWidget" name="FileNamingTab">
<attribute name="title">
<string>File Naming</string>
</attribute>
<widget class="QLabel" name="AspectLabel">
<property name="geometry">
<rect>
<x>10</x>
<y>140</y>
<width>47</width>
<height>13</height>
</rect>
</property>
<property name="text">
<string>Aspect</string>
</property>
</widget>
<widget class="QLabel" name="SubjectLabel">
<property name="geometry">
<rect>
<x>10</x>
<y>30</y>
<width>47</width>
<height>21</height>
</rect>
</property>
<property name="maximumSize">
<size>
<width>16777215</width>
<height>16777215</height>
</size>
</property>
<property name="text">
<string>Subject</string>
</property>
</widget>
<widget class="QLabel" name="RangeLabel">
<property name="geometry">
<rect>
<x>10</x>
<y>60</y>
<width>47</width>
<height>21</height>
</rect>
</property>
<property name="text">
<string>Range</string>
</property>
</widget>
<widget class="QLabel" name="PositionLabel">
<property name="geometry">
<rect>
<x>10</x>
<y>90</y>
<width>47</width>
<height>21</height>
</rect>
</property>
<property name="text">
<string>Position</string>
</property>
</widget>
<widget class="QComboBox" name="SubjectDropDown">
<property name="geometry">
<rect>
<x>60</x>
<y>30</y>
<width>91</width>
<height>22</height>
</rect>
</property>
</widget>
<widget class="QComboBox" name="RangeDropDown">
<property name="geometry">
<rect>
<x>60</x>
<y>60</y>
<width>91</width>
<height>22</height>
</rect>
</property>
</widget>
<widget class="QComboBox" name="PositionDropDown">
<property name="geometry">
<rect>
<x>60</x>
<y>90</y>
<width>91</width>
<height>22</height>
</rect>
</property>
</widget>
<widget class="QLabel" name="FOVLabel">
<property name="geometry">
<rect>
<x>10</x>
<y>190</y>
<width>47</width>
<height>21</height>
</rect>
</property>
<property name="text">
<string>FOV</string>
</property>
</widget>
<widget class="QComboBox" name="FOVDropDown">
<property name="geometry">
<rect>
<x>60</x>
<y>190</y>
<width>91</width>
<height>22</height>
</rect>
</property>
</widget>
<widget class="QPushButton" name="EditOptionsButton">
<property name="geometry">
<rect>
<x>20</x>
<y>310</y>
<width>91</width>
<height>21</height>
</rect>
</property>
<property name="text">
<string>Edit I/O Settings</string>
</property>
</widget>
<widget class="QLabel" name="RunLabel">
<property name="geometry">
<rect>
<x>10</x>
<y>220</y>
<width>47</width>
<height>21</height>
</rect>
</property>
<property name="text">
<string>Run #</string>
</property>
</widget>
<widget class="QLabel" name="TakeLabel">
<property name="geometry">
<rect>
<x>10</x>
<y>250</y>
<width>47</width>
<height>21</height>
</rect>
</property>
<property name="text">
<string>Take #</string>
</property>
</widget>
<widget class="Line" name="HorizLine1">
<property name="geometry">
<rect>
<x>10</x>
<y>300</y>
<width>221</width>
<height>16</height>
</rect>
</property>
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
</widget>
<widget class="QPushButton" name="LoadButton">
<property name="geometry">
<rect>
<x>60</x>
<y>340</y>
<width>51</width>
<height>23</height>
</rect>
</property>
<property name="text">
<string>Load</string>
</property>
</widget>
<widget class="QPushButton" name="SaveButton">
<property name="geometry">
<rect>
<x>120</x>
<y>340</y>
<width>51</width>
<height>23</height>
</rect>
</property>
<property name="text">
<string>Save</string>
</property>
</widget>
<widget class="QPushButton" name="SettingsButton">
<property name="geometry">
<rect>
<x>120</x>
<y>310</y>
<width>101</width>
<height>23</height>
</rect>
</property>
<property name="text">
<string>Structure Settings</string>
</property>
</widget>
<widget class="QTextEdit" name="NotesInput">
<property name="geometry">
<rect>
<x>10</x>
<y>390</y>
<width>221</width>
<height>111</height>
</rect>
</property>
</widget>
<widget class="QLabel" name="NotesLabel">
<property name="geometry">
<rect>
<x>10</x>
<y>370</y>
<width>211</width>
<height>21</height>
</rect>
</property>
<property name="text">
<string>Notes</string>
</property>
</widget>
<widget class="QPushButton" name="SaveNotesButton">
<property name="geometry">
<rect>
<x>70</x>
<y>500</y>
<width>81</width>
<height>23</height>
</rect>
</property>
<property name="text">
<string>Save Notes</string>
</property>
</widget>
<widget class="QTextEdit" name="AspectPreview">
<property name="geometry">
<rect>
<x>170</x>
<y>140</y>
<width>61</width>
<height>21</height>
</rect>
</property>
<property name="verticalScrollBarPolicy">
<enum>Qt::ScrollBarAlwaysOff</enum>
</property>
<property name="horizontalScrollBarPolicy">
<enum>Qt::ScrollBarAlwaysOff</enum>
</property>
<property name="readOnly">
<bool>true</bool>
</property>
</widget>
<widget class="QTextEdit" name="SubjectPreview">
<property name="geometry">
<rect>
<x>160</x>
<y>30</y>
<width>71</width>
<height>21</height>
</rect>
</property>
<property name="autoFillBackground">
<bool>false</bool>
</property>
<property name="verticalScrollBarPolicy">
<enum>Qt::ScrollBarAlwaysOff</enum>
</property>
<property name="horizontalScrollBarPolicy">
<enum>Qt::ScrollBarAlwaysOff</enum>
</property>
<property name="readOnly">
<bool>true</bool>
</property>
</widget>
<widget class="QTextEdit" name="RangePreview">
<property name="geometry">
<rect>
<x>160</x>
<y>60</y>
<width>71</width>
<height>21</height>
</rect>
</property>
<property name="verticalScrollBarPolicy">
<enum>Qt::ScrollBarAlwaysOff</enum>
</property>
<property name="horizontalScrollBarPolicy">
<enum>Qt::ScrollBarAlwaysOff</enum>
</property>
<property name="readOnly">
<bool>true</bool>
</property>
</widget>
<widget class="QTextEdit" name="PositionPreview">
<property name="geometry">
<rect>
<x>160</x>
<y>90</y>
<width>71</width>
<height>21</height>
</rect>
</property>
<property name="verticalScrollBarPolicy">
<enum>Qt::ScrollBarAlwaysOff</enum>
</property>
<property name="horizontalScrollBarPolicy">
<enum>Qt::ScrollBarAlwaysOff</enum>
</property>
<property name="readOnly">
<bool>true</bool>
</property>
</widget>
<widget class="QTextEdit" name="FOVPreview">
<property name="geometry">
<rect>
<x>160</x>
<y>190</y>
<width>71</width>
<height>21</height>
</rect>
</property>
<property name="verticalScrollBarPolicy">
<enum>Qt::ScrollBarAlwaysOff</enum>
</property>
<property name="horizontalScrollBarPolicy">
<enum>Qt::ScrollBarAlwaysOff</enum>
</property>
<property name="readOnly">
<bool>true</bool>
</property>
</widget>
<widget class="QTextEdit" name="RunPreview">
<property name="geometry">
<rect>
<x>160</x>
<y>220</y>
<width>71</width>
<height>21</height>
</rect>
</property>
<property name="verticalScrollBarPolicy">
<enum>Qt::ScrollBarAlwaysOff</enum>
</property>
<property name="horizontalScrollBarPolicy">
<enum>Qt::ScrollBarAlwaysOff</enum>
</property>
<property name="readOnly">
<bool>true</bool>
</property>
</widget>
<widget class="QTextEdit" name="SuffixInput">
<property name="geometry">
<rect>
<x>60</x>
<y>280</y>
<width>171</width>
<height>21</height>
</rect>
</property>
<property name="verticalScrollBarPolicy">
<enum>Qt::ScrollBarAlwaysOff</enum>
</property>
<property name="horizontalScrollBarPolicy">
<enum>Qt::ScrollBarAlwaysOff</enum>
</property>
</widget>
<widget class="QLabel" name="SuffixLabel_2">
<property name="geometry">
<rect>
<x>10</x>
<y>280</y>
<width>47</width>
<height>21</height>
</rect>
</property>
<property name="text">
<string>Suffix</string>
</property>
</widget>
<widget class="QTextEdit" name="TakePreview">
<property name="geometry">
<rect>
<x>160</x>
<y>250</y>
<width>71</width>
<height>21</height>
</rect>
</property>
<property name="verticalScrollBarPolicy">
<enum>Qt::ScrollBarAlwaysOff</enum>
</property>
<property name="horizontalScrollBarPolicy">
<enum>Qt::ScrollBarAlwaysOff</enum>
</property>
<property name="readOnly">
<bool>true</bool>
</property>
</widget>
<widget class="QSpinBox" name="RunSpin">
<property name="geometry">
<rect>
<x>60</x>
<y>220</y>
<width>91</width>
<height>22</height>
</rect>
</property>
<property name="frame">
<bool>true</bool>
</property>
<property name="maximum">
<number>10000</number>
</property>
</widget>
<widget class="QSpinBox" name="TakeSpin">
<property name="geometry">
<rect>
<x>60</x>
<y>250</y>
<width>91</width>
<height>22</height>
</rect>
</property>
</widget>
<widget class="QLabel" name="SubjectLabel_2">
<property name="geometry">
<rect>
<x>166</x>
<y>0</y>
<width>61</width>
<height>21</height>
</rect>
</property>
<property name="font">
<font>
<weight>75</weight>
<bold>true</bold>
</font>
</property>
<property name="text">
<string>Output</string>
</property>
<property name="alignment">
<set>Qt::AlignCenter</set>
</property>
</widget>
<widget class="QLabel" name="SubjectLabel_3">
<property name="geometry">
<rect>
<x>60</x>
<y>0</y>
<width>91</width>
<height>21</height>
</rect>
</property>
<property name="font">
<font>
<weight>75</weight>
<bold>true</bold>
</font>
</property>
<property name="text">
<string>Input</string>
</property>
<property name="alignment">
<set>Qt::AlignCenter</set>
</property>
</widget>
<widget class="QLabel" name="SubjectLabel_4">
<property name="geometry">
<rect>
<x>10</x>
<y>0</y>
<width>31</width>
<height>21</height>
</rect>
</property>
<property name="font">
<font>
<weight>75</weight>
<bold>true</bold>
</font>
</property>
<property name="text">
<string>Field</string>
</property>
<property name="alignment">
<set>Qt::AlignCenter</set>
</property>
</widget>
<widget class="Line" name="HorizLine1_2">
<property name="geometry">
<rect>
<x>10</x>
<y>360</y>
<width>221</width>
<height>16</height>
</rect>
</property>
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
</widget>
带有单选按钮的块:
<widget class="QRadioButton" name="Rb8">
<property name="geometry">
<rect>
<x>107</x>
<y>121</y>
<width>16</width>
<height>13</height>
</rect>
</property>
<property name="toolTip">
<string>Right Rear</string>
</property>
<property name="text">
<string/>
</property>
<property name="autoExclusive">
<bool>false</bool>
</property>
<attribute name="buttonGroup">
<string notr="true">buttonGroup</string>
</attribute>
</widget>
<widget class="QRadioButton" name="Rb0">
<property name="enabled">
<bool>true</bool>
</property>
<property name="geometry">
<rect>
<x>62</x>
<y>159</y>
<width>16</width>
<height>13</height>
</rect>
</property>
<property name="toolTip">
<string>Left Front</string>
</property>
<property name="text">
<string/>
</property>
<property name="autoExclusive">
<bool>false</bool>
</property>
<attribute name="buttonGroup">
<string notr="true">buttonGroup</string>
</attribute>
</widget>
<widget class="QRadioButton" name="Rb9">
<property name="geometry">
<rect>
<x>130</x>
<y>140</y>
<width>13</width>
<height>13</height>
</rect>
</property>
<property name="toolTip">
<string>Multi Aspect</string>
</property>
<property name="text">
<string/>
</property>
<property name="shortcut">
<string/>
</property>
<property name="checked">
<bool>false</bool>
</property>
<property name="autoExclusive">
<bool>false</bool>
</property>
<attribute name="buttonGroup">
<string notr="true">buttonGroup</string>
</attribute>
</widget>
<widget class="QRadioButton" name="Rb7">
<property name="geometry">
<rect>
<x>84</x>
<y>121</y>
<width>17</width>
<height>13</height>
</rect>
</property>
<property name="toolTip">
<string>Rear</string>
</property>
<property name="text">
<string/>
</property>
<property name="autoExclusive">
<bool>false</bool>
</property>
<attribute name="buttonGroup">
<string notr="true">buttonGroup</string>
</attribute>
</widget>
<widget class="QRadioButton" name="Rb3">
<property name="geometry">
<rect>
<x>62</x>
<y>140</y>
<width>16</width>
<height>13</height>
</rect>
</property>
<property name="toolTip">
<string>Left Side</string>
</property>
<property name="text">
<string/>
</property>
<property name="autoExclusive">
<bool>false</bool>
</property>
<attribute name="buttonGroup">
<string notr="true">buttonGroup</string>
</attribute>
</widget>
<widget class="QRadioButton" name="Rb4">
<property name="geometry">
<rect>
<x>84</x>
<y>140</y>
<width>17</width>
<height>13</height>
</rect>
</property>
<property name="toolTip">
<string>Center</string>
</property>
<property name="text">
<string/>
</property>
<property name="checked">
<bool>true</bool>
</property>
<property name="autoExclusive">
<bool>false</bool>
</property>
<attribute name="buttonGroup">
<string notr="true">buttonGroup</string>
</attribute>
</widget>
(插入整个uifile太麻烦了)
解决方案
抱歉,我使用 github.com/tjwilli58/scratch-files/blob/master/ 下载了您的文件 FileNamingWidget.ui,但我不明白出了什么问题?
试试看:
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.uic import loadUi
class Tutorial(QTabWidget): #(QMainWindow): # QWidget
def __init__(self):
super(Tutorial, self).__init__()
loadUi('FileNamingWidget.ui', self)
print(self.Rb6.geometry(), self.Rb7.geometry(), self.Rb8.geometry())
print(self.Rb3.geometry(), self.Rb4.geometry(), self.Rb5.geometry(), self.Rb9.geometry())
print(self.Rb0.geometry(), self.Rb1.geometry(), self.Rb2.geometry())
print()
self.Rb0.toggled.connect(self.rbChecked)
self.Rb1.toggled.connect(self.rbChecked)
self.Rb2.toggled.connect(self.rbChecked)
self.Rb3.toggled.connect(self.rbChecked)
self.Rb4.toggled.connect(self.rbChecked)
self.Rb5.toggled.connect(self.rbChecked)
self.Rb6.toggled.connect(self.rbChecked)
self.Rb7.toggled.connect(self.rbChecked)
self.Rb8.toggled.connect(self.rbChecked)
self.Rb9.toggled.connect(self.rbChecked)
def rbChecked(self):
if self.sender().isChecked():
print(self.sender().objectName())
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Tutorial()
ex.show()
sys.exit(app.exec_())
推荐阅读
- r - 当行超过某些数字时,R apply 无法获得多个分钟
- javascript - 在 Vue.js 中从 DOM 中删除组件
- python - Pythonic通过str列表对对象列表进行分组的方法
- python - 如何让 mov 功能正常工作
- sql - SQL 显示不等于的记录
- javascript - 无法将用户上传的大文本文件从 Google Scripts Web App(JS 模块)传递到代码模块
- java - 如何使用递归将字符串中的字符大写?
- sql - PostgreSQL "group by" 与 "order by" 和 "limit" 意外工作
- javascript - 如何使用 sequelize 进行部署并在 heroku 中做出反应
- node.js - 节点JS | 在完成事件上访问发送数据