检测到QML绑定循环的属性宽度(TextMetrics行为异常)

时间:2019-08-13 09:41:32

标签: c++ qt user-interface qml

我遇到一个绑定循环问题,该代码实际上有效,但是我想摆脱这些警告。

如果我从calculateMaxWidth()函数返回一个恒定值,则没有警告,但是一旦我使用TextMetrics,事情就会变糟。有趣的是,对于for循环中的每个新循环,我都会收到越来越多的警告。如果将TextMetrics创建的内容与destroy一起放入for循环中,则对于每个测量的文本都会得到一个警告。

import QtQuick 2.12
import QtQuick.Window 2.12

Window {
    id: theWindow
    visible: true
    width: 640
    height: 480
    title: qsTr("Binding loop! Why?")

    property var mydata: ['somethinglonglikecucumber', 'tomato', 'potato']

    function calculateMaxWidth() {
        var maxWidth = 0;

        var textMetrics = Qt.createQmlObject('import QtQuick 2.12; TextMetrics {} ', theWindow);
        textMetrics.font.pixelSize = 12;

        for(var i=0; i<mydata.length; i++) {
            textMetrics.text = mydata[i];
            console.log("width of " + textMetrics.text + " is " + textMetrics.width)
            maxWidth = Math.max(maxWidth, textMetrics.width);
        }

        textMetrics.destroy();
        return maxWidth;
    }

    Rectangle {
        width: calculateMaxWidth()
        height: 30 * mydata.length
        color: "yellow"
    }
}

这是默认的main.cpp

#include <QGuiApplication>
#include <QQmlApplicationEngine>

int main(int argc, char *argv[])
{
    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);

    QGuiApplication app(argc, argv);

    QQmlApplicationEngine engine;
    const QUrl url(QStringLiteral("qrc:/main.qml"));
    QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
                     &app, [url](QObject *obj, const QUrl &objUrl) {
        if (!obj && url == objUrl)
            QCoreApplication::exit(-1);
    }, Qt::QueuedConnection);
    engine.load(url);

    return app.exec();
}

qrc:/main.qml:29:5:QML矩形:检测到属性“宽度”的绑定循环

1 个答案:

答案 0 :(得分:1)

对TextMetrics / FontMetrics属性的分配以某种方式导致绑定循环警告。但是,查询FontMetrics的boundingRect不会创建分配,因此不会出现警告。另外,在创建FontMetrics对象期间,我还必须将字体大小作为属性绑定传递。

       at com.caretool.controller.UsageEmailController.getEmailReport(UsageEmailController.java:35)
        at com.caretool.scheduler.UsageReportMailTask.run(UsageReportMailTask.java:15)
        at java.util.TimerThread.mainLoop(Timer.java:555)
        at java.util.TimerThread.run(Timer.java:505)
2019-08-12 20:32:00 ERROR DBUtil:193 - DBUtil fetchlist1 error:
org.hibernate.hql.internal.ast.QuerySyntaxException: **expecting CLOSE, found '(' near line 2, column 10** [select new map( COUNT(distinct username) as DistinctUser,
 COUNT(IF(searchParam='Account Sync',1,NULL)) AS 'AccountSync',
 COUNT(IF(searchParam='Account Info',1,NULL)) AS 'AccountInfo',
 COUNT(IF(searchParam='spectrum.net',1,NULL)) AS 'spectrum.net',
 COUNT(IF(searchParam='STVA',1,NULL)) AS 'STVA',
 COUNT(IF(searchParam like 'TVE%',1,NULL)) AS 'TVE',
 COUNT(IF(searchParam='sbnet',1,NULL)) AS 'sbnet',
 COUNT(IF(searchParam='OOH Info',1,NULL)) AS 'OOHInfo') from com.caretool.bean.dbBean.CareToolAuditBO where date(accesseddate)='2019-08-12' AND username<>'Scheduled']
        at org.hibernate.hql.internal.ast.QuerySyntaxException.convert(QuerySyntaxException.java:91)
        at org.hibernate.hql.internal.ast.ErrorCounter.throwQueryException(ErrorCounter.java:109)
        at org.hibernate.hql.internal.ast.QueryTranslatorImpl.parse(QueryTranslatorImpl.java:304)