最初,在Qt Designer dial
句柄中,我将Enabled False
. 在这个任务中,句柄应该在激活时激活radioButton
,在激活时停用radioButton_2
。
- 如何在代码中实现它?
- 是否可以直接在Qt Designer中进行此类设置?摆脱不必要的代码编写?
主文件
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow
from PyQt5.QtGui import QFont
from PyQt5.QtCore import QSettings
from PyQt5.uic import loadUi
CONFIG_FILE_NAME = 'config.ini'
class New(QMainWindow):
def __init__(self):
super(New, self).__init__()
loadUi("RButton_Dial_save.ui", self)
self.Exit.clicked.connect(self.close)
self.dial.setMinimum(0)
self.dial.setMaximum(10)
self.dial.setValue(5)
self.spinbox.setRange(5, 100)
self.spinbox.setValue(5)
self.load_settings()
self.dial.setMinimum(self.spinbox.value() - 5)
self.dial.setMaximum(self.spinbox.value() + 5)
self.dial.setValue(self.spinbox.value())
self.dial.valueChanged.connect(self.dial_changed)
self.spinbox.valueChanged.connect(self.spinbox_changed)
self.statusbar = self.statusBar()
self.statusbar.showMessage('Hello World', msecs=2000)
self.dial.setValue(self._dial)
self.radioButton.clicked.connect(self.spinbox_changed)
def save_settings(self):
settings = QSettings(CONFIG_FILE_NAME, QSettings.IniFormat)
settings.setValue('DialValue', self.dial.value())
settings.setValue('SpinBoxValue', self.spinbox.value())
def load_settings(self):
settings = QSettings(CONFIG_FILE_NAME, QSettings.IniFormat)
self.dial.setValue(
int(settings.value('DialValue', self.spinbox.value())))
self.spinbox.setValue(
int(settings.value('SpinBoxValue', self.spinbox.value())))
self._dial = int(settings.value('DialValue', self.spinbox.value()))
def closeEvent(self, event):
self.save_settings()
def spinbox_changed(self, value):
self.dial.setEnabled(True)
self.dial.setMinimum(value - 5)
self.dial.setMaximum(value + 5)
self.dial.setValue(value)
def dial_changed(self, value):
self.statusbar.showMessage(f'dial.value = {value}')
if __name__ == '__main__':
app = QApplication(sys.argv)
app.setFont(QFont("Times", 12, QFont.Bold))
window = New()
window.show()
sys.exit(app.exec_())
RButton_Dial_save.ui
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>800</width>
<height>600</height>
</rect>
</property>
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="maximumSize">
<size>
<width>800</width>
<height>600</height>
</size>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<property name="styleSheet">
<string notr="true"/>
</property>
<widget class="QWidget" name="centralwidget">
<property name="minimumSize">
<size>
<width>800</width>
<height>600</height>
</size>
</property>
<property name="maximumSize">
<size>
<width>800</width>
<height>600</height>
</size>
</property>
<widget class="QGroupBox" name="groupBox">
<property name="geometry">
<rect>
<x>10</x>
<y>30</y>
<width>761</width>
<height>211</height>
</rect>
</property>
<property name="title">
<string>GroupBox</string>
</property>
<widget class="QDial" name="dial">
<property name="enabled">
<bool>false</bool>
</property>
<property name="geometry">
<rect>
<x>200</x>
<y>130</y>
<width>100</width>
<height>55</height>
</rect>
</property>
<property name="wrapping">
<bool>false</bool>
</property>
<property name="notchesVisible">
<bool>true</bool>
</property>
</widget>
<widget class="QPushButton" name="Exit">
<property name="geometry">
<rect>
<x>650</x>
<y>30</y>
<width>93</width>
<height>28</height>
</rect>
</property>
<property name="text">
<string>Выход</string>
</property>
</widget>
<widget class="QSpinBox" name="spinbox">
<property name="geometry">
<rect>
<x>300</x>
<y>20</y>
<width>111</width>
<height>22</height>
</rect>
</property>
</widget>
<widget class="QRadioButton" name="radioButton">
<property name="geometry">
<rect>
<x>230</x>
<y>90</y>
<width>95</width>
<height>20</height>
</rect>
</property>
<property name="text">
<string>RButton 1</string>
</property>
</widget>
<widget class="QRadioButton" name="radioButton_2">
<property name="geometry">
<rect>
<x>420</x>
<y>90</y>
<width>95</width>
<height>20</height>
</rect>
</property>
<property name="text">
<string>RButton 2</string>
</property>
</widget>
</widget>
</widget>
<widget class="QStatusBar" name="statusbar"/>
</widget>
<resources/>
<connections/>
</ui>
我不会将来自不同对象的信号绑定到一个插槽中。试试这样: