PDA

View Full Version : Textarea is not Updating when set from C++



Cerberus
6th September 2015, 12:09
Hi,
I am new to Qt and therefore this question might be easy to solve (unfortunately not for me).
I have to update a textarea dynamically from C++ code.
Here is what I have so far:



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
}
}




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




#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




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


But the textarea is not updating, can anyone pls tell me what I missed?

anda_skoa
6th September 2015, 15:39
Why should be it "updating"?
There is no code that would interact in any way with the text area.

Cheers,
_

Cerberus
6th September 2015, 18:35
So what would I have to add/change to make it work?

anda_skoa
6th September 2015, 22:41
Well, it depends on what you want to do.

My guess is that you want the text property of the IMServer object to be displayed by the TextArea.

For that you need to use the exported object in QML, also obviously export it before you load the QML and, also very obviously, make the property readable.

I would suggest to fix this in the reverse order, i.e. start with the property currently not being readable.

Cheers,
_