升级到 Qt 5.15 后,ListView 委托中的父级为空

Rib*_*oks 5 c++ qt qml qt5 qtquick2

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

文件 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 }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

文件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();
}
Run Code Online (Sandbox Code Playgroud)

链接到完整的源代码

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

Mit*_*tch 8

这是 Qt 5.15 中行为改变的结果。据报道第一个问题在这里,有一个更详细的总结在这里。文档已更新,但可能需要更长的时间才能将其发布到网站上。在新的文件说:

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

ListView {
    id: listView
    // ...

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

        // Correct.
        width: listView.width
        width: ListView.view.width
        // ...
    }
}
Run Code Online (Sandbox Code Playgroud)

因此,您可以:

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

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

选项 1 导致少QObject创建一个(每个附加对象都是 a QObject),但将委托绑定到该特定视图。

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

  • @AlekseyKontsevich如果您锚定到父级,那么您现在需要更改该绑定以引用“listView.contentItem”,因为那是父级,而不是视图本身。 (3认同)
  • 感谢您的详细解释! (2认同)
  • 第一个不起作用 - 返回错误:“无法锚定到不是父项或同级项的项目。” (2认同)