from PyQt5.QtCore import Qt from PyQt5.QtWidgets import QWidget, QComboBox, QListView, QHBoxLayout, QVBoxLayout from UI.Setting.SearchDeviceWidget import DeviceMasterWidget, DeviceSlaveWidget class CustomBox(QComboBox): def __init__(self): super(CustomBox, self).__init__() self.setCursor(Qt.PointingHandCursor) self.setView(QListView()) def showPopup(self): super(CustomBox, self).showPopup() pop = self.children()[1] pop.move(pop.x(), pop.y() + 51) class RemoteConnectSettingWidget(QWidget): BORDER_WIDTH = 5 def __init__(self): super().__init__() # 主布局改为垂直布局(包含ComboBox和内容区) self.mainLayout = QVBoxLayout(self) self.mainLayout.setObjectName("mainLayout") # 添加模式选择 ComboBox(水平居中) self.modeComboBox = QComboBox() self.modeComboBox.addItem("主站模式") self.modeComboBox.addItem("从站模式") self.modeComboBox.setObjectName("setBox") self.modeComboBox.currentTextChanged.connect(self.switchMode) # 将ComboBox放在顶部并水平居中 hboxCombo = QHBoxLayout() hboxCombo.addStretch() # 左侧弹簧 hboxCombo.addWidget(self.modeComboBox) hboxCombo.addStretch() # 右侧弹簧 self.mainLayout.addLayout(hboxCombo) self.contentLayout = QHBoxLayout() # 存储两种模式的Widget self.deviceMasterWidget = DeviceMasterWidget() # 主站模式 self.deviceSlaveWidget = DeviceSlaveWidget() # 从站模式 # 初始显示主站模式 self.currentWidget = self.deviceMasterWidget self.contentLayout.addStretch() # 左侧弹簧 self.contentLayout.addWidget(self.currentWidget) self.contentLayout.addStretch() # 右侧弹簧 self.mainLayout.addLayout(self.contentLayout) self.mainLayout.addStretch() # 底部弹簧(将内容向上推) def switchMode(self, mode): """切换主站/从站模式,自动关闭另一端""" # 关闭当前Widget对应的服务/客户端 if self.currentWidget == self.deviceMasterWidget: # 主站切换到从站,关闭服务端 from utils import Globals protocolManage = Globals.getValue('protocolManage') if protocolManage and hasattr(protocolManage, 'closeServer'): protocolManage.closeServer() else: # 从站切换到主站,关闭客户端 from utils import Globals protocolManage = Globals.getValue('protocolManage') if protocolManage and hasattr(protocolManage, 'closeClient'): protocolManage.closeClient() # 移除当前Widget self.contentLayout.removeWidget(self.currentWidget) self.currentWidget.hide() # 根据选择显示对应的Widget if mode == "主站模式": self.currentWidget = self.deviceMasterWidget else: self.currentWidget = self.deviceSlaveWidget # 重新添加Widget(保持居中) self.contentLayout.insertWidget(1, self.currentWidget) # 插入到两个弹簧中间 self.currentWidget.show()