【问题标题】:Update TextArea in QML from C++从 C++ 更新 QML 中的 TextArea
【发布时间】:2015-09-09 00:52:56
【问题描述】:

我认为我的问题很简单。尽管如此,我还是无法弄清楚。我在.qml 文件中定义了一个TextArea,它需要从C++ 代码动态更新。

很遗憾,我不知道如何从 imserver.cpp 类中访问/更新 TextArea

谁能帮帮我?

这是 .qml 文件:

    import QtQuick 2.2
    import QtQuick.Controls 1.1


    ApplicationWindow {
        visible: true
        width: 640
        height: 480

        title: qsTr("IMServer")

        menuBar: MenuBar {
        Menu {
            title: qsTr("File")
            MenuItem {
            text: qsTr("Exit")
            onTriggered: Qt.quit();
            }
        }
        }

        TextArea {
        id: serverInformation
        x: 0
        y: 0
        width: 247
        height: 279
        }  
    }

我的 main.cpp:

    #include <QApplication>
    #include <QQmlApplicationEngine>
    #include <QQmlContext>
    #include <QQmlEngine>
    #include <QtQml>

    #include "imserver.h"

    int main(int argc, char *argv[]) {
        QApplication app(argc, argv);

        QQmlApplicationEngine engine;
        engine.load(QUrl(QStringLiteral("qrc:///main.qml")));

        IMServer server(2000);

        qmlRegisterUncreatableType<IMServer>("App", 1, 0, "IMServer", "");
        engine.rootContext()->setContextProperty("imserver", &server);

        server.startServer();

        return app.exec();
    }

imserver.h

#ifndef IMSERVER_H
#define IMSERVER_H

#include <QTcpServer>
#include <QTcpSocket>
#include <QAbstractSocket>
#include <QThreadPool>

class IMServer : public QTcpServer {

    Q_OBJECT
    Q_PROPERTY(QString text WRITE setText NOTIFY textChanged)

public:
    explicit IMServer(int port, QObject *parent = 0);
    void startServer();
    void setText(const QString &txt);

signals:
    void textChanged();

public slots:

protected:
    void incomingConnection(qintptr fd);

private:
    int port;
    QThreadPool *pool;
    QString m_text;
};


#endif // IMSERVER_H

imserver.cpp:

#include "imserver.h"
#include "clienthandler.h"

IMServer::IMServer(int port, QObject *parent) : QTcpServer(parent) {
    this->pool = new QThreadPool(this);
    this->pool->setMaxThreadCount(100);
    this->port = port;
}

void IMServer::startServer() {

    setText("TEST");

    if (!this->listen(QHostAddress::Any, this->port)) {
    qDebug() << "Server could not be started";
    } else {
    qDebug() << "Server started, listening...";
    }
}

void IMServer::setText(const QString &txt) {
    m_text = txt;
    emit textChanged();
}


void IMServer::incomingConnection(qintptr fd) {
    ClientHandler *client = new ClientHandler();
    client->setAutoDelete(true);
    client->fd = fd;
    this->pool->start(client);
} 

【问题讨论】:

    标签: qt qml qtquick2


    【解决方案1】:

    有几种方法。这是我的做法。

    首先你应该注册你的 IMServer 类:

    qmlRegisterUncreatableType<IMServer>("App", 1, 0, "IMServer", "");
    

    然后将您的 IMServer 实例添加到 QML:

    enigne.rootContext()->setContextProperty("imserver", &server);
    

    然后,您的 IMServer 类需要一个信号,您的 TextArea 可以连接到该信号,或者甚至更好地添加一个属性(您还需要在此处添加 getText() 函数和 textChanged() 信号,以获得只读属性):

    更新:

    Q_PROPERTY(QString text READ getText NOTIFY textChanged)
    

    然后您可以在 TextArea 中创建绑定:

    TextArea {
      text: imserver.text
    }
    

    然后,当您在 IMServer 类中发出 textChanged 时,TextArea 的文本将被更新。 更多信息:http://doc.qt.io/qt-5/qtqml-cppintegration-topic.html

    【讨论】:

    • 我将 qmlRegisterUncreatableType&lt;IMServer&gt;("App", 1, 0, "IMServer", ""); 放在 main.cpp 文件中,并包含 #include &lt;QQmlEngine&gt;QT += qml 也在 .pro 文件中,但我仍然收到错误 'qmlRegisterUncreatableType is not declared in这个范围`?
    • #include 来使用这个函数。
    • Ok thx,您能解释一下在将上述属性放入头文件后要设置哪些信号吗?然后如何设置 textarea 值?
    • 你需要一个信号“void textChanged();”它会像“void setText(const QString& txt) { m_text = txt; emit textChanged(); }”一样使用
    • 我现在更新了代码,但是Q_Property 中的 MEMBER 给了我一个错误,所以我尝试改用 WRITE。我尝试设置到 textarea 的值没有写入...
    猜你喜欢
    • 2018-05-23
    • 1970-01-01
    • 2020-08-18
    • 1970-01-01
    • 2020-06-24
    • 1970-01-01
    • 2018-06-12
    • 2019-04-18
    • 2013-03-11
    相关资源
    最近更新 更多