简体   繁体   English

从C ++更新QML中的TextArea

[英]Update TextArea in QML from C++

My question is pretty simple I think. 我的问题很简单。 Nevertheless I was not able to figure it out. 但是,我无法弄清楚。 I have a TextArea defined in my .qml file, which needs to be updated dynamically from the C++ code. 我在.qml文件中定义了一个TextArea ,需要从C ++代码动态更新。

Unfortunately, I don't know how to access/update the TextArea from within the imserver.cpp class. 不幸的是,我不知道如何从imserver.cpp类中访问/更新TextArea

Can anyone help me out please? 有人可以帮我吗?

Here is the .qml file: 这是.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
        }  
    }

My main.cpp: 我的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 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: 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);
} 

There are several ways. 有几种方法。 Here is how I'd do it. 这是我的方法。

First you should register your IMServer class: 首先,您应该注册您的IMServer类:

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

Then you add your IMServer instance to QML: 然后,将您的IMServer实例添加到QML:

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

Your IMServer class then needs a signal, that your TextArea can be connected to, or even better add a property (you need to add the getText() function and textChanged() signal here as well, for a read-only property): 然后,您的IMServer类需要一个信号,您的TextArea可以连接到该信号,甚至更好地添加一个属性(对于只读属性,您还需要在此处添加getText()函数和textChanged()信号):

Updated: 更新:

Q_PROPERTY(QString text READ getText NOTIFY textChanged)

In the TextArea you can then create a binding: 然后,您可以在TextArea中创建绑定:

TextArea {
  text: imserver.text
}

Then whenever you emit textChanged in your IMServer class, the TextArea's text will be updated. 然后,无论何时在IMServer类中发出textChanged,TextArea的文本都会被更新。 For more information: http://doc.qt.io/qt-5/qtqml-cppintegration-topic.html 有关更多信息: http : //doc.qt.io/qt-5/qtqml-cppintegration-topic.html

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

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