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 ...
随机推荐
- Java基础构造方法和this关键字整理
构造方法 8.1构造方法介绍 构造方法的格式: 修饰符 构造方法名(参数列表) { } l 构造方法的体现: n 构造方法没有返回值类型.也不需要写返回值.因为它是为构建对象的,对象创建完,方法就 ...
- Python中表达式与语句
简述 Python中我暂时并未发现谁对着两个名词的明确定义:我对这两个名词的理解就是,表达式就是你想要执行的对象,语句就是你的具体执行操作. 这里应用慕课网老师的一段话,摘自网上"表达式(E ...
- mac 安装pip
mac 安装pip报错 bogon:~ root# sudo easy_install pip Searching for pip Reading https://pypi.python.org/si ...
- 立个Flag不学好PHP誓不罢休
3年前从部队退伍退伍回来,就莫名其秒的爱上的编程,复学期间我几乎忘记了本专业的知识(原本我是读书籍设计的),从刚刚开始的C程序开始,一路走到一拿起书本我就几乎是睡着的状态,后来就开始了视频的学习之路, ...
- [leetcode-128] 最长连续序列
给定一个未排序的整数数组,找出最长连续序列的长度. 要求算法的时间复杂度为 O(n). 示例: 输入: [100, 4, 200, 1, 3, 2] 输出: 4 解释: 最长连续序列是 [1, 2, ...
- Python中late-binding-closures
2018-01-03 @望京 示例1 >>> b = [] >>> for i in range(5): ... b.append(lambda :i) ... & ...
- 爬虫基础02-day24
写在前面 上课第24天,打卡: 努力不必让全世界知道: s16/17爬虫2 内容回顾: 1. Http协议 Http协议:GET / http1.1/r/n...../r/r/r/na=1 TCP协议 ...
- 解决浏览器跨域限制方案之WebSocket
WebSocket是在HTML5中引入的浏览器与服务端的通信协议,可以类比HTTP. 可以在支持HTML5的浏览器版本中使用WebSocket进行数据通信,常见的案例是使用WebSocket进行实时数 ...
- Linux 用top命令查看CPU和内存使用情况
直接 top 回车 PID:进程的ID USER:进程所有者 PR:进程的优先级别,越小越优先被执行 NInice:值 VIRT:进程占用的虚拟内存 RES:进程占用的物理内存 SHR:进程使用的共享 ...
- SQL Server 2016 附加数据库提示创建文件失败如何解决
1.右键Microsoft SQL Server Management Studio2.以管理员方式运行