入门
官方模块参考手册
官方新手指导手册(有安装指导)
若读者熟悉 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
}
}