Qt for Python快速入门



  • 一.搭建环境

    • 1.python官网下载python3.7.4 安装

    https://www.python.org/ftp/python/3.7.4/python-3.7.4.exe

    • 2.命令行安装PySide2

      pip install PySide2

    • 3.安装qt 5.13

      参考帖子

    二、创建demo程序

    • 1.打开qt 5.13,创建Qt for Python - Windows 程序

    1563883960069.png

    • 2.在main.py里追加如下代码

    追加导入

        # This Python file uses the following encoding: utf-8
        import sys
        import random
        from PySide2 import QtCore, QtWidgets, QtGui
    

    定义MyWidget类

    class MyWidget(QtWidgets.QWidget):
        def __init__(self):
            super().__init__()
    
            self.hello = ["Hallo Welt", "Hei maailma", "Hola Mundo", "Привет мир"]
    
            self.button = QtWidgets.QPushButton("Click me!")
            self.text = QtWidgets.QLabel("Hello World")
            self.text.setAlignment(QtCore.Qt.AlignCenter)
    
            self.layout = QtWidgets.QVBoxLayout()
            self.layout.addWidget(self.text)
            self.layout.addWidget(self.button)
            self.setLayout(self.layout)
    
            self.button.clicked.connect(self.magic)
    
    
        def magic(self):
            self.text.setText(random.choice(self.hello))
    

    添加main函数,显示窗口

    if __name__ == "__main__":
        app = QtWidgets.QApplication([])
    
        widget = MyWidget()
        widget.resize(800, 600)
        widget.show()
    
        sys.exit(app.exec_())
    
    • 3.运行截图
      1563884627270.png

    参考qt官方文档 https://doc.qt.io/qtforpython/gettingstarted.html



  • 好奇怪,图片都裂了。怎么回事呢?不过目前顺利学会了Qt for Python,祝贺你哦。👍


Log in to reply
 

走马观花

最近的回复

  • 萌梦 男孩,女孩,和蛋

    menghome.png

    read more
  • 设计模式-工厂模式

    使用qt/qml来演示设计模式效果,便于学习理解

    1)定义创建对象的接口,封装对象的创建
    2)使具体化类的工作延迟到工厂子类中

    bg.png
    image.png

    1. 工厂类

    createProduct使用了参数来选择要创建哪个产品

    #ifndef FACTORY_H #define FACTORY_H #include <QObject> class Product; class QString; class Factory: public QObject { Q_OBJECT public: virtual ~Factory() = 0; virtual Product* createProduct(QString type) = 0; protected: Factory(); }; class ConcreteFactory: public Factory { Q_OBJECT public: ~ConcreteFactory(); ConcreteFactory(); public slots: Product* createProduct(QString type); }; #endif // FACTORY_H #include "factory.h" #include "product.h" #include <QtQml/qqml.h> Factory::~Factory() { } Factory::Factory() { qmlRegisterType<Product>("Product", 1, 0, "Product"); } ConcreteFactory::~ConcreteFactory() { } ConcreteFactory::ConcreteFactory() { } Product *ConcreteFactory::createProduct(QString type) { if(type == "boy") return static_cast<Product *>(new ConcreteProduct1()); else if(type == "girl") return static_cast<Product *>(new ConcreteProduct2()); return static_cast<Product *>(new ConcreteProduct1()); } 2 产品类

    一个产品是萌梦男,一个产品是萌梦女

    #ifndef PRODUCT_H #define PRODUCT_H #include <QObject> class Product: public QObject { Q_OBJECT public: virtual ~Product() = 0; Q_PROPERTY(QString icon READ icon NOTIFY iconChanged) QString m_icon; QString icon() const { return m_icon; } signals: void iconChanged(QString icon); protected: Product(); signals: public slots: }; class ConcreteProduct1: public Product { Q_OBJECT public: ~ConcreteProduct1(); ConcreteProduct1(); }; class ConcreteProduct2: public Product { Q_OBJECT public: ~ConcreteProduct2(); ConcreteProduct2(); }; #endif // PRODUCT_H #include "product.h" Product::~Product() { } Product::Product() { } ConcreteProduct1::~ConcreteProduct1() { } ConcreteProduct1::ConcreteProduct1() { m_icon = "qrc:/images/boy.png"; } ConcreteProduct2::~ConcreteProduct2() { } ConcreteProduct2::ConcreteProduct2() { m_icon = "qrc:/images/girl.png"; } 3. main.qml

    使用timer,canvas,listview等实现一个自动化生产的动画效果

    源代码

    Fork me on Gitee

    read more
  • blender建模 章鱼
    捕获2.PNG 😵

    read more

关注我们

微博
QQ群