微信公众号:OpenCV开发者联盟
关注获取更多计算机视觉与深度学习知识
引言
QSpinBox调节组件
spinbox = QSpinBox()
spinbox = QDoubleSpinBox()
spinbox.setRange(min, max)
spinbox..setSingleStep(0.01)
spinbox.setSingleStep(n)
spinbox.setDecimals(prec)
QComboBox选择组件
combox = QComboBox()
combox.addItem(”选择一”)
代码演示
1import cv2 as cv
2from PyQt5 import QtWidgets, QtCore, QtGui
3import sys
4
5
6class SpinBoxComBoxDemoPanel(QtWidgets.QWidget):
7 def __init__(self, parent=None):
8 super().__init__(parent)
9 # 文本标签
10 self.combox = QtWidgets.QComboBox()
11 self.combox.addItem("原图")
12 self.combox.addItem("模糊")
13 self.combox.addItem("灰度")
14 self.combox.setCurrentIndex(0)
15
16 hbox_layout1 = QtWidgets.QHBoxLayout()
17 hbox_layout1.addWidget(QtWidgets.QLabel("选择:"))
18 hbox_layout1.addWidget(self.combox)
19 hbox_layout1.addStretch(1)
20
21 panel1 = QtWidgets.QGroupBox("单选演示")
22 panel1.setLayout(hbox_layout1)
23
24 self.spinbox1 = QtWidgets.QSpinBox()
25 self.spinbox1.setRange(0, 100)
26 self.spinbox1.setSingleStep(1)
27 self.spinbox2 = QtWidgets.QSpinBox()
28 self.spinbox2.setRange(3, 50)
29 self.spinbox2.setSingleStep(2)
30
31 hbox_layout2 = QtWidgets.QHBoxLayout()
32 hbox_layout2.addWidget(QtWidgets.QLabel("窗口:"))
33 hbox_layout2.addWidget(self.spinbox1)
34 hbox_layout2.addWidget(QtWidgets.QLabel("方差:"))
35 hbox_layout2.addWidget(self.spinbox2)
36 hbox_layout2.addStretch(1)
37
38 panel2 = QtWidgets.QGroupBox("高斯模糊参数")
39 panel2.setLayout(hbox_layout2)
40
41 panel3 = QtWidgets.QWidget()
42 hbox_layout3 = QtWidgets.QHBoxLayout()
43 hbox_layout3.addWidget(panel1)
44 hbox_layout3.addWidget(panel2)
45 panel3.setLayout(hbox_layout3)
46
47 # 输入文本框
48 self.label = QtWidgets.QLabel()
49 pixmap = QtGui.QPixmap("images/16.jpg")
50 pix = pixmap.scaled(QtCore.QSize(620, 500), QtCore.Qt.KeepAspectRatio)
51 self.label.setPixmap(pix)
52 self.label.setAlignment(QtCore.Qt.AlignCenter)
53 self.label.setStyleSheet("background-color:black; color: green")
54
55 # 添加到布局管理器中
56 vbox_layout = QtWidgets.QVBoxLayout()
57 vbox_layout.addWidget(panel3)
58 vbox_layout.addWidget(self.label)
59 vbox_layout.addStretch(1)
60
61 self.spinbox1.setEnabled(False)
62 self.spinbox2.setEnabled(False)
63
64 # 面板容器
65 self.setLayout(vbox_layout)
66
67 # setup listener
68 self.combox.currentIndexChanged.connect(self.on_select_changed)
69 self.spinbox2.valueChanged.connect(self.on_update_blur)
70
71 def on_select_changed(self):
72 index = self.combox.currentIndex()
73 self.spinbox1.setEnabled(False)
74 self.spinbox2.setEnabled(False)
75 if index == 0:
76 self.on_update_original()
77 if index == 1:
78 self.spinbox1.setEnabled(True)
79 self.spinbox2.setEnabled(True)
80 self.on_update_blur()
81 if index == 2:
82 self.on_update_gray()
83
84 def on_update_original(self):
85 pixmap = QtGui.QPixmap("images/16.jpg")
86 pix = pixmap.scaled(QtCore.QSize(620, 500), QtCore.Qt.KeepAspectRatio)
87 self.label.setPixmap(pix)
88
89 def on_update_blur(self):
90 image = cv.imread("images/16.jpg")
91 sigma = self.spinbox2.value()
92 win_size = self.spinbox1.value()
93 bgr = cv.GaussianBlur(image, (win_size, win_size), sigma)
94 dst = cv.cvtColor(bgr, cv.COLOR_BGR2RGB)
95 height, width, channel = dst.shape
96 bytesPerLine = 3 * width
97 img = QtGui.QImage(dst.data, width, height, bytesPerLine, QtGui.QImage.Format_RGB888)
98 pixmap = QtGui.QPixmap(img)
99 pix = pixmap.scaled(QtCore.QSize(620, 500), QtCore.Qt.KeepAspectRatio)
100 self.label.setPixmap(pix)
101
102 def on_update_gray(self):
103 image = cv.imread("images/16.jpg")
104 gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
105 dst = cv.cvtColor(gray, cv.COLOR_GRAY2RGB)
106 height, width, channel = dst.shape
107 bytesPerLine = 3 * width
108 img = QtGui.QImage(dst.data, width, height, bytesPerLine, QtGui.QImage.Format_RGB888)
109 pixmap = QtGui.QPixmap(img)
110 pix = pixmap.scaled(QtCore.QSize(620, 500), QtCore.Qt.KeepAspectRatio)
111 self.label.setPixmap(pix)
总结
本文主要是演示了PyQT5的QSpinBox与QComboBox两种控件使用,从创建、Group方式排版布局,事件响应处理等等细节全面介绍了它们的使用。
扫码关注
OpenCV开发者联盟,
专注各种语言的OpenCV开发教程分享
OpenCV周边开发技术应用!
扫码查看OpenCV+OpenVIO+Pytorch系统化学习路线图
推荐阅读
CV全栈开发者说 - 从传统算法到深度学习怎么修炼
2022入坑深度学习,我选择Pytorch框架!
Pytorch轻松实现经典视觉任务
教程推荐 | Pytorch框架CV开发-从入门到实战
OpenCV4 C++学习 必备基础语法知识三
OpenCV4 C++学习 必备基础语法知识二
OpenCV4.5.4 人脸检测+五点landmark新功能测试
OpenCV4.5.4人脸识别详解与代码演示
OpenCV二值图象分析之Blob分析找圆
OpenCV4.5.x DNN + YOLOv5 C++推理
OpenCV4.5.4 直接支持YOLOv5 6.1版本模型推理
OpenVINO2021.4+YOLOX目标检测模型部署测试
比YOLOv5还厉害的YOLOX来了,官方支持OpenVINO推理