You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
PROFIBUS/UI/SearchAddressWidget.py

72 lines
2.5 KiB
Python

from UI.DeviceDialogWidget import DeviceDialog
import sys
import re
import random
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import Qt, QTimer
from PyQt5.Qt import *
from PyQt5.QtWidgets import QHBoxLayout, QVBoxLayout, \
QApplication, QLineEdit, QWidget, QTableWidget, QSplitter,QVBoxLayout,QPushButton, QProgressBar, QTableWidgetItem, QMessageBox
from PyQt5.QtWidgets import QDialog, QListWidgetItem
from protocol.ModBus.DPV1Master import DPV1Master
class SearchAddressWidget(QWidget):
def __init__(self, deviceAddressEdit):
super().__init__()
self.selected_value = None
self.deviceAddressEdit = deviceAddressEdit
self.initUI()
def initUI(self):
self.resize(800, 500)
self.setObjectName('deviceDialog')
self.addressListWidget = QListWidget()
self.mainlayout = QVBoxLayout()
self.progressBar = QProgressBar(self)
self.bottomLayout = QHBoxLayout()
self.confirmBtn = QPushButton('确定')
self.confirmBtn.clicked.connect(self.onConfirm)
self.cancelBtn = QPushButton('取消')
self.bottomLayout.addWidget(self.confirmBtn)
self.bottomLayout.addWidget(QSplitter())
self.bottomLayout.addWidget(self.cancelBtn)
self.mainlayout.addWidget(self.progressBar)
self.mainlayout.addWidget(self.addressListWidget)
self.mainlayout.addLayout(self.bottomLayout)
self.setLayout(self.mainlayout)
self.setWindowTitle('从站地址查找')
test = DPV1Master('192.168.3.10', 502)
test.searchMaster(self.updateProgress)
# self.timer = QTimer()
# self.timer.timeout.connect(self.updateProgress)
# self.timer.start(500)
def updateProgress(self, address, isSlave):
# 模拟一个任务,逐步更新进度条
self.progressBar.setValue(int(address * (100/125)))
if isSlave:
self.addressListWidget.addItem(QListWidgetItem(str(address)))
# QApplication.processEvents() # 确保UI及时更新
# QTimer.singleShot(100, lambda: None) # 暂停一小段时间,模拟任务执行时间
def onCellClicked(self, row, column, double = False):
item = self.addressListWidget.item(row, column)
if item:
self.address = item.text()
if double:
self.onConfirm()
def onConfirm(self):
self.deviceAddressEdit.setText(self.address)
self.close()