Python事多,做个笔记,区分。
PyQt5 Reference Guide
http://pyqt.sourceforge.net/Docs/PyQt5/index.html
Qt4 signal:
class CopyFileThread(QtCore.QThread): signal_process = QtCore.pyqtSignal(str, str, bool) def __init__(self, parent=None): super(CopyFileThread, self).__init__(parent) self.finished.connect(self.taskEnd) def setSourceAndDestination(self, src, des): self.source = src self.des = des self.status = False def run(self): #print "copy -> ", self.source, self.des # QtCore.QFile.copy(self.source,self.des) try: shutil.copy(self.source, self.des) self.status = True except: self.status = False def taskEnd(self): self.signal_process.emit(self.source, self.des, self.status)
接受这个signal槽:
@QtCore.pyqtSlot(str, str, bool) def perThreadCopyEnd(self, src, des, status): self.taskNum += 1 if (status): self.throwMessage(">>" + src + "\t->->->->\t" + des + "<>" + src + "\tT_T T_T T_T\t" + des + "< > process end")
Qt5展示的一些发射信号:
from PyQt5.QtCore import QObject, pyqtSignalclass Foo(QObject): # Define a new signal called 'trigger' that has no arguments. trigger = pyqtSignal() def connect_and_emit_trigger(self): # Connect the trigger signal to a slot. self.trigger.connect(self.handle_trigger) # Emit the signal. self.trigger.emit() def handle_trigger(self): # Show that the slot has been called. print "trigger signal received"
信号重载
from PyQt5.QtWidgets import QComboBoxclass Bar(QComboBox): def connect_activated(self): # The PyQt5 documentation will define what the default overload is. # In this case it is the overload with the single integer argument. self.activated.connect(self.handle_int) # For non-default overloads we have to specify which we want to # connect. In this case the one with the single string argument. # (Note that we could also explicitly specify the default if we # wanted to.) self.activated[str].connect(self.handle_string) def handle_int(self, index): print "activated signal passed integer", index def handle_string(self, text): print "activated signal passed QString", text
QML:
<1> show 一个qml里的window
import QtQuick 2.0import QtQuick.Window 2.2import QtQuick.Controls 1import QtQuick.Dialogs 1.2Window{ id:root width:1280 height:720 Rectangle { id:rec color:"#FF2020" width:100 height:100 anchors.centerIn:parent border.color:"#202020" border.width:1 } MouseArea { id:quitArea anchors.fill: { rec } onClicked: { close() } }}
main.py:
from PyQt5 import QtWidgets,QtGui,QtCorefrom PyQt5 import QtQmlfrom PyQt5.QtQuick import QQuickView,QQuickWindowimport sysif __name__ == "__main__": app = QtGui.QGuiApplication(sys.argv) eng = QtQml.QQmlApplicationEngine() eng.load(QtCore.QUrl.fromLocalFile('./UI/main.qml')) topLevel = eng.rootObjects()[0] print topLevel topLevel.show() app.exec_()
创建混合窗口:
按钮是QPushButton,下面的白色区域是QtQuickWindow
from PyQt5 import QtWidgets,QtGui,QtCorefrom PyQt5 import QtQmlfrom PyQt5.QtQuick import QQuickView,QQuickWindowimport sysif __name__ == "__main__": app = QtWidgets.QApplication(sys.argv) eng = QtQml.QQmlApplicationEngine() eng.load(QtCore.QUrl.fromLocalFile('./UI/Main.qml')) topLevel = eng.rootObjects()[0] print topLevel #topLevel.show() layout = QtWidgets.QVBoxLayout() button = QtWidgets.QPushButton() button.setText("houdini") layout.addWidget(button) mainWidget = QtWidgets.QWidget() quickWidget = QtWidgets.QWidget.createWindowContainer(topLevel) #quickWidget.show() mainWidget.setLayout(layout) layout.addWidget(quickWidget) mainWidget.show() app.exec_()