总结Pyqt的基础知识
1. Pyqt 设置禁止最大化及禁止拖拽窗口大小
# PyQT禁止窗口最大化按钮:
self.setWindowFlags(QtCore.Qt.WindowMinimizeButtonHint)
# PyQT禁止调整窗口大小:
self.setFixedSize(self.width(), self.height())
1.1 修改窗体大小
self.resize(500, 400)
2. 当按下Esc退出
重载keyPressEvent
def keyPressEvent(self, event):
if event.key() ==QtCore.Qt.Key_Escape:
self.close()
3.去掉标题栏
self.setWindowFlags(Qt.FramelessWindowHint | Qt.WindowStaysOnTopHint)
self.setWindowFlags(Qt.CustomizeWindowHint) # 有透明边框
4.触发信号槽
内部方法 triggered() clicked()
执行: quit()
QtGui.QWidget.connect(self.ui_qr.pushButtonOk, QtCore.SIGNAL('clicked()'), self.generateQr)
self.ui_qr.pushButtonOk.clicked.connect(self.generateQr)
5.弹框消息
消息类型: information , warning , question , about
OK = QtGui.QMessageBox.question(self, (u'提示'),(u'滚动轴点击'),QtGui.QMessageBox.Yes , QtGui.QMessageBox.No)
6.支持窗口拖动
重载鼠标事件方法
def mousePressEvent(self, event):
if event.button()==Qt.LeftButton:
self.m_drag=True
self.m_DragPosition=event.globalPos()-self.pos()
event.accept() def mouseMoveEvent(self, QMouseEvent):
if QMouseEvent.buttons() and Qt.LeftButton:
self.move(QMouseEvent.globalPos()-self.m_DragPosition)
QMouseEvent.accept() def mouseReleaseEvent(self, QMouseEvent):
self.m_drag=False
7.设置全局编码
# -*- coding: utf-8 -*- reload(sys)
sys.setdefaultencoding("utf-8")
8.设置标题、Icon
label.setPixmap(QtGui.QPixmap(filename))
self.setWindowTitle('Pyqt ComboBox')
self.setWindowIcon(QtGui.QIcon('../Document/images/QQ.png'))
9.设置button lineEdit 等字体
self.lineEdit.setFont(QFont("SimSun", 18, QFont.Bold)) # 宋体 , 18号字体, 粗体
10.退出后,后台继续保持进程
app.setQuitOnLastWindowClosed(True) #true 关闭, false 开启后台
11.显示在最前端
self.setWindowFlags(QtCore.Qt.WindowStaysOnTopHint)
12.窗体透明度
self.setWindowOpacity(0.5) # 1表示100%显示, 0.5 透明度50%
13.只显示关闭按钮
self.setWindowFlags(Qt.CustomizeWindowHint| Qt.WindowCloseButtonHint )
14.隐藏状态栏
self.setWindowFlags(QtCore.Qt.Tool )
15.自定义发射事件信号
from PyQt5.QtWidgets import *
from PyQt5 import QtCore class MyForm(QMainWindow):
_signal = QtCore.pyqtSignal(str) # 定义信号,定义参数为str类型 str、int、list、object、float、tuple、dict def __init__(self):
super().__init__()
button = QPushButton('Button ')
button.clicked.connect(self.on_button) self._signal.connect(self.mySignal) # 将信号连接到函数mySignal
layout = QHBoxLayout()
layout.addWidget(button)
main_frame = QWidget()
main_frame.setLayout(layout)
self.setCentralWidget(main_frame) def on_button(self):
self._signal.emit('hello,你好') # 需要之时emit def mySignal(self, string):
print(string) if __name__ == "__main__":
import sys
app = QApplication(sys.argv)
form = MyForm()
form.show()
app.exec_()
16.信号和槽传递额外参数
from PyQt5.QtWidgets import *
# 使用lambda传递按钮数字给槽 by: http://blog.csdn.net/a359680405/article/details/45246605
class MyForm(QMainWindow):
def __init__(self):
super().__init__()
button1 = QPushButton('Button 1')
button2 = QPushButton('Button 2')
button1.clicked.connect(lambda: self.on_button(''))
button2.clicked.connect(lambda: self.on_button('')) layout = QHBoxLayout()
layout.addWidget(button1)
layout.addWidget(button2) main_frame = QWidget()
main_frame.setLayout(layout)
self.setCentralWidget(main_frame) def on_button(self, n):
print('Button {0} clicked'.format(n)) if __name__ == "__main__":
import sys
app = QApplication(sys.argv)
form = MyForm()
form.show()
app.exec_()