首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >QML和QQuickWidget

QML和QQuickWidget
EN

Stack Overflow用户
提问于 2021-01-29 14:52:54
回答 1查看 823关注 0票数 0

我是qml的新手,但是我想通过引用QT示例的仪表板向QQuickWidget添加一个圆规。下面是我的密码。

guagetest.pro

代码语言:javascript
复制
QT       += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets quickwidgets
CONFIG += c++11

DEFINES += QT_DEPRECATED_WARNINGS

SOURCES += \
    main.cpp \
    mainwindow.cpp

HEADERS += \
    mainwindow.h

FORMS += \
    mainwindow.ui

# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target

RESOURCES += \
    dashboard.qrc

mainwindow.cpp

代码语言:javascript
复制
#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    ui->quickWidget->setSource(QUrl("qrc:/qml/qml/test.qml"));
    ui->quickWidget->show();
}

MainWindow::~MainWindow()
{
    delete ui;
} 

test.qml

代码语言:javascript
复制
import QtQuick 2.2
import QtQuick.Window 2.1
import QtQuick.Controls 1.4
import QtQuick.Controls.Styles 1.4
import QtQuick.Extras 1.4

Item {
    id: container
    width: parent.width
    height: parent.height
    anchors.centerIn: parent.Center

    Row {
        id: gaugeRow
        spacing: container.width * 0.2
        anchors.centerIn: parent

        CircularGauge {
            id: speedometer
            value: valueSource.kph
            anchors.verticalCenter: parent.verticalCenter
            maximumValue: 280
            // We set the width to the height, because the height will always be
            // the more limited factor. Also, all circular controls letterbox
            // their contents to ensure that they remain circular. However, we
            // don't want to extra space on the left and right of our gauges,
            // because they're laid out horizontally, and that would create
            // large horizontal gaps between gauges on wide screens.
            width: height
            height: container.height * 0.8

            style: DashboardGaugeStyle {}
        }
    }
}

DashboardGaugeStyle.qml

代码语言:javascript
复制
import QtQuick 2.2
import QtQuick.Controls.Styles 1.4

CircularGaugeStyle {
    tickmarkInset: toPixels(0.04)       // gauge graduation radius
    minorTickmarkInset: tickmarkInset
    labelStepSize: 20                   // gauge graduation text
    labelInset: toPixels(0.23)          // gauge graduation text position

    property real xCenter: outerRadius
    property real yCenter: outerRadius
    property real needleLength: outerRadius - tickmarkInset * 1.25
    property real needleTipWidth: toPixels(0.02)
    property real needleBaseWidth: toPixels(0.06)
    property bool halfGauge: false

    function toPixels(percentage) {
        return percentage * outerRadius;
    }

    function degToRad(degrees) {
        return degrees * (Math.PI / 180);
    }

    function radToDeg(radians) {
        return radians * (180 / Math.PI);
    }

    function paintBackground(ctx) {
        if (halfGauge) {
            ctx.beginPath();
            ctx.rect(0, 0, ctx.canvas.width, ctx.canvas.height / 2);
            ctx.clip();
        }

        ctx.beginPath();
        ctx.fillStyle = "black";
        ctx.ellipse(0, 0, ctx.canvas.width, ctx.canvas.height);
        ctx.fill();

        ctx.beginPath();
        ctx.lineWidth = tickmarkInset;
        ctx.strokeStyle = "black";
        ctx.arc(xCenter, yCenter, outerRadius - ctx.lineWidth / 2, outerRadius -ctx.lineWidth / 2, 0, Math.PI * 2);
        ctx.stroke();

        ctx.beginPath();
        ctx.lineWidth = tickmarkInset / 2;
        ctx.strokeStyle = "#222";
        ctx.arc(xCenter, yCenter, outerRadius - ctx.lineWidth / 2, outerRadius -ctx.lineWidth / 2, 0, Math.PI * 2);
        ctx.stroke();

        ctx.beginPath();
        var gradient = ctx.createRadialGradient(xCenter, yCenter, 0, xCenter, yCenter, outerRadius * 1.5);
        gradient.addColorStop(0, Qt.rgba(1, 1, 1, 0));
        gradient.addColorStop(0.7, Qt.rgba(1, 1, 1, 0.13));
        gradient.addColorStop(1, Qt.rgba(1, 1, 1, 1));
        ctx.fillStyle = gradient;
        ctx.arc(xCenter, yCenter, outerRadius - tickmarkInset, outerRadius - tickmarkInset, 0, Math.PI * 2);
        ctx.fill();
    }

    background: Canvas {
        onPaint: {
            var ctx = getContext("2d");
            ctx.reset();
            paintBackground(ctx);
        }

        Text {
            id: speedText
            font.pixelSize: toPixels(0.3)
            text: kphInt
            color: "white"
            horizontalAlignment: Text.AlignRight
            anchors.horizontalCenter: parent.horizontalCenter
            anchors.top: parent.verticalCenter
            anchors.topMargin: toPixels(0.1)

            readonly property int kphInt: control.value
        }
        Text {
            text: "km/h"
            color: "white"
            font.pixelSize: toPixels(0.09)
            anchors.top: speedText.bottom
            anchors.horizontalCenter: parent.horizontalCenter
        }
    }

    needle: Canvas {
        implicitWidth: needleBaseWidth
        implicitHeight: needleLength

        property real xCenter: width / 2
        property real yCenter: height / 2

        onPaint: {
            var ctx = getContext("2d");
            ctx.reset();

            ctx.beginPath();
            ctx.moveTo(xCenter, height);
            ctx.lineTo(xCenter - needleBaseWidth / 2, height - needleBaseWidth / 2);
            ctx.lineTo(xCenter - needleTipWidth / 2, 0);
            ctx.lineTo(xCenter, yCenter - needleLength);
            ctx.lineTo(xCenter, 0);
            ctx.closePath();
            ctx.fillStyle = Qt.rgba(0.66, 0, 0, 0.66);
            ctx.fill();

            ctx.beginPath();
            ctx.moveTo(xCenter, height)
            ctx.lineTo(width, height - needleBaseWidth / 2);
            ctx.lineTo(xCenter + needleTipWidth / 2, 0);
            ctx.lineTo(xCenter, 0);
            ctx.closePath();
            ctx.fillStyle = Qt.lighter(Qt.rgba(0.66, 0, 0, 0.66));
            ctx.fill();
        }
    }

    foreground: null
}

