使用效果
代码
import cv2
import osfrom ui.imageQuery import Ui_DialogImageQuery
from utils.log_util import log_message
from utils.sys_util import create_dirfrom PySide6.QtWidgets import QApplication, QDialog, QGraphicsPixmapItem, QGraphicsScene
from PySide6.QtCore import Qt, QModelIndex, QDate
from PySide6.QtGui import QMouseEvent, QPixmap, QWheelEvent, QStandardItemModel, QStandardItem, QImageclass ImageQueryDialog(QDialog, Ui_DialogImageQuery):def __init__(self):super(ImageQueryDialog, self).__init__()self.setupUi(self)self.setWindowFlags(Qt.FramelessWindowHint)self.setStyleSheet('border: 1px solid #676767')self.setWindowMiddle()self.setMouseTracking(True)self.dragStartPosition = Noneself.edtDate.setStyleSheet('QDateEdit{color:white}')self.btnQuery.setStyleSheet('QPushButton{border: 0px;font-size:18px}')self.edtDate.setDate(QDate.currentDate())try:self.initImageListViewer()except Exception as e:log_message('initImageListViewer() error: %s' % e.__cause__)self.tvImageView.clicked.connect(self.clickLoadImage)self.btnQuery.clicked.connect(self.initImageListViewer)self.btnExit.setStyleSheet('border: 0px;font-size: 20px')self.btnExit.clicked.connect(self.close)def initImageListViewer(self):self.today = self.edtDate.date().toString('yyyyMMdd')self.model = QStandardItemModel(0, 3)self.model.setHeaderData(0, Qt.Horizontal, '图片')self.model.setHeaderData(1, Qt.Horizontal, '测量值')self.model.setHeaderData(2, Qt.Horizontal, '文件名')self.tvImageView.setModel(self.model)self.tvImageView.setColumnWidth(0, 130)self.tvImageView.setColumnWidth(1, 200)self.tvImageView.setColumnWidth(2, 0)today_path = 'data/' + self.todaycreate_dir(today_path)data_list = sorted(os.listdir(today_path), reverse=True)self.lblNoData.setVisible(len(data_list) == 0)for data in data_list:if data.split('.')[1] != 'data':continuewith open(today_path + '/' + data, 'r') as f:line = f.readline()if line == '':continuedict_data = eval(line)value = dict_data['value']filename1 = dict_data['filename1']filename2 = dict_data['filename2'].split('.')[0]self.addItem('images/' + self.today + '/' + filename1.split('.')[0] + 'm.jpg','杯身高度:' + str(value[0]) + '\n时间:' + get_time_from_filename(filename1), filename1)self.addItem('images/' + self.today + '/' + filename2.split('.')[0] + 'm.jpg','\n'.join(['杯口最大直径:' + str(value[1]), '杯口最小直径:' + str(value[2]),'螺纹最大直径:' + str(value[3]), '螺纹最小直径:' + str(value[4]),'时间:' + get_time_from_filename(filename2)]), filename2)def addItem(self, image_path, description, filename):frame = cv2.imread(image_path)h, w, ch = frame.shapebytesPerLine = ch * wconvertToQtFormat = QImage(frame.data.tobytes(), w, h, bytesPerLine, QImage.Format_RGB888).rgbSwapped()pixmap = QPixmap.fromImage(convertToQtFormat)pixmap_item = QStandardItem()pixmap_item.setData(pixmap, Qt.DecorationRole)description_item = QStandardItem(description)filename_item = QStandardItem(filename)self.model.appendRow([pixmap_item, description_item, filename_item])def clickLoadImage(self, index: QModelIndex):try:row = index.row()image_name = self.model.item(row, 2).text()self.loadImage('images/' + self.today + '/' + image_name)except Exception as e:log_message('clickLoadImage() error: %s' % e.__cause__)def loadImage(self, image_file):scene = QGraphicsScene()self.graphicsView.setScene(scene)item = QGraphicsPixmapItem(QPixmap(image_file))scene.addItem(item)self.graphicsView.fitInView(item, Qt.KeepAspectRatio)def wheelEvent(self, event: QWheelEvent) -> None:if event.modifiers() & Qt.ControlModifier:factor = 1.1if event.angleDelta().y() < 0:factor = 1 / factorold_scale = self.graphicsView.transform().m11()new_scale = old_scale * factorif new_scale < 0.1 or new_scale > 1:returnself.graphicsView.scale(factor, factor)else:super().wheelEvent(event)def setWindowMiddle(self):screen = QApplication.primaryScreen()screen_geometry = screen.geometry()window_width, window_height = 1500, 960center_x = (screen_geometry.width() - window_width) // 2center_y = (screen_geometry.height() - window_height) // 2self.setGeometry(center_x, center_y - 20, window_width, window_height)def mousePressEvent(self, event: QMouseEvent) -> None:if event.button() == Qt.LeftButton:self.dragStartPosition = event.pos()def mouseMoveEvent(self, event: QMouseEvent) -> None:if self.dragStartPosition is not None:delta = event.pos() - self.dragStartPositionself.move(self.pos() + delta)def mouseReleaseEvent(self, event: QMouseEvent) -> None:if event.button() == Qt.LeftButton:self.dragStartPosition = Nonedef get_time_from_filename(filename):time_str = filename.split('.')[0]# time_str 20240929124900 转成 2024-09-29 14:49:00return (time_str[:4] + '-' + time_str[4:6] + '-' + time_str[6:8] + ' ' + time_str[8:10] + ':' +time_str[10:12] + ':' + time_str[12:14])def frmImageQueryExec():dialog = ImageQueryDialog()dialog.exec()
data文件内容:
{'filename1': '20241202142959-1.jpg', 'filename2': '20241202142959-2.jpg', 'value': ['236.952 mm', '98.734 mm', '97.934 mm', '90.052 mm', '89.211 mm'], 'result': 1}
images文件: