神剑山庄资源网 Design By www.hcban.com
问题背景
上图中的红色框框就是在QLabel
的基础上实现的功能。
实现思路
"htmlcode">
from PyQt5.QtWidgets import QLabel from PyQt5.QtCore import Qt,QRect from PyQt5.QtGui import QPainter,QPen class Label(QLabel): x0=0 y0=0 x1=0 y1=0 open_mouse_flag=False select_roi_flag=False draw_roi_flag=False clear_flag=False rect = QRect() #按下鼠标 def mousePressEvent(self, event): if self.open_mouse_flag is True: self.select_roi_flag=True self.x0=event.x() self.y0=event.y() #释放鼠标 def mouseReleaseEvent(self, event): self.select_roi_flag=False #移动鼠标 def mouseMoveEvent(self, event): if self.select_roi_flag is True: self.x1=event.x() self.y1=event.y() if self.draw_roi_flag is True: self.update() #绘制事件 def paintEvent(self,event): super().paintEvent(event) painter = QPainter(self) painter.setPen(QPen(Qt.red, 5, Qt.SolidLine)) if self.clear_flag is True: self.x0=0 self.y0=0 self.x1=0 self.y1=0 self.rect = QRect(self.x0, self.y0, abs(self.x1 - self.x0), abs(self.y1 - self.y0)) painter.drawRect(self.rect) self.update()
其他要注意的问题
"htmlcode">
# 清除label对象的绘制内容 def clear_label(self): self.label_show.clear_flag = True self.label_show.clear()
"htmlcode">
# 重写键盘事件 def keyPressEvent(self, QKeyEvent): if self.open_keyboard_flag is True: # 当键盘事件为真的是才有键盘事件监控 if QKeyEvent.key() == Qt.Key_S: self.label_show.setCursor(Qt.CrossCursor) # 切换游标为十字型 self.label_show.open_mouse_flag = True self.label_show.draw_roi_flag = True if QKeyEvent.key() == Qt.Key_Q: # 按下'q'键键盘监控关闭 self.label_show.unsetCursor() self.label_show.draw_roi_flag = False self.label_show.open_mouse_flag = False self.open_keyboard_flag = False
神剑山庄资源网 Design By www.hcban.com
神剑山庄资源网
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
神剑山庄资源网 Design By www.hcban.com
暂无使用PyQt的QLabel组件实现选定目标框功能的方法示例的评论...
更新日志
2024年09月30日
2024年09月30日
- 孙悦.1996-伙伴【正大国际】【WAV+CUE】
- 纪钧瀚《钢琴阅读时光 雨中书店聆听轻音乐》[FLAC/分轨][399.62MB]
- 证声音乐图书馆《走向自然 疗心爵士乐》[320K/MP3][87.4MB]
- 证声音乐图书馆《走向自然 疗心爵士乐》[FLAC/分轨][184.94MB]
- 陈慧娴.2018-Priscilla-Ism演唱会3CD(2024环球红馆40复刻系列)【环球】【WAV+CUE】
- 郑秀文.1999-我应该得到(国)【华纳】【WAV+CUE】
- 陈家慧.2011-钢琴酒吧2CD【龙吟唱片】【WAV+CUE】
- 证声音乐图书馆《雨季 蓝调吉他 Rainy Blues》[320K/MP3][45.01MB]
- 证声音乐图书馆《雨季 蓝调吉他 Rainy Blues》[FLAC/分轨][109.13MB]
- 赞多《序章》[320K/MP3][45.54MB]
- 许巍.2004-每一刻都是崭新的【步升大风】【WAV+CUE】
- 群星.2024-四方馆影视原声带【韶愔音乐】【FLAC分轨】
- 陈雷.1997-安锁咧【金圆唱片】【WAV+CUE】
- 关淑怡.2013-MY.FAVORITE.SK.3CD【环球】【WAV+CUE】
- Sweety.2006-花言乔语【丰华】【WAV+CUE】