如何在运行时以编程方式更改主窗口的几何形状 w.r.t QML? [英] How to change the geometry of main window programmatically at run time w.r.t QML?

查看:52
本文介绍了如何在运行时以编程方式更改主窗口的几何形状 w.r.t QML?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 Qt Creator 2.6.1.我从项目模板创建了简单的 Qt Qucik 2.0 项目并在此更改了 main.qml 文件:

I have Qt Creator 2.6.1. I created simple Qt Qucik 2.0 project from project templates and changed main.qml file on this:

import QtQuick 2.0

Rectangle {
    width: 360
    height: 360

    color: "red"

    MouseArea {
        anchors.fill: parent
        onClicked: parent.height = 180
    }
}

如果我点击矩形,它应该减半.并且它发生了,但窗口没有减少.

If i will click on rectangle it should be reduced in half. And it is occured, but window is not reduced.

如果我希望主窗口必须重复主 qml 矩形的几何形状,最好的解决方案是什么?

What is a best solution, if i want that main window must repeat geometry of main qml rectangle?

更新.找到了一种解决方案.请参阅 Amit Tomar 的回答.但是存在更简单的解决方案,例如,使用 QtQuick 5.0:Qt Quick Window QML 类型 ?

UPDATE. One solution was found. See Amit Tomar answer. But is exist more easier solution, for example, using QtQuick 5.0: Qt Quick Window QML Types ?

推荐答案

您正在更改 qml 的几何结构,而不是查看器.这样做:

You are changing the geometry of your qml, not the Viewer. To do so:

  1. 可以使用您在主函数中创建的 QmlApplicationViewer 对象更改查看器的几何形状.
  2. 但是那个对象是用 C++ 编写的,所以你需要向 qml 公开一个 C++ 函数,并在点击这个按钮时调用这个函数.

步骤:

  1. 创建一个类并将在 main.cpp 中创建的应用程序查看器对象存储在该类中以供进一步调用.
  2. 将这个类中的一个函数暴露给 qml. 这个函数应能够使用存储在类中的应用程序查看器对象修改大小.
  3. 点击 qml 矩形时,调用此函数.
  1. Create a class and store the Application viewer object created in the main.cpp, inside this class for further calls.
  2. Expose a function in this class to qml. This function shall be able to modify the size using the application viewer object stored in the class.
  3. On click of qml rectangle, call this function.

main.cpp

#include <QtGui/QApplication>
#include "qmlapplicationviewer.h"
#include "qdeclarativecontext.h"

#include "myclass.h"

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    QmlApplicationViewer *viewer = new QmlApplicationViewer();

    MyClass myClassObject(viewer);
    viewer->rootContext()->setContextProperty("myViewer", &myClassObject);
    viewer->setOrientation(QmlApplicationViewer::ScreenOrientationAuto);
    viewer->setMainQmlFile(QLatin1String("qml/untitled/main.qml"));
    viewer->showExpanded();

    return app.exec();
}

myclass.h

#ifndef MYCLASS_H
#define MYCLASS_H

#include "qmlapplicationviewer.h"

class MyClass : public QObject
 {
     Q_OBJECT

 public:

    MyClass( QmlApplicationViewer * p ) { internalViewer = p ; }
    Q_INVOKABLE void viewerResize(int x, int y, int length, int breadth)

     {
       internalViewer->setGeometry(internalViewer->x(),internalViewer->y(),length,breadth);
     }

private:

    QmlApplicationViewer *internalViewer;
 };

#endif // MYCLASS_H

main.qml

import QtQuick 1.0

Rectangle {
    width: 360
    height: 360
    Text {
        text: "Hello World"
        anchors.centerIn: parent
    }
    MouseArea {
        anchors.fill: parent
        onClicked:
        {
            myViewer.viewerResize(0,0,110,110)
        }
    }
}

这篇关于如何在运行时以编程方式更改主窗口的几何形状 w.r.t QML?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