记录偶尔使用到的,方便快速查到,免得之后去查半天。
1.pyqt+qtdesingner+pycharm配置
先期条件:安装好了conda,有conda的虚拟环境。
pip insatll pyqt5
pip insatll pyqt5-tools
designer.exe的安装位置在 conda\envs\pytorch-1.0\Lib\site-packages\qt5_applications\Qt\bin
pycharm 的配置:https://blog.csdn.net/u013044310/article/details/80777840
因为有图片和声音资源文件也要将.qrc转成.py :https://blog.csdn.net/qq_38187188/article/details/104643487
---------------------------------------------------------------------------------------------------------使用记录------------------------------------------------------------------------------------------------------------
1.放置圆形按钮(图案),直接将背景设置为透明。
background-color: rgba(0,0,0,0);#前三个参数为rgb,最后一个为透明度。
2.使用pyaudio
# 没有读完整个文件,声音一直播放的
chunk = 1024 # 2014kb
wf = wave.open(self.file, 'rb')
p = pyaudio.PyAudio()
stream = p.open(format=p.get_format_from_width(wf.getsampwidth()), channels=wf.getnchannels(),
rate=wf.getframerate(), output=True)
data = wf.readframes(chunk) # 读取数据
print("start------------------")
while data != b'': # 播放
stream.write(data)
data = wf.readframes(chunk)
#print('while循环中!')
print("over---------------------------")
stream.stop_stream() # 停止数据流
stream.close()
self.signal_flag = True
p.terminate() # 关闭 PyAudio
3.在Qlabel的图片上画矩形和画线
思路是不使用界面上的label,需要自己重新写一个label类。然后再在ui转python的文件里面添加上label类,就可以正常使用了。
参考连接:https://github.com/PeterZhongHi/SmartDog
同样的思路,qt上也可以实现。https://github.com/chengyangkj/VideoRoi
重写Qlabel:
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()
将Qlabel类,添加到ui转py的文件里面,这个缺点是之后修改界面后生成的ui_py文件无法直接使用。
# add Qlabel
self.label_show = Label(self.centralwidget)
self.label_show.setGeometry(QtCore.QRect(10, 10, 1280, 720))
self.label_show.setMinimumSize(QtCore.QSize(1280, 720))
self.label_show.setMaximumSize(QtCore.QSize(1280, 720))
self.label_show.setStyleSheet("background-color: rgb(255, 255, 255);")
self.label_show.setText("")
self.label_show.setObjectName("label_show")
正常使用自己写的Qlabel 就行
resize_frame = cv2.resize(frame, (self.frame_width, self.frame_height), interpolation=cv2.INTER_AREA)
show_frame = cv2.cvtColor(resize_frame, cv2.COLOR_BGR2RGB)
showImage = QImage(show_frame.data, show_frame.shape[1], show_frame.shape[0], QImage.Format_RGB888)
self.label_show.setPixmap(QPixmap.fromImage(showImage))
# 开始画线来打开绘制事件,画线完成关闭绘制事件
# 开始画线
def slot_start_draw(self):
try:
self.line_point = []
self.label_show.open_mouse_flag = True
except:
traceback.print_exc()
# 画线完成
def slot_draw_done(self):
try:
self.label_show.open_mouse_flag = False
self.line_point.append(self.label_show.x0)
self.line_point.append(self.label_show.y0)
self.line_point.append(self.label_show.x1)
self.line_point.append(self.label_show.y1)
print(self.line_point)
self.textBrowser.textCursor().insertText(str(self.line_point))
except:
traceback.print_exc()