您好,登錄后才能下訂單哦!
這篇文章主要介紹“PyQt5 PySide2觸摸測試功能如何實現”的相關知識,小編通過實際案例向大家展示操作過程,操作方法簡單快捷,實用性強,希望這篇“PyQt5 PySide2觸摸測試功能如何實現”文章能幫助大家解決問題。
該測試功能是Linux產測軟件的一個子功能,主要涉及:
140行代碼
PySide2的Event、信號和槽、QLabel,QWidget。
QLabel實現每個小框,QWidget作為主界面
另外發現PySide2和Pyqt5沒啥大區別,只要把import的包改好,代碼是可以兩方通用的。
手指滑動,手指坐標所在方塊的顏色發生改變,如果手指劃出方塊區域,則所有已染色方塊清空顏色,松開事件同理
實現效果:
1.使用GridLayout,繪制四周方格,方格初始化為黃色,點擊到則為紅色
2.重寫Label,對每個label定義x,y表示所在gridlayout中的位置
如x = 1,y = 3表示label位于第一行第三列,left,right,top,bottom表示方格四條邊的實際坐標
3.重寫moveEvent事件和ReleaseEvent事件,
moveEvnet事件:
每次鼠標移動即觸發moveEvent,則發出已經定義的信號move_signal
move_singal.connect(self.manager_touch)
manager_touch是判斷當前鼠標是否位于方格內,如果在方格內則染色,如果鼠標劃入空白部分,則清空所有方塊顏色
另對染色label進行計數,如果達到繪制的label的總數量則發出信號返回測試通過
ReleaseEvent事件:
鼠標左鍵松開(對應手指離開屏幕)即觸發ReleaseEvent,則清空已染色方塊,另把已染色方塊數量清零
4.clear_sources: 在退出頁面前應將保存label的列表即self.touch_labels清空,否則重啟該界面會報錯
sytle.py
COLOR_RED = "color: rgb(255, 255, 255); background-color: #FF0000;" COLOR_YELLOW = "color: rgb(255, 255, 255); background-color: #FFFF00;" COLOR_WHITE = "color: rgb(255, 255, 255); background-color: #FFFFFF;" COLOR_BLACK = "color: rgb(255, 255, 255); background-color: #000000;" COLOR_GREEN = "color: rgb(255, 255, 255); background-color: #00FF00;" COLOR_BLUE = "color: rgb(255, 255, 255); background-color: #0000FF;" COLOR_PURPLE = "color: rgb(255, 255, 255); background-color: #871F78;"
main.py
import sys from functools import partial from PySide2 import QtWidgets from PySide2.QtCore import Qt, Signal from PySide2.QtGui import QCursor from PySide2.QtWidgets import QGridLayout, QPushButton, QWidget, QApplication import style class TouchLabel(QtWidgets.QLabel): def __init__(self, i, j, top, bottom, left, right, target, parent=None): super(TouchLabel, self).__init__(parent) self.setStyleSheet(style.COLOR_YELLOW) self.flag = False # 如果被滑過則置為True self.target = target self.x = i # x,y 代表在gridLayout中的位置 self.y = j self.flag = False self.left = left self.right = right self.top = top self.bottom = bottom class TouchWidget(QWidget): touch_labels = [] # 存儲label的列表 sum_moved_labels = 0 move_signal = Signal() out_signal = Signal() release_signal = Signal() def __init__(self): super().__init__() self.sum_labels = 0 # 統計染色的方塊個數 self.target = 0 print("start TouchWidget") self.setWindowFlags(Qt.FramelessWindowHint) self.showFullScreen() self.init_parameters() # 初始化gridlayout的參數 print("繪制的按鈕個數為:" + str(self.target)) def init_parameters(self): self.layout = QGridLayout(self) self.layout.setMargin(0) # 設置widget離窗口的距離 self.layout.setSpacing(0) # 設置控件間距 self.row = 10 # 縱向的按鈕數量 self.column = 10 # 橫向的按鈕數量 desktop = QtWidgets.QApplication.desktop() global label_width, label_height label_width = desktop.width() / self.column # 列寬 label_height = desktop.height() / self.row # 行高 self.target = self.row * 2 + self.column * 2 - 4 self.touch_labels.clear() self.init_touch_label(self.row, self.column, label_height, label_width) self.add_touch_label(label_width, label_height) def init_touch_label(self, row, column, label_height, label_width): desktop = QtWidgets.QApplication.desktop() height = desktop.height() width = desktop.width() for i in range(row): for j in range(column): if i == 0 and j == 0: label = TouchLabel(i, j, 0, label_height, 0, label_width, self.target) self.touch_labels.append(label) elif i == 0 and j != 0: label = TouchLabel(i, j, 0, label_height, j * label_width, (j + 1) * label_width, self.target) self.touch_labels.append(label) elif i != 0 and j == 0: label = TouchLabel(i, j, i * label_height, (i + 1) * label_height, 0, label_width, self.target) self.touch_labels.append(label) elif i == row - 1 and j != 0: label = TouchLabel(i, j, height - label_height, height, j * label_width, (j + 1) * label_width, self.target) self.touch_labels.append(label) elif j == column - 1 and i != 0: label = TouchLabel(i, j, i * label_height, (i + 1) * label_height, width - label_width, width, self.target) self.touch_labels.append(label) def add_touch_label(self, label_width, label_height): for label in self.touch_labels: self.layout.addWidget(label, label.x, label.y, 1, 1) nopass_quit_btn = QPushButton("如果測試不通過,點擊此按鈕退出") nopass_quit_btn.setMinimumSize(label_width, label_height) self.layout.addWidget(nopass_quit_btn, self.row / 2 - 1, self.column / 2 - 1, 3, 3) nopass_quit_btn.clicked.connect(partial(self.on_result, "TEST NOT PASS")) self.move_signal.connect(self.manager_touch) self.out_signal.connect(partial(self.on_result, "TEST PASS")) self.release_signal.connect(self.clear_label_status) def manager_touch(self): desktop = QtWidgets.QApplication.desktop() frontier_top = label_height frontier_bottom = desktop.height() - label_height frontier_left = label_width frontier_right = desktop.width() - label_width x = QCursor.pos().x() y = QCursor.pos().y() if frontier_left <= x <= frontier_right and frontier_top <= y <= frontier_bottom: self.clear_label_status() print("手指劃出邊界,清空所有方塊顏色") for label in self.touch_labels: if label.left <= x <= label.right and label.top <= y <= label.bottom: if label.flag: continue else: label.setStyleSheet(style.COLOR_RED) TouchWidget.sum_moved_labels += 1 label.flag = True if TouchWidget.sum_moved_labels == self.target: self.out_signal.emit() def clear_label_status(self): for label in self.touch_labels: label.setStyleSheet(style.COLOR_YELLOW) label.flag = False TouchWidget.sum_moved_labels = 0 def clear_sources(self): TouchWidget.sum_moved_labels = 0 self.touch_labels.clear() self.sum_moved_labels = 0 self.target = 0 def mouseMoveEvent(self, event): self.move_signal.emit() def mouseReleaseEvent(self, event): self.release_signal.emit() print("釋放焦點,清空所有方塊顏色") def on_result(self, ret): self.clear_sources() print(ret) self.close() if __name__ == '__main__': app = QApplication() widget = TouchWidget() widget.show() sys.exit(app.exec_())
關于“PyQt5 PySide2觸摸測試功能如何實現”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業相關的知識,可以關注億速云行業資訊頻道,小編每天都會為大家更新不同的知識點。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。