Unable to assing [undefined] to QString
Solved
QML and Qt Quick
-
I have encountered a problem that I have been trying to avoid for months. It is that I cannot assign a variable that I get from outside QML, that is, in the backend with C++ and it is giving me errors when assigning an external variable to something within QML. I will show you the entire program below.
main.cpp:#include <QGuiApplication> #include <QQmlApplicationEngine> #include <stringHandler.h> int main( int argc, char *argv[]) { QGuiApplication app(argc, argv); QQmlApplicationEngine engine; StringHandler stringHandler; stringHandler.loadData(); engine.rootContext()->setContextProperty("stringHandler", &stringHandler); QObject::connect( &engine, &QQmlApplicationEngine::objectCreationFailed, &app, []() { QCoreApplication::exit(-1); }, Qt::QueuedConnection); engine.loadFromModule("untitled4", "Main"); return app.exec(); }
stringHandler.h:
#ifndef STRINGHANDLER_H #define STRINGHANDLER_H #include <QObject> #include <QStringListModel> #include <vector> #include <string> #include <QQmlContext> class StringHandler : public QObject { Q_OBJECT Q_PROPERTY(QStringListModel* model READ model NOTIFY modelChanged) public: explicit StringHandler(QObject *parent = nullptr) : QObject(parent), m_model(new QStringListModel(this)) {} QStringListModel *model() const{ return m_model; } Q_INVOKABLE void loadData() { QStringList list; const std::vector<std::string> data = {"Mapa 1", "Mapa 2", "Mapa 3"}; for (const std::string &str : data) { qDebug() << "Loading: " << QString::fromStdString(str); // Depuración list.append(QString::fromStdString(str)); } m_model->setStringList(list); emit modelChanged(); // Notificar a QML } signals: void modelChanged(); private: QStringListModel *m_model; }; #endif // STRINGHANDLER_H
Main.qml
import QtQuick 2.15 import QtQuick.Window 2.15 import QtQuick.Controls 2.15 Window { // width: 640 // height: 480 visible: true title: qsTr("Hello World") Rectangle { visible: true width: 500 height: 400 // ScrollView para contener el ListView ScrollView { anchors.fill: parent clip: true // ListView para mostrar los elementos ListView { id: listView model: stringHandler.model // Modelo que se enlaza con el StringHandler de C++ delegate: Item { width: listView.width height: 70 Rectangle { width: parent.width height: 60 color: "#ffffff" border.color: "#cccccc" radius: 10 Row { anchors.fill: parent spacing: 20 anchors.margins: 10 Text { text: modelData // Se accede al modelo de datos font.pixelSize: 18 color: "#000000" anchors.verticalCenter: parent.verticalCenter } Button { text: "Editar" anchors.verticalCenter: parent.verticalCenter onClicked: console.log("Editar", modelData) } Button { text: "Borrar" anchors.verticalCenter: parent.verticalCenter onClicked: console.log("Borrar", modelData) } } } } } } } }
The output:
QML debugging is enabled. Only use this in a safe environment. Loading: "Mapa 1" Loading: "Mapa 2" Loading: "Mapa 3" qrc:/qt/qml/untitled4/Main.qml:42:33: Unable to assign [undefined] to QString qrc:/qt/qml/untitled4/Main.qml:42:33: Unable to assign [undefined] to QString qrc:/qt/qml/untitled4/Main.qml:42:33: Unable to assign [undefined] to QString qrc:/qt/qml/untitled4/Main.qml:23: TypeError: Cannot read property 'model' of null
CMakeLists.txt:
cmake_minimum_required(VERSION 3.16) project(untitled4 VERSION 0.1 LANGUAGES CXX) set(CMAKE_CXX_STANDARD_REQUIRED ON) find_package(Qt6 REQUIRED COMPONENTS Quick) qt_standard_project_setup(REQUIRES 6.5) qt_add_executable(appuntitled4 main.cpp ) qt_add_qml_module(appuntitled4 URI untitled4 VERSION 1.0 QML_FILES Main.qml SOURCES stringHandler.h ) # Qt for iOS sets MACOSX_BUNDLE_GUI_IDENTIFIER automatically since Qt 6.1. # If you are developing for iOS or macOS you should consider setting an # explicit, fixed bundle identifier manually though. set_target_properties(appuntitled4 PROPERTIES # MACOSX_BUNDLE_GUI_IDENTIFIER com.example.appuntitled4 MACOSX_BUNDLE_BUNDLE_VERSION ${PROJECT_VERSION} MACOSX_BUNDLE_SHORT_VERSION_STRING ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR} MACOSX_BUNDLE TRUE WIN32_EXECUTABLE TRUE ) target_link_libraries(appuntitled4 PRIVATE Qt6::Quick ) include(GNUInstallDirs) install(TARGETS appuntitled4 BUNDLE DESTINATION . LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} )
I have the same problem when i use a .qml as a header where I have all the constanst
recommendation I'm all ears thank you very much
-
Use default role display for QStringListModel like this,
Text { text: display // Se accede al modelo de datos font.pixelSize: 18 color: "#000000" anchors.verticalCenter: parent.verticalCenter }
-
Amazing thank you very much ^^
-