Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Issue with TCPSocket in QWidgets [Solved]
Qt 6.11 is out! See what's new in the release blog

Issue with TCPSocket in QWidgets [Solved]

Scheduled Pinned Locked Moved General and Desktop
11 Posts 2 Posters 2.1k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    KGWAY
    wrote on last edited by KGWAY
    #1

    New to Qt.

    trying to update a gui text browser with data. It works, however the application is very slow upon launching. when enable my qdebug statements I see it's going through the code without launching the application till the end. The issue is I need the textbrowser to be RealTime. This is a very simple application right now that I tweaked from someone else's code. I want to better understand tcp on the application level until I progress into deeper territory. Here is my .ccp code:

    `
    #include "client.h"
    #include "ui_client.h"

    Client::Client(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Client)
    {
    ui->setupUi(this);
    Connect();

    }

    Client::~Client()
    {
    delete ui;
    }

    void Client::Connect()
    {
    //connected
    QString str;
    QString datas;
    QByteArray data;

    socket = new QTcpSocket(this);
    
    socket->connectToHost("192.168.1.2",2000);
    
    if(socket->waitForConnected(100))
    {
        //qDebug()<<"Connected!";
    
        socket->write("GetFWD\r");
        socket->waitForBytesWritten(100);
        socket->waitForReadyRead(50);
        //qDebug()<< "Reading:"<<socket->bytesAvailable();
    
        data = socket->readAll();
        datas = data;
        ui->textBrowser->append(datas);
        socket->close();
    
    
    }
    else
    {
        //qDebug()<<"Not Connected";
        str = "Not Connected";
        ui->textBrowser->setText(str);
    }
    

    }
    `

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi and welcome to devnet,

      Can you define "very slow" ?

      What's your OS + Qt combo ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • K Offline
        K Offline
        KGWAY
        wrote on last edited by
        #3

        Sorry for the slow response (I wear many hats at work so this is 1 of many projects I deal with). To answer your question, it can take as long as 20 seconds to build (I don't think it's my compiler since this is the only application that takes this long. Another interesting tidbit is when the ethernet is not connected it launches right away. So I know it's having a conflict with the hardware I"m talking to. I"m not sure why though since it does work correctly. To be honest I wouldn't care that much if the application took a little bit to launch (I understand TCP has overhead and based one your ethernet module could be slower), I just need the text box to update in real time and putting it in a giant while loop won't work...I tried. Oh the OS I'm using is Windows 7 pro w/ service pack 1 64 bit and Qt Creator 3.4.2 enterprise.

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Something's not clear, you were talking about start time and now build time.

          Wouldn't be by any chance 30 seconds rather that 20 ?

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • K Offline
            K Offline
            KGWAY
            wrote on last edited by
            #5

            I guess it's sorta both in a sense. This is what happens. I press the debug and launch button. Qt begins building then gets halfway before the bar stops moving. Qt creator (the actual program) says "Not responding" in the upper status bar; 20 or more seconds go by then the program that I created launches.

            1 Reply Last reply
            0
            • K Offline
              K Offline
              KGWAY
              wrote on last edited by
              #6

              I think the reason it takes longer is because it's actually running my code before launching any of the gui. I built the bases of this gui using Qt designer so I didn't actually declare my text windows(not that that should matter). but the last thing it does is actually launch my gui, which is already populated with information. If I use an infinite while loop for my data acquisition the program takes longer to actually build and never launches since the new data is still coming in.

              1 Reply Last reply
              0
              • K Offline
                K Offline
                KGWAY
                wrote on last edited by
                #7

                I've been reading up on this issue and from what I can gather because Qt's signal and slots are done in sequential order same with applying text to text fields the only solution I could gather would be to use threads. Does that sound correct? if so my knowledge of threads is sorta lacking is there like some end all tutorial that will launch me in the proper direction on how to implement them. Thanks!

                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  Rather rewrite the Connect part to truly use signals and slots.

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  K 1 Reply Last reply
                  0
                  • SGaistS SGaist

                    Rather rewrite the Connect part to truly use signals and slots.

                    K Offline
                    K Offline
                    KGWAY
                    wrote on last edited by
                    #9

                    @SGaist Ok I'll try to do that. Signal and slots is still kinda new to me but I know it's very necessary for me to master Qt; So I'll report back with any issues or outcomes. Thanks again for the help!

                    1 Reply Last reply
                    0
                    • K Offline
                      K Offline
                      KGWAY
                      wrote on last edited by
                      #10

                      I was able to get my application to work using signals and slots. I used a timer to actually read the port when ready then print to a text box. Thanks for the help!

                      1 Reply Last reply
                      0
                      • SGaistS Offline
                        SGaistS Offline
                        SGaist
                        Lifetime Qt Champion
                        wrote on last edited by
                        #11

                        Great !

                        Since you have it working now, please update the thread title prepending [solved] so other forum users may know a solution has been found :)

                        Also, while browsing the forum, if you find helpful posts please consider up-voting them so they may be more easily found by other users :)

                        Interested in AI ? www.idiap.ch
                        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                        1 Reply Last reply
                        0

                        • Login

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • Users
                        • Groups
                        • Search
                        • Get Qt Extensions
                        • Unsolved