posts - 319, comments - 22, trackbacks - 0, articles - 11
  C++博客 :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理
其实最为重要的一点,就是App的类型:
Qt5 中包括:
QObject -> QCoreApplication -> QGuiApplication -> QApplication

一般,QML的程序使用QGuiApplication,如果使用QGuiApplication的话,那么QtWidget的相关类就不能运行正常了。
需要使用QApplication类来处理,这样,QtWidget的相关类就可以使用了。
特别是我们对标准对话框(QFileDialog\ QFontDialog \ QColorDialog)等等


下面上代码:


main.cpp
=====================================================================
//![0]
#include "piechart.h"
#include <QtQuick/QQuickView>
#include <QGuiApplication>
#include <QApplication>

int main(int argc, char *argv[])
{
 QApplication app(argc, argv);

 qmlRegisterType<PieChart>("Charts", 1, 0, "PieChart");

 QQuickView view;
 view.setResizeMode(QQuickView::SizeRootObjectToView);
 view.setSource(QUrl::fromLocalFile("app.qml"));
 view.show();
 return app.exec();
}
//![0]

=======================================================================================
PieChart.h
=======================================================================================
#ifndef PIECHART_H
#define PIECHART_H

#include <QtQuick/QQuickPaintedItem>
#include <QColor>

//![0]
class PieChart : public QQuickPaintedItem
{
//![0]
 Q_OBJECT
 Q_PROPERTY(QString name READ name WRITE setName)
 Q_PROPERTY(QColor color READ color WRITE setColor)

//![1]
public:
//![1]

 PieChart(QQuickItem *parent = 0);

 QString name() const;
 void setName(const QString &name);

 QColor color() const;
 void setColor(const QColor &color);

 void paint(QPainter *painter);

//![2]
 Q_INVOKABLE void clearChart();

signals:
 void chartCleared();
//![2]

private:
 QString m_name;
 QColor m_color;

//![3]
};
//![3]

#endif


========================================================================================
PieChart.cpp
========================================================================================
#include "piechart.h"
#include <QPainter>

#include <QFileDialog>

PieChart::PieChart(QQuickItem *parent)
 : QQuickPaintedItem(parent)
{
}

QString PieChart::name() const
{
 return m_name;
}

void PieChart::setName(const QString &name)
{
 m_name = name;
}

QColor PieChart::color() const
{
 return m_color;
}

void PieChart::setColor(const QColor &color)
{
 m_color = color;
}

void PieChart::paint(QPainter *painter)
{
 QPen pen(m_color, 2);
 painter->setPen(pen);
 painter->setRenderHints(QPainter::HighQualityAntialiasing, true);
 painter->drawPie(boundingRect(), 90 * 16, 290 * 16);
}

//![0]
void PieChart::clearChart()
{
 QString fileName = QFileDialog::getOpenFileName(0,
 tr("Open Image"), "", tr("Image Files (*.png *.jpg *.bmp)"));
 setColor(QColor(Qt::transparent));
 update();

 emit chartCleared();
}
//![0]




========================================================================================
app.qml
========================================================================================
 
//![0]
import Charts 1.0
import QtQuick 2.0

Item {
 width: 300; height: 200

 PieChart {
 id: aPieChart
 anchors.centerIn: parent
 width: 100; height: 100
 color: "red"

 onChartCleared: console.log("The chart has been cleared")
 }

 MouseArea {
 anchors.fill: parent
 onClicked: aPieChart.clearChart()
 }

 Text {
 anchors { bottom: parent.bottom; horizontalCenter: parent.horizontalCenter; bottomMargin: 20 }
 text: "Click anywhere to clear the chart"
 }
}
//![0]



只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理