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_()
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 + "<<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")
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"
信号重载
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

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()
}
} }
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_()
PyQt4 / PyQt5的更多相关文章
- Python pyQt4/PyQt5 学习笔记3(绝对对位,盒布局,网格布局)
本节研究布局管理的内容. (一)绝对对位 import sys from PyQt4 import QtGui class Example(QtGui.QWidget): def __init__( ...
- Python pyQt4/pyQt5 学习笔记2(状态栏、菜单栏和工具栏)
例子:状态栏.菜单栏和工具栏 import sys from PyQt4 import QtGui class Example(QtGui.QMainWindow): def __init__(sel ...
- Python pyQt4/pyQt5 学习笔记1(空白窗口,按钮,控件事件,控件提示,窗体显示到屏幕中间,messagebox)
PyQt4是用来编写有图形界面程序(GUI applications)的一个工具包.PyQt4作为一个Python模块来使用,它有440个类和超过6000种函数和方法.同时它也是一个可以在几乎所有主流 ...
- Python pyQt4/PyQt5 学习笔记4(事件和信号)
信号 & 槽 import sys from PyQt5.QtCore import Qt from PyQt5.QtWidgets import (QWidget,QLCDNumber,QS ...
- PyQt 5.4参考指南 ---- PyQt5和PyQt4之间的差异
欢迎关注博主主页,学习python视频资源,还有大量免费python经典文章 sklearn实战-乳腺癌细胞数据挖掘(博主亲自录制视频) https://study.163.com/course/in ...
- PyQt5初识
学习PyQt5是个机缘,那是因为我的linux16.04+python3.6使了浑身解数也装不上PyQt4! PyQt5的官方文档貌似是要钱的!又想快速了解这个东东,我还是借鉴了万能的博客园大佬博主: ...
- GUI1_综合介绍
最终比较,选择pyqt用于GUI开发 https://pythonspot.com/en/gui/ 图形化界面可以使用PyQt5, PyQt4, wxPython or Tk.模板 Graphical ...
- 共有49款Windows GUI开发框架开源软件 【转】
源文 : http://www.oschina.net/project/tag/178/gui?lang=36&os=0&sort=view&p=1 桌面应用开发引擎 Allo ...
- 【pyqtgraph绘图】安装pyqtgraph
解读官方API-安装 安装 参考:http://www.pyqtgraph.org/documentation/installation.html 根据您的需要,有许多不同的方式来安装pyqtgrap ...
随机推荐
- 【3D动画建模设计工具】Maxon Cinema 4D Studio for Mac 20.0
图标 Icon 软件介绍 Description Maxon Cinema 4D Studio R20 ,是由德国公司Maxon Computer一款适用于macOS系统的3D动画建模设计工具,是 ...
- python自动化开发-[第十三天]-前端Css续
今日概要: 1.伪类选择器 2.选择器优先级 3.vertical-align属性 4.backgroud属性 5.边框border属性 6.display属性 7.padding,margine(见 ...
- entityManager分页
十分操蛋. 需要两步. 第一步,查询一共需要多少条. 第二步 分页得到数据 Query query = this.entityManager.createNativeQuery(sb2.toStr ...
- Hbase记录-zookeeper部署
#官网下载二进制包解压到/usr/app下,配置/etc/profile: export ZOOKEEPER_HOME=/usr/app/zookeeper export PATH=$PATH:$ZO ...
- Linux学习笔记:【000】Linux系统入门
什么是Linux? Linux是一套免费使用和自由传播的类Unix操作系统,是一个基于POSIX(可移植操作系统接口 Portable Operating System Interface of UN ...
- Openresty 学习笔记(四)lualocks包管理器安装使用
Luarocks是一个Lua包管理器,基于Lua语言开发,提供一个命令行的方式来管理Lua包依赖.安装第三方Lua包等,社区比较流行的包管理器之一,另还有一个LuaDist,Luarocks的包数量比 ...
- Openresty 学习笔记(三)扩展库之neturl
github地址:https://github.com/golgote/neturl 最近在搞一个视频加密播放,中间使用要用lua 匹配一个域名,判断该域名是否正确 PS:使用PHP很好做,lua 的 ...
- java中数组、集合、字符串之间的转换,以及用加强for循环遍历
java中数组.集合.字符串之间的转换,以及用加强for循环遍历: @Test public void testDemo5() { ArrayList<String> list = new ...
- 一个强大的VS代码搜索工具
最近一直在寻找一款VS代码搜索插件,终于找到了一个不错的,仅支持vs2012以上. https://marketplace.visualstudio.com/items?itemName=mario- ...
- Win10 64位连接LJM1005打印机局域网访问
除了网上常见的开Guest用户之类需要额外三个设置 (1)安装LJM1005驱动LJM1005_Full_Solution (2)设置打印机共享和安全中的everyone全部勾选(解决能看到打印机无法 ...