从QML文本输入更改c ++类属性

时间:2020-04-22 08:19:38

标签: c++ qt qml

好的,所以标题恰好说明了我想做的事情。我将向您展示代码和存在的问题。

我想做什么


我正在尝试通过单击在textInput中输入具有自定义文件名的按钮来打开文件。此textInput通过我的AntennaFile类进行处理。文件打开是在openButtonClicked函数中进行的。

我遇到的问题


我的m_fileName属性在openButtonClicked函数中没有值,即使它在setFileName中也是一个值(我在调试时使用了断点和{{1} }。

那么如何使属性不“失去”价值呢?我肯定会错过一些非常简单且重要的东西,所有这些东西都无法正常工作,这让我发疯。

所以这是所有需要的代码(我想吗?),所以您可以确切地看到我所做的事情。

antennafile.cpp

qDebug()

antennafile.h

#include "antennafile.h"

AntennaFile::AntennaFile(QObject *parent) : QObject(parent)
{
}

QString AntennaFile::getFileName()
{
    return m_fileName;
}

void AntennaFile::setFileName(const QString &fileName)
{
    if (fileName != m_fileName)
    {
        m_fileName = fileName;
        emit fileNameChanged();
        qDebug() << m_fileName; // Here, m_fileName has the value of what I typed in the textInput
    }
}

void AntennaFile::processFile(QString fileName, QVector<QString> &vecOfStrs)
{
    // Code removed for smaller post
}

void AntennaFile::openButtonClicked()
{
    QVector<QString> vecOfStrs;
    QString nameOfFile = "../QMLStageProject/AntennaTypes/";
    nameOfFile.append(m_fileName);
    qDebug() << nameOfFile; // Here, m_fileName is empty, hence, nothing is changed to nameOfFile

    processFile(nameOfFile, vecOfStrs);
}

main.cpp

#ifndef AntennaFile_H
#define AntennaFile_H

#include "framework.h"
#include "antennadata.h"

class AntennaFile : public QObject
{
    Q_OBJECT
    Q_PROPERTY(QString fileName READ getFileName WRITE setFileName NOTIFY fileNameChanged)

public:
    explicit AntennaFile(QObject *parent = 0);
    QString getFileName();
    void setFileName(const QString &fileName);

private:
    void processFile(QString fileName, QVector<QString> & vecOfStrs);

public slots:
    void  openButtonClicked();

signals:
    void fileNameChanged();

private:
    QString m_fileName;
};

#endif // AntennaFile_H

main.qml-并非全部代码,只是所需的

#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();
}

1 个答案:

答案 0 :(得分:1)

您有两个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);