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();
}
#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();
}
To copy to clipboard, switch view to plain text mode
// 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>
{
connect(&server, SIGNAL(newConnection()),
this, SLOT(acceptConnection()));
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()
{
client->waitForReadyRead(3000);
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");
}
// 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");
}
To copy to clipboard, switch view to plain text mode
//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
}
}
}
//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
}
}
}
To copy to clipboard, switch view to plain text mode
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.
Bookmarks