Issue with TCPSocket in QWidgets [Solved]



  • 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);
    }
    

    }
    `


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    Can you define "very slow" ?

    What's your OS + Qt combo ?



  • 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.


  • Lifetime Qt Champion

    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 ?



  • 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.



  • 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.



  • 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!


  • Lifetime Qt Champion

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



  • @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!



  • 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!


  • Lifetime Qt Champion

    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 :)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.