這大概會(huì)成為“一周突破Qt + QML "編程系列
1.debug 的使用
- Header:
#include <QDebug>
- qmake:
QT += core
- basic use:
qDebug() << "Date:" << QDate::currentDate();
qDebug() << "Types:" << QString("String") << QChar('x')
<< QRect(0, 10, 50, 40);
qDebug() << "Custom coordinate type:" << coordinate;
2.QQuickImageProvider Class的使用
- Header:
#include <QQuickImageProvider>
- qmake:
QT += quick
- Since:
Qt 5.0
- Inherits: QQmlImageProviderBase
- Inherited By:QQuickAsyncImageProvider
- public funciton:
有兩個(gè)虛函數(shù)- virtual QImage requestImage(const QString &id, QSize size, const QSize &requestedSize*)
用于返回Qimage類型匾二,其中: - id 對(duì)應(yīng)qml中image的source宛畦,例如在下面的main.qml中冈在,
source:“source: "image://myprovider/yellow""
,則id是source - requestedSize 是Image Item的尺寸勃蜘,如果image 中定義了width和height屬性,則該寬和長會(huì)傳給requesetedSzie
- size
- virtual QImage requestImage(const QString &id, QSize size, const QSize &requestedSize*)
- virtual QPixmap *requestPixmap (const QString &id, QSize size, const QSize &requestedSize)
用于返回qpixmap 類型 - examples:
//myprovider.h
#ifndef MYPROVIDER_H
#define MYPROVIDER_H
#include <QObject>
#include <QQuickImageProvider>
#include <QImage>
#include <QSize>
class MyProvider : public QQuickImageProvider
{
public:
MyProvider();
~MyProvider();
QPixmap requestPixmap(const QString &id, QSize *size, const QSize &requestedSize);
//requestPixmap 是指返回qpixmap
};
#endif // MYPROVIDER_H
//myprovider.cpp
#include "myprovider.h"
#include<QDebug>
#include<QPixmap>
MyProvider::MyProvider():
QQuickImageProvider(QQuickImageProvider::Pixmap)
{
}
MyProvider::~MyProvider(){
}
QPixmap MyProvider::requestPixmap(const QString &id, QSize *size, const QSize &requestedSize)
{
int width = 100;
int height = 50;
if (size)
*size = QSize(width, height);
QPixmap pixmap(requestedSize.width() > 0 ? requestedSize.width() : width,
requestedSize.height() > 0 ? requestedSize.height() : height);
pixmap.fill(QColor(id).rgba());
qDebug()<<id;
return pixmap;
}
//main.cpp
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include "myprovider.h"
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
MyProvider *imgprovider = new MyProvider();
QQmlApplicationEngine engine;
engine.addImageProvider("myprovider",imgprovider);
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
return app.exec();
}
//main.qml
import QtQuick 2.6
import QtQuick.Window 2.2
import QtQuick.Controls 1.4
Window {
visible: true
width: 800
height: 600
Column{
anchors.centerIn: parent
Image{
id:img
width: 200
height: 100
source: "image://myprovider/yellow"
//MyProvider::requestPixmap(***)的id是yellow
}
Image {
id: img2
source: "image://myprovider/green"
}
}
}
3 .qquickview
- usage:
QQuickView *view = new QQuickView;
view->setSource(QUrl::fromLocalFile("myqmlfile.qml"));
view->show();
4.使用插件拓展QML功能
QML插件配置()
解決“ module "myplugin" is not installed” 的問題
1. 目錄結(jié)構(gòu):
QMLPluginTest
| - myplugin // 該目錄下需含有qmldir插件和自定義的qml文件
| - MyRect.qml // 自定義的qml文件
| - qmldir
| - app
| - main.qml // 里面使用了MyRect組件
2.app.pro的配置
添加:QML_IMPORT_PATH = $$PWD/../
其中:$$PWD指得是源文件的配置目錄,此處讓main.qml可以找到myplugin所在目錄(目錄為
myplugin,組件名和目錄名應(yīng)該相同)
3.qmldir 的內(nèi)容:
module myplugin
MyRect 1.0 MyRect.qml
4.app工程中,在main.cpp中添加如下代碼柠新,從而找到組件:
QQmlApplicationEngine engine;
engine.addImportPath("E:\\dev\\QT\\QML\\QMLPluginText"); //該目錄下,能找到和模塊名相同的文件夾
名辉巡,模塊存在文件夾下(即myplugin目錄下恨憎,有myplugin模塊中定義的qml文件,參考上面的目錄結(jié)構(gòu))
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));