PyQt5 自我学习总结
1.widget,mainwindow,dialog的区别
2.关于布局时空间之间的距离以及空间的大小
主要通过layoutstrech和layoutspacing调整空间的间距和相对大小。
除此以外,还需要设置每个控件的最小宽度和最小高度。如下图
3.界面最大化时控件随之缩放
在完成布局以后,点击这个按钮(layout in grid)就可以达到这个目的。
如果觉得(layout in grid)之后的界面四周边距太窄,可以通过如图所示的参数调整
4.最简单的main函数
from PyQt5 import QtWidgets #这里我的ui文件生成的python文件名为ui.py,因此使用ui
import ui
import sys
class MyWindow(QtWidgets.QMainWindow, ui.Ui_MainWindow):
def __init__(self):
super(MyWindow, self).__init__()
self.setupUi(self)
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
myshow = MyWindow()
myshow.show()
sys.exit(app.exec_())1
2
3
4
5
6
7
8
9
10
11
12
13
14
这段代码是执行图形界面的最简单的程序,运行这段代码就可以运行编辑好的ui界面。但是这之前需要对.ui文件执行pyuic生成.py文件。
5.设置菜单栏的槽函数
为了实现点击菜单栏的某个菜单,然后执行对应的动作,就需要为其设置槽函数。
如上图所示,从上至下依次执行圈住的三个步骤。点击edit以后在slot一栏里添加槽函数open_file。我这里是为了到时候点击file,能够自动执行槽函数open_file()。
添加完毕以后点击cancel就可以。
然后在designer的右下角会有如图所示的信号/槽编辑。sender是发送器,点击可以选择你想要发送信号的控件;signal是信号类型;receiver是接收器,slot是对应的槽函数,这里我们选择之前编辑好的open_file
之后保存ui文件,重新执行pyuic。
在主函数里编辑对应的open_file()函数。
6.打开文件对话框
提示:
from PyQt5.QtWidgets import QFileDialog
filename, _ = QFileDialog.getOpenFileName(self, ‘Open file’, ‘./’)
详细内容可依此进行百度。
7.在mainwindow里调用另一个widget窗口
同理先制作好另一个widget窗口,并进行pyuic得到py文件。
为这个widget窗口写个类,类似于上述简单的main函数,并把这个类的声明和main函数放在一起
from PyQt5 import QtWidgets #这里我的ui文件生成的python文件名为ui.py,因此使用ui
import ui
import ui2
import sys
class widget1(QtWidgets.QMainWindow, Ui_Widgets):
def __init__(self):
super(widget1, self).__init__()
self.setupUi(self)
class MyWindow(QtWidgets.QMainWindow, ui.Ui_MainWindow): #主窗口
def __init__(self):
super(MyWindow, self).__init__()
self.setupUi(self)
def slot(self): # 这个函数是某个槽函数,比如某个按钮的槽函数,点击按钮打开新的widget窗口
self.child1 = widget1() #这里必须使用self,否则槽函数执行完毕以后窗口就关掉了
self.child1.show() #注意括号里没有self
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
myshow = MyWindow()
myshow.show()
sys.exit(app.exec_())1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
7.使用Qgraphicsview显示图像
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
import cv2
srcImage = cv2.imread(str(filename))
self.Mat_dict['srcImage'] = srcImage
img = cv2.cvtColor(srcImage, cv2.COLOR_BGR2RGB)
y, x, dim = img.shape # 获取图像大小
if dim == 4:
frame = QImage(img, x, y, x * 4, QImage.Format_RGB32)
elif dim == 3:
frame = QImage(img, x, y, x * 3, QImage.Format_RGB888)
elif dim == 1:
frame = QImage(img, x, y, x * 1, QImage.Format_Grayscale8)
else:
raise Exception("image channels must be 1,3,4")
pix = QPixmap.fromImage(frame)
item = QGraphicsPixmapItem(pix) # 创建像素图元
scene = QGraphicsScene() # 创建场景
scene.addItem(item)
self.graphicsView.setScene(scene)1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
更详细的介绍在https://www.cnblogs.com/cy568searchx/p/3502242.html
这里有很重要的一部分就是对于不同的通道数,使用不同的参数讲cv类型的数据转换成QImage类型的数据
8.多个文件可以共用的全局变量
9.在两个窗口之间传递信号并处触发槽函数
比如在子窗口中点击按钮,希望在主窗口中执行对应的函数
from PyQt5.QtCore import *
class subwindow(Qmainwindow, ui_subwindow):
mysignal = pyqtSignal(int)
def _init_(self):
pass
def func(self):
self.mysignal.emit(0) #发射信号
class mywindow(Qmainwindow, ui_subwindow):
...
def slot(self): # 这个函数是某个槽函数,比如某个按钮的槽函数,点击按钮打开新的widget窗口
self.child1 = subwindow() #这里必须使用self,否则槽函数执行完毕以后窗口就关掉了
self.child1.connect(self.myslot)
self.child1.show() #注意括号里没有self
def myslot(self, var):
pass1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
在子窗口的类声明中自定义信号,并在类的成员函数中发射信号(emit)
发射信号时传递的值可以是很多类型,可以是str、int、list、object、float、tuple、dict等等。要传递什么只需要在信号声明时指定,还可以传递多个值,比如mysignal=pyqtSignal(int, int, int)
在主窗口的用于显示子窗口的类成员函数中接受信号 (connect),其参数为槽函数(注意槽函数的函数签名需要和发射信号时的函数签名相同)
10.在listView显示文字
listView是一个实例
slm = QStringListModel(); # 创建mode
content = ['ss','aa','ff'] # 添加的数组数据
slm.setStringList(content) # 将数据设置到model
self.listView.setModel(slm) # 绑定 listView 和 model1
2
3
4
11.label显示字符串
self.label.setText
12.listView的槽函数
def display_choice_2(self, qModelIndex): # listView_2单击的槽函数
print(qModelIndex.row) #当前点击选中的行数
13.ListWidget显示list,以及其他用法
self.listWidget.addItems(list)
注意addItems会在原有的基础上添加,必要时需要用clear进行删除。
使用selectedIndexes()获取当前选择的行数
注意selectedIndexes()得到的是qModelIndex对象,使用qModelIndex.row()获取索引值
https://www.cnblogs.com/ygzhaof/p/10077658.html
14.QCombox的使用
currentText()获得文本
currentIndex()获得下标
self.comboBox.currentIndexChanged.connect() comboBox的事件选中函数
self.comboBox.highlight.connect() comboBox的事件选中函数(高亮)
15.坑
不要再初始化函数里创建类的成员变量