from tkinter.ttk import Combobox import qtawesome from PyQt5.QtCore import Qt from PyQt5.QtWidgets import QStyledItemDelegate, QStyle, QComboBox, QMessageBox, QPushButton,QStackedWidget, QLineEdit, QVBoxLayout, QHBoxLayout, QWidget, QLabel, QSplitter, QButtonGroup from utils.DBModels.DeviceParModels import * from UI.BlockParameterView import ParmView from UI.SearchAddressWidget import SearchAddressWidget from utils.DBModels.DeviceParModels import * from UI.LoadingDataWidget import LoadingDataWidget class HideTextDelegate(QStyledItemDelegate): def paint(self, painter, option, index): # 仅在下拉列表中绘制文本,避免在ComboBox显示区域绘制 if option.state & QStyle.State_Enabled: super().paint(painter, option, index) def sizeHint(self, option, index): # 返回原始大小,确保下拉列表项不受ComboBox宽度影响 return super().sizeHint(option, index) class TbCombox(QComboBox): def __init__(self): super().__init__() self.initUI() def initUI(self): # 创建ComboBox self.addItems(["选项很长很长很长", "选项2", "选项3"]) self.setEditable(True) # 设置为可编辑以应用样式表隐藏文本 self.setObjectName('tbcombox') # 设置样式隐藏文本 self.lineEdit().setStyleSheet("color: transparent; border:none; background-color: #f0f0f0") self.lineEdit().setCursor(Qt.ArrowCursor) self.lineEdit().setReadOnly(True) # 设置自定义代理以调整下拉列表行为 self.delegate = HideTextDelegate(self) self.setItemDelegate(self.delegate) # 设置ComboBox的固定大小 self.setFixedSize(30, 40) # 确保下拉列表宽度适应最长项 max_width = max(self.fontMetrics().width(self.itemText(i)) for i in range(self.count())) self.view().setMinimumWidth(max_width) class DynamicAddBlock(QHBoxLayout): def __init__(self, blocklist): super().__init__() self.blocklist = blocklist self.buttonlist = [] self.blockViewlist = [] self.initUI() def initUI(self): pbNumber = self.blocklist[0] tbNumber = self.blocklist[1] fbNumber = self.blocklist[2] for i in range(pbNumber): pblockBtn = QPushButton('物理块') pblockBtn.setCheckable(True) pblockBtn.setFixedSize(90, 43) pblockBtn.setObjectName("parameBtn") pblockBtn.setIcon(qtawesome.icon('fa.th-large', color='#1fbb6f')) # pblockBtn.setCheckable(True) pblockBtn.clicked.connect(lambda _, pbbtn = pblockBtn: self.switchParameterWidget(pbbtn)) self.addWidget(pblockBtn) self.buttonlist.append(pblockBtn) physicalBlockView = ParmView(PhysicalBlock) self.blockViewlist.append(physicalBlockView) if i == 0: pblockBtn.setChecked(True) for i in range(fbNumber): fblockBtn = QPushButton('功能块' + str(i + 1)) fblockBtn.setFixedSize(90, 43) fblockBtn.setCheckable(True) fblockBtn.setObjectName("parameBtn") fblockBtn.setIcon(qtawesome.icon('fa.th-large', color='#1fbb6f')) fblockBtn.clicked.connect(lambda _, fbbtn = fblockBtn: self.switchParameterWidget(fbbtn)) self.addWidget(fblockBtn) self.buttonlist.append(fblockBtn) aiFunctionBlockView = ParmView(AIFunctionBlock) self.blockViewlist.append(aiFunctionBlockView) for i in range(tbNumber): tbwidget = QWidget() tbwidget.setObjectName('tbwidget') tblayout = QHBoxLayout() tbwidget.setLayout(tblayout) tbcombox = TbCombox() tblockBtn = QPushButton('转换块' + str(i + 1)) tblockBtn.setObjectName("parameBtn") tblockBtn.setFixedSize(90, 43) tblockBtn.setCheckable(True) tblockBtn.setIcon(qtawesome.icon('fa.th-large', color='#1fbb6f')) tblockBtn.clicked.connect(lambda _, tbbtn = tblockBtn: self.switchParameterWidget(tbbtn)) tbcombox.currentIndexChanged.connect(lambda index, combox = tbcombox, tbbtn = tblockBtn : self.chooseTbtype(index, combox, tbbtn)) tblayout.addWidget(tblockBtn) tblayout.addWidget(tbcombox) self.addWidget(tbwidget) self.buttonlist.append(tblockBtn) pressureTBlockView = ParmView(PressureTranslationBlock) self.blockViewlist.append(pressureTBlockView) self.parameterButtonGroup = QButtonGroup() self.parameterButtonGroup.setExclusive(True) for button in self.buttonlist: self.parameterButtonGroup.addButton(button) self.parameStackWidget = QStackedWidget() for view in self.blockViewlist: self.parameStackWidget.addWidget(view) def switchParameterWidget(self, buttonType): print(buttonType) for index , button in enumerate(self.buttonlist): if button == buttonType: self.parameStackWidget.setCurrentIndex(index) def chooseTbtype(self, index, combox, button): tbType = combox.itemText(index) button.setText(tbType) class BlockParameterManageWidget(QWidget): def __init__(self): super().__init__() self.initUI() def initUI(self): blocklist = [1,2,2] self.blockLayout = DynamicAddBlock(blocklist) self.mainlayout = QVBoxLayout() self.settingLayout = QHBoxLayout() self.deviceAddressLabel = QLabel('从站地址') self.deviceAddressLabel.setObjectName('deviceAddressLabel') self.deviceAddressEdit = QLineEdit() self.deviceAddressEdit.setObjectName("deviceAddressEdit") self.confirmBtn = QPushButton('加载数据') self.confirmBtn.setIcon(qtawesome.icon('fa5s.check-circle', color='#1fbb6f')) self.confirmBtn.clicked.connect(self.refreshData) self.confirmBtn.setObjectName("parameBtn") self.deviceAddressSearchBtn = QPushButton('查找') self.deviceAddressSearchBtn.setIcon(qtawesome.icon('fa.search', color='#1fbb6f')) self.deviceAddressSearchBtn.clicked.connect(self.searchAddress) self.deviceAddressSearchBtn.setObjectName("parameBtn") self.settingLayout.addWidget(self.deviceAddressLabel, 1) self.settingLayout.addWidget(self.deviceAddressEdit, 1) self.settingLayout.addWidget(self.confirmBtn, 1) self.settingLayout.addWidget(self.deviceAddressSearchBtn, 1) # self.settingLayout.addLayout(self.blockLayout) self.splitter = QSplitter() self.settingLayout.addWidget(self.splitter, 18) self.mainlayout.addLayout(self.settingLayout,1) self.widget = QWidget() self.mainlayout.addWidget(self.widget, 20) # self.proxy = QtCore.QSortFilterProxyModel(self) # self.parameTableView.proxy = self.proxy # self.proxy.setSourceModel(self.parameTableView.model) # self.parameTableView.setModel(self.proxy) # datas = PressureTranslationBlock.getallParame() # for index, data in enumerate(datas): # desc = data[2].replace('\r\n', '').replace('\n', '') # lines = [desc[i:i+50] + "\r\n" for i in range(0, len(desc), 50)] # # 合并列表为一个字符串,移除最后一个换行符 # result = "".join(lines).rstrip("\r\n") # data[2] = result # data = data + ['', '', ''] # self.parameTableView.model.append_data(data) # self.parameTableView.setVerticalHeader(ParamsVHeader(self, self.parameTableView.model.datas)) self.setLayout(self.mainlayout) def refreshData(self): if self.deviceAddressEdit.text(): self.splitter.deleteLater() self.widget.deleteLater() self.loadingDataWidget = LoadingDataWidget() self.loadingDataWidget.loadData() self.settingLayout.addLayout(self.blockLayout) self.settingLayout.addWidget(QSplitter(), 18) self.mainlayout.addWidget(self.blockLayout.parameStackWidget, 20) model = self.blockLayout.parameStackWidget.currentWidget().model model.updateColumn(5, '查询中...') else: reply = QMessageBox.question(self.parent(), '警告', "请输入从站地址", QMessageBox.Yes) return def searchAddress(self): self.searchAddressWidget = SearchAddressWidget(self.deviceAddressEdit) self.searchAddressWidget.show()