简体   繁体   English

Qt-连接C ++和QML-ReferenceError:未定义xxx

[英]Qt - Connecting C++ and QML - ReferenceError: xxx is not defined

I am new to Qt and am currently trying to connect a c++ part of my program with the qml part. 我是Qt的新手,目前正在尝试将程序的c ++部分与qml部分连接起来。 The goal of th connection is to pass the currently selected item of a TreeView to qml (possibly via on_treeView_doubleClicked). 连接的目的是将TreeView的当前选定项传递给qml(可能通过on_treeView_doubleClicked)。 Since that didnt work I tried all the suggested connections on here for a very basic programm, but I am always receiving the following error: 既然没有用,我尝试了所有建议的连接,在这里进行了非常基本的编程,但是我总是收到以下错误:

file::/main.qml:10: ReferenceError: test is not defined

This is the piece of my code regarding the connection: 这是我有关连接的代码片段:

test.h: test.h:

#ifndef TEST_H
#define TEST_H

#include <QObject>
#include <QDebug>

class test : public QObject
{
    Q_OBJECT
public:
    explicit test(QObject *parent = nullptr);
    Q_INVOKABLE void testFunc();

signals:

public slots:
};

#endif // TEST_H

test.cpp: TEST.CPP:

#include "test.h"

test::test(QObject *parent) : QObject(parent)
{

}

void test::testFunc()
{
    qDebug() <<  "Hello from C++!";
}

mainwindow.cpp: mainwindow.cpp:

test testObj;
QQmlApplicationEngine engine;
ui->quickWidget->setSource(QUrl::fromLocalFile(":/main.qml"));

engine.rootContext()->setContextProperty("test", &testObj);

main.qml: main.qml:

import QtQuick 2.0
import QtQuick.Window 2.3

Item {
    Timer{
        id: timer
        interval: 1000; running: true; repeat:true
        onTriggered:{
            console.log("ex")
            test.testFunc();
        }
    }
}

I would be really thankful for any help (not only for the simple programm, but possibly for passing the currently selected item of my treeView). 我将非常感谢您提供的任何帮助(不仅是简单的编程,而且还有可能传递了treeView中当前选择的项)。 I know there aa couple suggestions in other threads, but they dont seem to work for me, so please dont mark this as duplicate. 我知道其他线程中有一些建议,但它们似乎对我没有用,所以请不要将此标记为重复。

Thanks in advance, 提前致谢,

Lucas 卢卡斯

You have 3 main errors: 您有3个主要错误:

  • If you are going to use a .qml from a qresource you should not use QUrl::fromLocalFile() since the qresource is not a local file but virtual. 如果要使用来自qresource的.qml,则不应使用QUrl::fromLocalFile()因为qresource不是本地文件,而是虚拟文件。

  • testObj is a local variable so it will be eliminated as soon as the function where it was created is finished, one solution is to make testObj a member of the class. testObj是一个局部变量,因此将在创建该函数的函数完成后将其消除,一种解决方案是使testObj成为testObj的成员。

  • And for the last one, the main error, QQuickWidget already has a QQmlEngine , you do not have to create another one. 对于最后一个错误,主要错误QQuickWidget已经具有QQmlEngine ,您不必创建另一个。


*.h *。H

Ui::MainWindow *ui;
test testObj;

*.cpp *的.cpp

ui->quickWidget->setSource(QUrl("qrc:/main.qml"));
ui->quickWidget->engine()->rootContext()->setContextProperty("test", &testObj);

My full test can be found at the following link 我的完整测试可以在以下链接中找到

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM