从 C++ 更新 QML 中的 TextArea
Update TextArea in QML from C++
我想我的问题很简单。尽管如此,我还是无法弄清楚。我在我的 .qml
文件中定义了一个 TextArea
,它需要从 C++ 代码动态更新。
不幸的是,我不知道如何从 imserver.cpp
class.
中 access/update 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);
}
有几种方法。这是我的做法。
首先你应该注册你的 IMServer class:
qmlRegisterUncreatableType<IMServer>("App", 1, 0, "IMServer", "");
然后将 IMServer 实例添加到 QML:
enigne.rootContext()->setContextProperty("imserver", &server);
你的 IMServer class 然后需要一个信号,你的 TextArea 可以连接到,或者甚至更好地添加一个 属性 (你需要添加 getText() 函数和 textChanged() 信号这里也是,只读 属性):
更新:
Q_PROPERTY(QString text READ getText NOTIFY textChanged)
然后您可以在 TextArea 中创建绑定:
TextArea {
text: imserver.text
}
然后,每当您在 IMServer class 中发出 textChanged 时,TextArea 的文本就会更新。
更多信息:http://doc.qt.io/qt-5/qtqml-cppintegration-topic.html
我想我的问题很简单。尽管如此,我还是无法弄清楚。我在我的 .qml
文件中定义了一个 TextArea
,它需要从 C++ 代码动态更新。
不幸的是,我不知道如何从 imserver.cpp
class.
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);
}
有几种方法。这是我的做法。
首先你应该注册你的 IMServer class:
qmlRegisterUncreatableType<IMServer>("App", 1, 0, "IMServer", "");
然后将 IMServer 实例添加到 QML:
enigne.rootContext()->setContextProperty("imserver", &server);
你的 IMServer class 然后需要一个信号,你的 TextArea 可以连接到,或者甚至更好地添加一个 属性 (你需要添加 getText() 函数和 textChanged() 信号这里也是,只读 属性):
更新:
Q_PROPERTY(QString text READ getText NOTIFY textChanged)
然后您可以在 TextArea 中创建绑定:
TextArea {
text: imserver.text
}
然后,每当您在 IMServer class 中发出 textChanged 时,TextArea 的文本就会更新。 更多信息:http://doc.qt.io/qt-5/qtqml-cppintegration-topic.html