【问题标题】:Parent is null in ListView delegate after upgrade to Qt 5.15升级到 Qt 5.15 后,ListView 委托中的父级为空
【发布时间】:2020-09-06 18:40:45
【问题描述】:

如果尝试设置它的委托anchors 属性并滚动列表(这会使委托被销毁/创建),带有最简单委托的ListView 会产生大量警告"21:35:31.911 warning T#16084047 unknown - qrc:/main.qml:15: TypeError: Cannot read property 'left' of null"。在 Qt 5.12 或 5.9 中并非如此。

文件main.qml

import QtQuick 2.15
import QtQuick.Window 2.15

Window {
    width: 640
    height: 480
    visible: true
    title: qsTr("Hello World")

    ListView {
        anchors.fill: parent
        model: cppModel

        delegate: Rectangle {
            anchors.left: parent.left
            anchors.right: parent.right
            height: 50

            Text { text: model.itemName }
        }
    }
}

文件main.cpp

#include <QAbstractListModel>
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QtGlobal>
#include <QQmlContext>

#include <iostream>

void myMessageHandler(QtMsgType type, const QMessageLogContext &context, const QString &msg) {
    QString logLine = qFormatLogMessage(type, context, msg);
    std::cout << logLine.toStdString() << std::endl;
}

class CppModel: public QAbstractListModel {
    // QAbstractItemModel interface
public:
    virtual int rowCount(const QModelIndex &parent) const override { return 100; }
    virtual QVariant data(const QModelIndex &index, int role) const override {
        if (role == (Qt::DisplayRole + 1)) {
            return QString("Element %1").arg(index.row());
        }
        return QVariant();
    }
    virtual QHash<int, QByteArray> roleNames() const override {
        return {{(Qt::DisplayRole+1), "itemName"}};
    }
};

int main(int argc, char *argv[])
{
    qSetMessagePattern("%{time hh:mm:ss.zzz} %{type} T#%{threadid} %{function} - %{message}");
    qInstallMessageHandler(myMessageHandler);

    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);

    QGuiApplication app(argc, argv);

    QQmlApplicationEngine engine;
    CppModel cppModel;
    engine.rootContext()->setContextProperty("cppModel", &cppModel);

    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();
}

Link to full source code

我做错了什么以及如何为委托元素正确设置anchors

【问题讨论】:

  • 嗯,你为什么要这样使用anchors?对我来说,width: parent.width 似乎会更好。还给 ListView 一个 id 并通过它引用它而不是 parent
  • @Megued 如果您再次阅读该问题,您会发现问题在于parentnull,而不是parent.leftnull。代理的父级也是contentItem,而不是ListView 本身,因此通过ID 引用它是不正确的。
  • 我知道错误在说什么,这就是为什么我建议使用ListView作为参考,因为contentItem显然不存在,并且由于ListView是它的父级,我不'在使用它获得相同结果时没有发现任何问题,只是没有错误。

标签: c++ qt qml qt5 qtquick2


【解决方案1】:

这是 Qt 5.15 中行为更改的结果。第一个问题报告了here,更详细的摘要herenew documentation 说:

委托会根据需要进行实例化,并且可以随时销毁。 因此,状态永远不应该存储在委托中。代表们是 通常是 ListView 的 contentItem 的父级,但通常取决于 无论它在视图中是否可见,父级都可以更改,并且 有时为空。因此,绑定到父级的属性 不建议从代表内部。如果你想要委托 要填写 ListView 的宽度,请考虑使用以下之一 改为以下方法:

ListView {
    id: listView
    // ...

    delegate: Item {
        // Incorrect.
        width: parent.width

        // Correct.
        width: listView.width
        width: ListView.view.width
        // ...
    }
}

所以,你可以:

  1. ListView 一个id 并在绑定中使用它而不是parent
  2. 使用附加属性 (ListView.view) 访问视图。
  3. 检查是否为空 (anchors.left: parent ? parent.left : undefined)。

选项 1 和 2 将产生更清晰的代码。

选项 1 会减少创建一个 QObject(每个附加对象都是一个 QObject),但会将委托与该特定视图相关联。

选项 2 更适合作为独立组件的委托,这些组件将与其他 ListViews 一起重用。

【讨论】:

  • 感谢详细解释!
  • 第一个不起作用 - 返回错误:Cannot anchor to an item that isn't a parent or sibling.
  • 第三个表达式也错误:anchors.left 不能是 null。正确一个:anchors { left: parent ? parent.left : undefined; right: parent ? parent.right : undefined }
  • @AlekseyKontsevich 如果您锚定到父级,您现在需要更改该绑定以引用listView.contentItem,因为那是父级,而不是视图本身。
  • @Mitch 这里的重点是该项目有时可能没有父项。锚定不起作用。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-02-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多