PyQt4 / PyQt5

Python事多,做个笔记,区分。

PySide2 Signal Slot Test

from PySide2.QtWidgets import QMainWindow,QApplication,QWidget,QPushButton
from PySide2 import QtWidgets
from PySide2.QtCore import Slot, Signal

import sys

class MyCenWidget(QWidget):

    closeSignal = Signal()

    quitSignal = Signal()

    def __init__(self,parent = None):
        super(MyCenWidget, self).__init__(parent)
        # first create layout
        self.createAndSetLayout()
        # add widget to my layout
        self.createButtons()

    def createAndSetLayout(self):
        self.mainLayout = QtWidgets.QHBoxLayout()
        self.setLayout(self.mainLayout)

    def createButtons(self):
        self.closeBtn = QPushButton(self)
        self.closeBtn.setText("Close")
        self.closeBtn.clicked.connect(self.closeSignal)  # 信号连接信号
        self.mainLayout.addWidget(self.closeBtn)

        # Quit button settings
        self.quitBtn = QPushButton(self)
        self.quitBtn.setText("Quit")
        self.quitBtn.clicked.connect(self.quitSlot)
        self.mainLayout.addWidget(self.quitBtn)

    def quitSlot(self):
        print("我的中心控件点了quit button")
        self.quitSignal.emit()

class MyWindow(QMainWindow):
    def __init__(self):
        super(MyWindow, self).__init__()

        # create my widget
        self.setupMenuBar()
        # bind my widget to function
        self.accessMenuBar()

        # take my widget to here
        cenWidget = MyCenWidget()  # 野指针
        cenWidget.closeSignal.connect(self.close)  # 从我定义的窗口里发射的信号,这个是closeSignal
        cenWidget.quitSignal.connect(self.close)

        
        self.setCentralWidget(cenWidget) # 基类有个函数让我设置它到我的QMainWindow

    def setupMenuBar(self):
        parentMenuBar = self.menuBar()

        fileMenu = parentMenuBar.addMenu("File")
        self.newAction = fileMenu.addAction("new")

        editMenu = parentMenuBar.addMenu("Edit")
        self.undoAction = editMenu.addAction("undo")


    # make connection new action
    def accessMenuBar(self):
        self.newAction.triggered.connect(self.newSlot)

    @Slot(bool)
    def newSlot(self, checked):
        print("into my new slot")


if __name__ == "__main__":
    app = QApplication(sys.argv)
    print("My window argv:", sys.argv)
    w = MyWindow()
    w.show()
    app.exec_()
View Code

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 + "	->->->->	" + des + "<<copy end")
        else:
            self.throwMessage(">>" + src + "	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
    1280
    height:720
    Rectangle
    {
        id:rec
        color:"#FF2020"
        100
        height:100
        anchors.centerIn:parent
        border.color:"#202020"
        border.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