当我编译时,会出现以下消息。

代码语言:javascript
复制
qrc:/qml/qml/test.qml:9: TypeError: Cannot read property 'width' of null
qrc:/qml/qml/test.qml:10: TypeError: Cannot read property 'height' of null
qrc:/qml/qml/test.qml:20: ReferenceError: valueSource is not defined
qrc:/qml/qml/test.qml:11: TypeError: Cannot read property 'Center' of null

我想知道为什么会有这样的消息,以及如何解决。以及我如何改变QQuickWidget的背景色?

请帮帮我。

EN

回答 1

Stack Overflow用户

发布于 2021-01-29 16:21:59

根对象需要初始大小。不需要以父母为中心,因为没有父母。假设尺寸是500×300。

代码语言:javascript
复制
Item {
    id: container
    width: 500
    height: 300

或者,如果您不想给出一个恒定的大小,但是要使大小精确地适合内容,可以使用childrenRect。确保您的内容大小不依赖于根,并且在使用它之前有一个有效的宽度和高度。它可能会导致“检测到宽度/高度的绑定循环”。警告。

代码语言:javascript
复制
Item {
    id: container
    width: childrenRect.width
    height: childrenRect.height

如果您想要您的场景调整大小,尊重QQuickWidget的大小,动态设置调整大小模式。

代码语言:javascript
复制
ui->quickWidget->setResizeMode(QQuickWidget::SizeRootObjectToView);

让我们到着色点。要更改根项的颜色,可以使用Rectangle.color属性。因此,将根对象从项更改为矩形。让我们把背景变成红色。

代码语言:javascript
复制
Rectangle {
    id: container
    width: childrenRect.width
    height: childrenRect.height
    color: "red"

或者,如果要更改QQuickWidget的窗口颜色,请设置调色板。但既然你的场景会被掩盖,我怀疑这就是你所需要的。

代码语言:javascript
复制
auto palette = ui->quickWidget->palette();
palette.setColor(QPalette::Window, QColor(Qt::red));
ui->quickWidget->setPalette(palette);

你还有一个问题:

qrc:/qml/qml/test.qml:20: ReferenceError: valueSource is not defined

我不知道valueSource是什么,要么确保你有它,要么把它处理掉。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65956728

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档