qml写界面的时候,很有可能会多个页面跳来跳去,很多时候需要逐级回退,这里我提供一种方法,页面使用c++管理
#include <QObject>
#include <QList>
#include <QColor>
#include <QFont>
class VGQmlViewItem : public QObject
{
Q_OBJECT
public:
VGQmlViewItem(QObject *item, VGQmlViewItem *parent = NULL);
~VGQmlViewItem();
void AddChild(QObject *qmlObj);
QObject *QmlObject()const;
VGQmlViewItem *FindQmlObjectItem(const QObject *item);
QString GetName()const;
VGQmlViewItem *GetParent()const;
void RemoveItem(QObject *qmlObj);
private:
VGQmlViewItem *findQmlObjectItem(const QObject *item)const;
private slots:
void onQmldestroyed();
private:
QObject *m_qmlObject;
QList<VGQmlViewItem*> m_children;
};
class VGMainPage : public QObject
{
Q_OBJECT
Q_PROPERTY(int qmlRootWidth READ qmlRootWidth WRITE setQmlRootWidth NOTIFY qmlRootWidthChanged)
Q_PROPERTY(int qmlRootHeight READ qmlRootHeight WRITE setQmlRootHeight NOTIFY qmlRootHeightChanged)
Q_PROPERTY(QColor backColor READ GetBackColor WRITE SetBackColor NOTIFY backColorChanged)
Q_PROPERTY(QColor bgColor READ GetBackGroundColor CONSTANT)
Q_PROPERTY(QString qmlTip READ GetQmlTip NOTIFY qmlTipChanged)
Q_PROPERTY(QString messgeTip READ GetMessgeTip NOTIFY messgeTipChanged)
Q_PROPERTY(bool tipVisible READ IsTipVisible NOTIFY tipVisibleChanged)
Q_PROPERTY(bool tipWarn READ IsTipWarn NOTIFY tipWarnChanged)
//是否横屏
Q_PROPERTY(bool landscape READ landscape WRITE setLandscape NOTIFY landscapeChanged)
Q_PROPERTY(bool titleVisible READ GetTitleVisible WRITE SetTitleVisible NOTIFY titleVisibleChanged)
Q_PROPERTY(QString headerName READ headerName WRITE setHeaderName NOTIFY headerNameChanged)
Q_PROPERTY(QObject* curQmlPage READ GetCurQmlPage WRITE SetCurrentQmlPage NOTIFY sigShowPage)
Q_PROPERTY(bool showText READ IsShowText WRITE SetShowText NOTIFY showTextChanged)
Q_PROPERTY(bool showImage READ IsShowImage WRITE SetShowImage NOTIFY showImageChanged)
Q_PROPERTY(bool showSrSt READ IsShowSrSt CONSTANT)
public:
enum ButtonShowStayle
{
Show_Imge = 1,
Show_Text = 2,
Show_ImgeAndText = Show_Imge|Show_Text,
};
public:
explicit VGMainPage(QObject *parent = 0);
~VGMainPage();
Q_INVOKABLE void setScreenHoriz(bool b = false);
Q_INVOKABLE QString getTimeString(qint64 tMs, const QString &format = "yyyy-MM-dd hh:mm:ss");
Q_INVOKABLE QFont font(bool bBold = false);
Q_INVOKABLE QString getImageProviderUrl(const QString &name, bool enable = true, bool press = false);
Q_INVOKABLE qreal fontHeight(const QFont &f);
Q_INVOKABLE qreal stringWidth(const QFont &f, const QString &str);
public:
int qmlRootWidth()const;
void setQmlRootWidth(int width);
int qmlRootHeight()const;
void setQmlRootHeight(int height);
void setFont(const QFont &font);
bool landscape()const { return m_landscape;}
void setLandscape(bool landscape);
QString headerName()const { return m_headerName; }
void setHeaderName(const QString &name);
void SetCurrentQmlPage(QObject *qmlPage);
QObject *GetCurQmlPage()const;
void SetQmlTip(const QString &tip, bool bWarn);
void SetMessageBox(const QString &tip);
QColor GetBackGroundColor()const;
bool IsShowSrSt()const;
protected:
bool IsShowText()const;
void SetShowText(bool b);
bool IsShowImage()const;
void SetShowImage(bool b);
QColor GetBackColor()const;
void SetBackColor(const QColor &);
bool GetTitleVisible()const;
void SetTitleVisible(bool b);
bool IsTipVisible()const;
QString GetQmlTip()const;
QString GetMessgeTip() const;
bool IsTipWarn()const;
Q_INVOKABLE QFont miniFont(bool bBold = false);
Q_INVOKABLE QFont littleFont(bool bBold = false);
Q_INVOKABLE QFont biggerFont(bool bBold = false);
Q_INVOKABLE QFont largeFont(b