PDA

View Full Version : how can i change textbox data remotely in qt?



rebwar
15th November 2017, 08:05
i have a client and server and a qml file that design the UI of my program.
in qml file there is only a textbox that i want to change the data of textbox to new data that received by client.
i added the files in below.
// main.cpp


#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQmlContext>
#include "test.h"
#include "test2.h"
#include "server.h"
#include <QDeclarativeComponent>
#include <qquickview.h>

int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
Server server;


qDebug() << "server is listening";


return app.exec();

}



// server.cc
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include "server.h"
#include <QDebug>
#include <iostream>
#include <QQmlContext>
#include "test.h"
#include "test2.h"
#include <QDeclarativeComponent>
#include <qquickview.h>

Server::Server(QObject* parent): QObject(parent)
{
connect(&server, SIGNAL(newConnection()),
this, SLOT(acceptConnection()));


server.listen(QHostAddress::Any, 8888);
qDebug() << "vasl shod";
}

Server::~Server()
{
server.close();
}

void Server::acceptConnection()
{
client = server.nextPendingConnection();
client->write("Hello client\r\n");

connect(client, SIGNAL(readyRead()),
this, SLOT(startRead()));
}

void Server::startRead()
{

QString data = QString(client->readAll());

client->waitForReadyRead(3000);
QString buf= client->readAll();
qDebug() << buf;
QQmlApplicationEngine engine;
engine.load(QUrl(QStringLiteral("qrc:///main.qml")));

QObject *rootObject = engine.rootObjects().first();
QObject *qmlObject = rootObject->findChild<QObject*>("ttt");
qmlObject ->setProperty("text","new data");

}



//main.qml
import QtQuick 2.0
import QtQuick 2.2
import QtQuick.Window 2.1
import QtQuick.Window 2.2
import QtQuick.Controls 1.2

Window{
width:400
height:400
visible:true

Item {
objectname:"ttt"
id: rootItem
property string text: "hello world"
Text {
x:0
y:0
visible: true
id: message
text: rootItem.text
}
}

}



when run and send data by client the window appear and disappear automatically and i don't know how can i correct it.

I really appreciate it.

d_stranz
15th November 2017, 16:44
when run and send data by client the window appear and disappear automatically

What do you think happens to the QQmlApplicationEngine instance that you create on the stack as a local variable in your startRead() method when that method exits?