PyQt4 / PyQt5

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)
View Code

接受这个signal槽:

@QtCore.pyqtSlot(str, str, bool)
    def perThreadCopyEnd(self, src, des, status):
        self.taskNum += 1
        if (status):
            self.throwMessage(">>" + src + "\t->->->->\t" + des + "<<copy end")
        else:
            self.throwMessage(">>" + src + "\tT_T T_T T_T\t" + des + "<<copy failed")
        if self.taskNum == len(self.sources):
            self.throwMessage(">>   process end")
View Code

 

Qt5展示的一些发射信号:

from PyQt5.QtCore import QObject, pyqtSignal

class 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"
View Code

 

信号重载

from PyQt5.QtWidgets import QComboBox

class 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
View Code

 

 QML:

<1> show 一个qml里的window

import QtQuick 2.0
import QtQuick.Window 2.2
import QtQuick.Controls 1
import QtQuick.Dialogs 1.2


Window
{
    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()
        }
    }

}
View Code

main.py:

from PyQt5 import QtWidgets,QtGui,QtCore
from PyQt5 import QtQml
from PyQt5.QtQuick import QQuickView,QQuickWindow

import sys
if __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,QtCore
from PyQt5 import QtQml
from PyQt5.QtQuick import QQuickView,QQuickWindow

import sys
if __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_()
View Code

 

转载于:https://www.cnblogs.com/gearslogy/p/7805044.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值