[SOLVED] Add a QTreeView in QML

Issue

I would like to register a QTreeView c++ object to QML.
I tried to register it like this:

main.cpp:

qmlRegisterType<QTreeView>("com.MyApp.QTreeView", 1, 0, "QTreeView");

relevant code in main.qml

import com.MyApp.QTreeView 1.0

QWindow {
    QTreeView{
        headerHidden: true
    }
}

Result: it compiles. headerHidden property is found so it is registered correctly. However I have an error at runtime:
ASSERT: "!d->isWidget" in file kernel\qobject.cpp, line 2090

Solution

QWidgets are not directly compatible with QML such that they can be embedded in a QML view. They are two different UI technologies and cannot be used together in that fashion.

You can however embed a QML view inside of a QWidget hierarchy:

https://www.ics.com/blog/combining-qt-widgets-and-qml-qwidgetcreatewindowcontainer

Or just use the QML TreeView component instead:

https://doc.qt.io/qttreeview/qml-treeview.html

Answered By – David K. Hess

Answer Checked By – Timothy Miller (BugsFixing Admin)

Leave a Reply

Your email address will not be published. Required fields are marked *