pyside (6) 的基础学习笔一:加载 QML

入门

官方模块参考手册
官方新手指导手册(有安装指导)
若读者熟悉 CPP 方式开发 QT 可以简单看一下,官方对比的示例

##### python vs c++ 两则转换(若不清楚,可以参考 CPP 方式)
1. import vs #include 
2. __init__() vs 构造函数
3. self vs this
4. global 拓展到全局
5. @property vs Q_PROPERTY
6. connect(),Q_SIGNALS,Q_SLOTS  vs  @Slot
7. bytes vs QByteArray ; str vs QString
8. if __name__ == "__main__" vs main()
还有很多,暂写以上

由此看到,不同语言中 QT 的开发方式其实较为相近,方便用户快速上手

两个代码开发的对比示例(官方有更多)

def __init__(self, parent=None):
    star_png = Path(__file__).parent / "images" / "star.png"
    self.star = QPixmap(star_png)
    
def sizeHint(self, option, index):
    """ Returns the size needed to display the item in a QSize object. """
     if index.column() == 5:
        size_hint = QSize(5 * self.star.width(), self.star.height()) + QSize(1, 1)
        return size_hint58        

     # Since we draw the grid ourselves:59        
     return QSqlRelationalDelegate.sizeHint(self, option, index) + QSize(1, 1)

QSize BookDelegate::sizeHint(const QStyleOptionViewItem &option,                                 const QModelIndex &index) const
{   
    if (index.column() == 5)        
        return QSize(5 * star.width(), star.height()) + QSize(1, 1);
    // Since we draw the grid ourselves:  
    return QSqlRelationalDelegate::sizeHint(option, index) + QSize(1, 1);
}

加载 QML
#--------------Python-----------
import sys
from PySide6.QtQml import QQmlApplicationEngine, QmlElement
from PySide6.QtGui import  QGuiApplication
from PySide6.QtCore import QObject, QUrl, Slot

if __name__ == '__main__':    
    app = QGuiApplication()    
    engine = QQmlApplicationEngine() 
    engine.load(QUrl('main.qml'))   
    if not engine.rootObjects():        
        sys.exit(-1)    
    sys.exit(app.exec())

#--------------QML-------------
ApplicationWindow  {    
    visible: true    
    width: 640    
    height: 480    
    title: qsTr("Hello World")
    
    
    Rectangle {        
        color: "red"       
        width: 100        
        height: 100   
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值