Results 1 to 2 of 2

Thread: how can i change textbox data remotely in qt?

  1. #1

    Default how can i change textbox data remotely in qt?

    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
    Qt Code:
    1. #include <QGuiApplication>
    2. #include <QQmlApplicationEngine>
    3. #include <QQmlContext>
    4. #include "test.h"
    5. #include "test2.h"
    6. #include "server.h"
    7. #include <QDeclarativeComponent>
    8. #include <qquickview.h>
    9.  
    10. int main(int argc, char *argv[])
    11. {
    12. QGuiApplication app(argc, argv);
    13. Server server;
    14.  
    15.  
    16. qDebug() << "server is listening";
    17.  
    18.  
    19. return app.exec();
    20.  
    21. }
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. // server.cc
    2. #include <QGuiApplication>
    3. #include <QQmlApplicationEngine>
    4. #include "server.h"
    5. #include <QDebug>
    6. #include <iostream>
    7. #include <QQmlContext>
    8. #include "test.h"
    9. #include "test2.h"
    10. #include <QDeclarativeComponent>
    11. #include <qquickview.h>
    12.  
    13. Server::Server(QObject* parent): QObject(parent)
    14. {
    15. connect(&server, SIGNAL(newConnection()),
    16. this, SLOT(acceptConnection()));
    17.  
    18.  
    19. server.listen(QHostAddress::Any, 8888);
    20. qDebug() << "vasl shod";
    21. }
    22.  
    23. Server::~Server()
    24. {
    25. server.close();
    26. }
    27.  
    28. void Server::acceptConnection()
    29. {
    30. client = server.nextPendingConnection();
    31. client->write("Hello client\r\n");
    32.  
    33. connect(client, SIGNAL(readyRead()),
    34. this, SLOT(startRead()));
    35. }
    36.  
    37. void Server::startRead()
    38. {
    39.  
    40. QString data = QString(client->readAll());
    41.  
    42. client->waitForReadyRead(3000);
    43. QString buf= client->readAll();
    44. qDebug() << buf;
    45. QQmlApplicationEngine engine;
    46. engine.load(QUrl(QStringLiteral("qrc:///main.qml")));
    47.  
    48. QObject *rootObject = engine.rootObjects().first();
    49. QObject *qmlObject = rootObject->findChild<QObject*>("ttt");
    50. qmlObject ->setProperty("text","new data");
    51.  
    52. }
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. //main.qml
    2. import QtQuick 2.0
    3. import QtQuick 2.2
    4. import QtQuick.Window 2.1
    5. import QtQuick.Window 2.2
    6. import QtQuick.Controls 1.2
    7.  
    8. Window{
    9. width:400
    10. height:400
    11. visible:true
    12.  
    13. Item {
    14. objectname:"ttt"
    15. id: rootItem
    16. property string text: "hello world"
    17. Text {
    18. x:0
    19. y:0
    20. visible: true
    21. id: message
    22. text: rootItem.text
    23. }
    24. }
    25.  
    26. }
    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.
    Last edited by high_flyer; 15th November 2017 at 11:17.

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: how can i change textbox data remotely in qt?

    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?
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. Replies: 1
    Last Post: 21st March 2016, 22:49
  2. Replies: 3
    Last Post: 15th April 2013, 18:42
  3. textbox+highlighter
    By nima in forum Qt Programming
    Replies: 0
    Last Post: 27th June 2011, 15:39
  4. Textbox
    By Devoraz in forum Qt Programming
    Replies: 6
    Last Post: 23rd July 2009, 06:37
  5. connecting MySQL remotely
    By rickym in forum Qt Programming
    Replies: 5
    Last Post: 25th March 2007, 09:55

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.