美文网首页
【PySide2学习笔记】1_简单的QtWidgets应用和Qt

【PySide2学习笔记】1_简单的QtWidgets应用和Qt

作者: 4thirteen2one | 来源:发表于2019-04-22 18:14 被阅读0次

1. QtWidgets应用

import sys
from PySide2.QtWidgets import QApplication, QLabel

app = QApplication(sys.argv)
# A QLabel is a widget that can present text (simple or rich, like html), and images
label = QLabel("<font color=red size=40>Hello World!</font>")
# show the label
label.show()
# enter the Qt main loop and start to execute the Qt code
# In reality, it is only here where the label is shown
app.exec_()

运行结果如下:


QtWidgets.png

2. QtQuick/QML应用

PySide2/QML应用程序至少包含两个不同的文件——一个带有用户界面QML描述的文件,以及一个加载QML文件的Python文件。

  • view.qml

    import QtQuick 2.0
    
    Rectangle {
        width: 200
        height: 200
        color: "green"
    
        Text {
            text: "Hello World"
            anchors.centerIn: parent
        }
    }
    
  • main.py

    from PySide2.QtWidgets import QApplication
    from PySide2.QtQuick import QQuickView
    from PySide2.QtCore import QUrl
    
    # A PySide2/QML application consists, at least, 
    # of two different files - a file with the QML description of the user interface, 
    # and a python file that loads the QML file. 
    
    app = QApplication([])
    view = QQuickView()
    # import QtQuick and set the source of the QQuickView object to the URL of your QML file
    url = QUrl("./view.qml")
    
    # import QtQuick and set the source of the QQuickView object to the URL of your QML file
    # If you are programming for desktop, you should consider 
    # adding view.setResizeMode(QQuickView.SizeRootObjectToView) before showing the view.
    view.setSource(url)
    # view.setResizeMode(QQuickView.SizeRootObjectToView)
    view.show()
    app.exec_()
    
  • 运行结果:


    QML.png

相关文章

网友评论

      本文标题:【PySide2学习笔记】1_简单的QtWidgets应用和Qt

      本文链接:https://www.haomeiwen.com/subject/iincgqtx.html