Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/135.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 更改a c++;来自QML文本输入的类属性_C++_Qt_Qml - Fatal编程技术网

C++ 更改a c++;来自QML文本输入的类属性

C++ 更改a c++;来自QML文本输入的类属性,c++,qt,qml,C++,Qt,Qml,好吧,标题正好说明了我要做的。我会告诉你我的代码和问题 我想做什么 我试图通过单击按钮打开一个文件,该按钮在textInput中输入了自定义文件名。此textInput通过我的AntennaFile类处理。文件打开发生在openButtonClicked函数中 我的问题 我的m_fileName属性在openButtonClicked函数中没有值,即使它在setFileNameone中有一个值(我使用断点和qDebug()签入调试) 那么如何使属性不“丢失”它的值呢?我肯定错过了一些非常简单

好吧,标题正好说明了我要做的。我会告诉你我的代码和问题

我想做什么


我试图通过单击按钮打开一个文件,该按钮在
textInput
中输入了自定义文件名。此
textInput
通过我的
AntennaFile
类处理。文件打开发生在
openButtonClicked
函数中

我的问题


我的
m_fileName
属性在
openButtonClicked
函数中没有值,即使它在
setFileName
one中有一个值(我使用断点和
qDebug()
签入调试)

那么如何使属性不“丢失”它的值呢?我肯定错过了一些非常简单和重要的东西,让所有这些都发挥作用,这让我发疯

所以这里是所有需要的代码(我想?),所以你可以确切地看到我做了什么

antennafile.cpp


您有两个AntennaFile实例!第一个作为上下文属性。main.qml中的第二个。您的“文本编辑”和“编辑”按钮使用的不是同一个! 像这样更改代码应该可以:

import QtQuick 2.12
import QtQuick 2.6
import QtQuick.Window 2.12
import QtQuick.Controls 1.4
import QtQuick.Controls 2.0
import QtQuick.Controls 2.14
import QtCharts 2.3

import AntennaFile 1.0;
import AntennaTableModel 1.0;

Window
{
    id: window
    visible: true
    width: 950
    height: 680
    title: qsTr("QML Stage Project")

    AntennaFile {
        id: antennafile
    }

    TextInput {
        id: antennaTextInput
        x: 92
        y: 14
        width: 351
        height: 20
        text: antennafile.fileName
        font.pixelSize: 17
        onTextChanged: antennafile.fileName = text
    }

    Button {
        id: openButton
        x: 363
        y: 0
        width: 170
        height: 31
        text: qsTr("Open")
        font.pixelSize: 15
        onClicked: antennafile.openButtonClicked();
    }
}
实际上,您可以从主菜单中删除以下行:

AntennaFile antennaFile;
engine.rootContext()->setContextProperty("_AntennaFile", &antennaFile);

代码中不清楚QML中何时/如何调用“openButtonClicked”。您可以将其添加到示例中吗?还有一个可能的错误是:您有两个AntennaFile实例!第一个是在主as上下文属性“_antenafile”中。第二个是直接在main中声明的。qml刚刚编辑过它!我忘了,对不起^^那么我该如何使这两个实例“链接”/只有一个实例?你是生命救世主!谢谢你的帮助!我不敢相信它这么简单,但同时我离它还很远。如果可以这样做的话,我不会真正使用setContextProperty,但是我会在这个主题上做一些研究。再次感谢!当您希望在C++中保持对象的所有权时,使用SETCONTeXT属性。这样,对象只实例化一次,可以在多个QML文件中使用,也可以在C++中使用。这对西格尔顿来说特别有用。您可以通过从QML中删除触角文件{}项,并仅使用C++中声明的“TyAtnnFaFILE”属性来测试代码,谢谢这个答案,非常有用的信息。我很高兴我所有的问题都得到了回答。别紧张!
#include "framework.h"
#include "antennafile.h"
#include "antennatablemodel.h"

int main(int argc, char *argv[])
{
    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
    QGuiApplication app(argc, argv);
    QQmlApplicationEngine engine;

    const QUrl url(QStringLiteral("qrc:/main.qml"));

    AntennaFile antennaFile;
    engine.rootContext()->setContextProperty("_AntennaFile", &antennaFile);

    // Registers
    qmlRegisterType<AntennaFile>("AntennaFile", 1, 0, "AntennaFile");
    qmlRegisterType<AntennaTableModel>("AntennaTableModel", 1, 0, "AntennaTableModel"); // An other class

    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();
}
import QtQuick 2.12
import QtQuick 2.6
import QtQuick.Window 2.12
import QtQuick.Controls 1.4
import QtQuick.Controls 2.0
import QtQuick.Controls 2.14
import QtCharts 2.3

import AntennaFile 1.0;
import AntennaTableModel 1.0;

Window
{
    id: window
    visible: true
    width: 950
    height: 680
    title: qsTr("QML Stage Project")

    AntennaFile {
        id: antennafile
    }

    TextInput {
        id: antennaTextInput
        x: 92
        y: 14
        width: 351
        height: 20
        text: antennafile.fileName
        font.pixelSize: 17
        onTextChanged: antennafile.fileName = text
    }

    Button {
        id: openButton
        x: 363
        y: 0
        width: 170
        height: 31
        text: qsTr("Open")
        font.pixelSize: 15
        onClicked: _AntennaFile.openButtonClicked();
    }
}
import QtQuick 2.12
import QtQuick 2.6
import QtQuick.Window 2.12
import QtQuick.Controls 1.4
import QtQuick.Controls 2.0
import QtQuick.Controls 2.14
import QtCharts 2.3

import AntennaFile 1.0;
import AntennaTableModel 1.0;

Window
{
    id: window
    visible: true
    width: 950
    height: 680
    title: qsTr("QML Stage Project")

    AntennaFile {
        id: antennafile
    }

    TextInput {
        id: antennaTextInput
        x: 92
        y: 14
        width: 351
        height: 20
        text: antennafile.fileName
        font.pixelSize: 17
        onTextChanged: antennafile.fileName = text
    }

    Button {
        id: openButton
        x: 363
        y: 0
        width: 170
        height: 31
        text: qsTr("Open")
        font.pixelSize: 15
        onClicked: antennafile.openButtonClicked();
    }
}
AntennaFile antennaFile;
engine.rootContext()->setContextProperty("_AntennaFile", &antennaFile);