QML 中调用本地标准的对话框如:QFileDialog QFontDialog Google搜索关键字qml qfiledialog (Qt5)

其实最为重要的一点,就是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]


你可能感兴趣的:(QML 中调用本地标准的对话框如:QFileDialog QFontDialog Google搜索关键字qml qfiledialog (Qt5))