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 QtCoreclass MyForm(QMainWindow):_signal = QtCore.pyqtSignal(str) # 定義信號,定義參數為str類型 str、int、list、object、float、tuple、dictdef __init__(self):super().__init__()button = QPushButton('Button ')button.clicked.connect(self.on_button)self._signal.connect(self.mySignal) # 將信號連接到函數mySignallayout = 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 sysapp = 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 sysapp = QApplication(sys.argv)form = MyForm()form.show()app.exec_()?
總結
- 上一篇: 对Java泛型的简单理解
- 下一篇: Linux下多线程查看工具(pstree