Pyqt 基础功能
总结Pyqt的基础知识
1. Pyqt 设置禁止最大化及禁止拖拽窗口大小
1 # PyQT禁止窗口最大化按钮: 2 self.setWindowFlags(QtCore.Qt.WindowMinimizeButtonHint) 3 # PyQT禁止调整窗口大小: 4 self.setFixedSize(self.width(), self.height())
1.1 修改窗体大小
self.resize(500, 400)
2. 当按下Esc退出
重载keyPressEvent
1 def keyPressEvent(self, event): 2 if event.key() ==QtCore.Qt.Key_Escape: 3 self.close()
3.去掉标题栏
1 self.setWindowFlags(Qt.FramelessWindowHint | Qt.WindowStaysOnTopHint)
self.setWindowFlags(Qt.CustomizeWindowHint) # 有透明边框
4.触发信号槽
内部方法 triggered() clicked()
执行: quit()
1 QtGui.QWidget.connect(self.ui_qr.pushButtonOk, QtCore.SIGNAL('clicked()'), self.generateQr) 2 self.ui_qr.pushButtonOk.clicked.connect(self.generateQr)
5.弹框消息
消息类型: information , warning , question , about
1 OK = QtGui.QMessageBox.question(self, (u'提示'),(u'滚动轴点击'),QtGui.QMessageBox.Yes , QtGui.QMessageBox.No)
6.支持窗口拖动
重载鼠标事件方法
1 def mousePressEvent(self, event): 2 if event.button()==Qt.LeftButton: 3 self.m_drag=True 4 self.m_DragPosition=event.globalPos()-self.pos() 5 event.accept() 6 7 def mouseMoveEvent(self, QMouseEvent): 8 if QMouseEvent.buttons() and Qt.LeftButton: 9 self.move(QMouseEvent.globalPos()-self.m_DragPosition) 10 QMouseEvent.accept() 11 12 def mouseReleaseEvent(self, QMouseEvent): 13 self.m_drag=False
7.设置全局编码
1 # -*- coding: utf-8 -*- 2 3 reload(sys) 4 sys.setdefaultencoding("utf-8")
8.设置标题、Icon
1 label.setPixmap(QtGui.QPixmap(filename)) 3 self.setWindowTitle('Pyqt ComboBox') 4 self.setWindowIcon(QtGui.QIcon('../Document/images/QQ.png'))
9.设置button lineEdit 等字体
1 self.lineEdit.setFont(QFont("SimSun", 18, QFont.Bold)) # 宋体 , 18号字体, 粗体
10.退出后,后台继续保持进程
1 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('1')) button2.clicked.connect(lambda: self.on_button('2')) 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_()