Important: Please read the Qt Code of Conduct -

TCP Socket problem

  • Working on a program that gets information from a telnet connection to a web server. Works fine unless I lose connection or the web server was not ready.
    To solve this i created a button a dialog and this dialog calls at the parent function to connect. I tested this button with just a message on the connect function and works fine. When i put the code to connect i get a segmentation error at connectToHost() function of the socket.
    Here is the code on the main window:

    • bool PowerMonitor::connectToServer()
      data->setSystemConnected(true); // Need to add handling for fail connection
      ui->statusBar->showMessage("Connected to:"+data->getHostIp()+" Port:"+QString::number(data->getPort()));
      QMessageBox::warning(this,"Connection","Could not find Sensors");
      ui->statusBar->showMessage("No Connection");
      return true;

    This is the code from the Child Dialog:

    • void SystemDataDisplay::on_reconnect_clicked()
      PowerMonitor pmonitor = qobject_cast<PowerMonitor>(parent());

    My hunch is something to do with calling connectToHost() function for the second time. Any ideas?
    Thanks for your help

  • After running using the debugger, i understand what is going on.
    When i'm creating the QDialog object from the main program I was using the following :

    • SystemDataDisplay systemdatadisplay;

    Thus, i was not passing the address of the parent to the child. This was solved like this:
    SystemDataDisplay systemdatadisplay(this);

    No it works fine

  • Lifetime Qt Champion


    Glad you found out but that design is pretty bad. You are creating a tight coupling where none is needed.

    If you want that dialog to trigger something in your other widget, then just give that dialog a signal and connect it before calling exec.

Log in to reply