Results 1 to 4 of 4

Thread: Textarea is not Updating when set from C++

  1. #1
    Join Date
    Sep 2015
    Posts
    23
    Thanks
    1

    Default Textarea is not Updating when set from C++

    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:

    Qt Code:
    1. import QtQuick 2.2
    2. import QtQuick.Controls 1.1
    3.  
    4.  
    5. ApplicationWindow {
    6. visible: true
    7. width: 640
    8. height: 480
    9.  
    10. title: qsTr("IMServer")
    11.  
    12. menuBar: MenuBar {
    13. Menu {
    14. title: qsTr("File")
    15. MenuItem {
    16. text: qsTr("Exit")
    17. onTriggered: Qt.quit();
    18. }
    19. }
    20. }
    21.  
    22. TextArea {
    23. id: serverInformation
    24. x: 0
    25. y: 0
    26. width: 247
    27. height: 279
    28. }
    29. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #include <QApplication>
    2. #include <QQmlApplicationEngine>
    3. #include <QQmlContext>
    4. #include <QQmlEngine>
    5. #include <QtQml>
    6.  
    7. #include "imserver.h"
    8.  
    9. int main(int argc, char *argv[]) {
    10. QApplication app(argc, argv);
    11.  
    12. QQmlApplicationEngine engine;
    13. engine.load(QUrl(QStringLiteral("qrc:///main.qml")));
    14.  
    15. IMServer server(2000);
    16.  
    17. qmlRegisterUncreatableType<IMServer>("App", 1, 0, "IMServer", "");
    18. engine.rootContext()->setContextProperty("imserver", &server);
    19.  
    20. server.startServer();
    21.  
    22. return app.exec();
    23. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #ifndef IMSERVER_H
    2. #define IMSERVER_H
    3.  
    4. #include <QTcpServer>
    5. #include <QTcpSocket>
    6. #include <QAbstractSocket>
    7. #include <QThreadPool>
    8.  
    9. class IMServer : public QTcpServer {
    10.  
    11. Q_OBJECT
    12. Q_PROPERTY(QString text WRITE setText NOTIFY textChanged)
    13.  
    14. public:
    15. explicit IMServer(int port, QObject *parent = 0);
    16. void startServer();
    17. void setText(const QString &txt);
    18.  
    19. signals:
    20. void textChanged();
    21.  
    22. public slots:
    23.  
    24. protected:
    25. void incomingConnection(qintptr fd);
    26.  
    27. private:
    28. int port;
    29. QThreadPool *pool;
    30. QString m_text;
    31. };
    32.  
    33.  
    34. #endif // IMSERVER_H
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #include "imserver.h"
    2. #include "clienthandler.h"
    3.  
    4. IMServer::IMServer(int port, QObject *parent) : QTcpServer(parent) {
    5. this->pool = new QThreadPool(this);
    6. this->pool->setMaxThreadCount(100);
    7. this->port = port;
    8. }
    9.  
    10. void IMServer::startServer() {
    11.  
    12. setText("TEST");
    13.  
    14. if (!this->listen(QHostAddress::Any, this->port)) {
    15. qDebug() << "Server could not be started";
    16. } else {
    17. qDebug() << "Server started, listening...";
    18. }
    19. }
    20.  
    21. void IMServer::setText(const QString &txt) {
    22. m_text = txt;
    23. emit textChanged();
    24. }
    25.  
    26.  
    27. void IMServer::incomingConnection(qintptr fd) {
    28. ClientHandler *client = new ClientHandler();
    29. client->setAutoDelete(true);
    30. client->fd = fd;
    31. this->pool->start(client);
    32. }
    To copy to clipboard, switch view to plain text mode 

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

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Textarea is not Updating when set from C++

    Why should be it "updating"?
    There is no code that would interact in any way with the text area.

    Cheers,
    _

  3. #3
    Join Date
    Sep 2015
    Posts
    23
    Thanks
    1

    Default Re: Textarea is not Updating when set from C++

    So what would I have to add/change to make it work?

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Textarea is not Updating when set from C++

    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,
    _

Similar Threads

  1. QML TextArea zooming/scaling
    By mh8411 in forum Qt Quick
    Replies: 1
    Last Post: 27th December 2014, 12:17
  2. qml Textarea
    By codeman in forum Qt Quick
    Replies: 4
    Last Post: 13th January 2014, 18:51
  3. Updating widgets
    By The 11th plague of Egypt in forum Qt Programming
    Replies: 17
    Last Post: 28th September 2011, 18:41
  4. QTCreator GUI How to link button with textarea
    By chs in forum Qt Programming
    Replies: 8
    Last Post: 23rd May 2011, 07:30
  5. QtWebKit and clickEvent from textarea
    By keen in forum Qt Programming
    Replies: 0
    Last Post: 30th June 2010, 15:15

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.