0

In my Qt application, I have a MainWindow and a DialogWindow. The DialogWindow is for setting up server's IP address and port. While MainWindow is for performing communication after successful connection.

However, setting up QTcpSocket *socket in DialogWindow means that when I close DialogWindow, the socket will be destroyed, thus the socket will be disconnected with the server.

I would like to keep the socket connected to the server after the DialogWindow was closed. Are there any methods that can achieve this result?

Please give me some comments and ideas on this. I am quite a newbie to Qt.

DialogSetup.cpp

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

    socket = new QTcpSocket(this);
    connect(socket, SIGNAL(connected()),this, SLOT(wasconnected()));
    connect(socket,SIGNAL(disconnected()),this,SLOT(wasdisconnected()));
}
DialogSetup::~DialogSetup()
{
    delete ui;
}

void DialogSetup::on_pushButtonConnect_clicked()
{
    m_strIPAdd = ui->lineEditIPAdd->text();
    m_strPort = ui->lineEditPort->text().toInt();

    socket->connectToHost(m_strIPAdd,m_strPort);

    if(!socket->waitForConnected(10000))
    {
       ui->labelStatus->setText("Error");
       QMessageBox::information(this,"Error",socket->errorString());
    }
}
void DialogSetup::wasconnected()
{
    socket->write("Hello server!");
    ui->labelStatus->setText("Connected!");
    ui->pushButtonDisconnect->setDisabled(false);
}

void DialogSetup::wasdisconnected()
{
    ui->labelStatus->setText("Disonnected!");
}

void DialogSetup::on_pushButtonDisconnect_clicked()
{
    socket->close();
}

Mainwindow.cpp

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

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

void MainWindow::on_actionSetup_triggered()
{
    dialogsetup = new DialogSetup(this);
    dialogsetup->show();
}
Ryan
  • 279
  • 1
  • 4
  • 12
  • You can place socket pointer in MainWindow class and communicate with socket's signals throuhg DialogSetup form. – t3ft3l--i Jun 30 '15 at 09:23

2 Answers2

2

Move the socket outside the dialog class. Something like that:

void MainWindow::on_actionSetup_triggered()
{
    socket = new QTcpSocket(this); //declared as private in MainWindow
    dialogsetup = new DialogSetup(socket, this);
    connect(socket,SIGNAL(connected()), dialogsetup, SLOT(wasconnected()));
    connect(socket,SIGNAL(disconnected()), dialogsetup ,SLOT(wasdisconnected()));
    dialogsetup->show();
}

The dialog class stores a pointer to the socket and operates with it:

DialogSetup::DialogSetup(QTcpSocket *socket, QWidget *parent) :
    QDialog(parent),
    socket_(socket),
    ui(new Ui::DialogSetup)
{
    ui->setupUi(this);
}

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

void DialogSetup::on_pushButtonConnect_clicked()
{
    m_strIPAdd = ui->lineEditIPAdd->text();
    m_strPort = ui->lineEditPort->text().toInt();

    socket_->connectToHost(m_strIPAdd,m_strPort);

    if(!socket_->waitForConnected(10000))
    {
       ui->labelStatus->setText("Error");
       QMessageBox::information(this,"Error",socket->errorString());
    }
}
void DialogSetup::wasconnected()
{
    socket_->write("Hello server!");
    ui->labelStatus->setText("Connected!");
    ui->pushButtonDisconnect->setDisabled(false);
}

void DialogSetup::wasdisconnected()
{
    ui->labelStatus->setText("Disonnected!");
}

void DialogSetup::on_pushButtonDisconnect_clicked()
{
    socket_->close();
}
Sommerwild
  • 506
  • 1
  • 6
  • 18
  • Need store one more pointer to this socket in DialogSetup to provide opportunity clicked on button at DialogSetup – t3ft3l--i Jun 30 '15 at 09:31
  • Thanks for you guys help! @t3ft3l--i , do you mean there will be 2 pointers, 1 for the Dialog Setup and 1 for the mainwindow? And may I know what is opportunity clicked on button? – Ryan Jul 01 '15 at 01:55
  • @Ryan presence`void DialogSetup::on_pushButtonConnect_clicked()` so i see you have `QPushButton` and some actions with `socket_` at this slot. So need have access to `socket_` at `DiaglogSetup`. Thats why i said about storing one more pointer to socket: 1 for the `Dialog Setup` and 1 for the `MainWindow`. Or make more complex slot-signal communication between this classes. – t3ft3l--i Jul 01 '15 at 05:31
  • @t3ft3l--i @Cits I have trouble understanding pointers that used between class. So is `socket_` a pointer declared in DialogSetup. And I got this `error: C2065: 'socket_' : undeclared identifier`, does it mean I need to declare the `socket_` pointer in DialogSetup? Also, does this `socket_(socket) `means the class DialogSetup has a socket pointer? Thanks much~~! – Ryan Jul 01 '15 at 07:47
  • 1
    @Ryan yes, you need a declare at `DialogSetup.h` file `QTcpSocket *socket_` – t3ft3l--i Jul 01 '15 at 07:51
  • Yeah, its like t3ft3l said – Sommerwild Jul 01 '15 at 11:23
0

Change it's parent, or remove parent at all (though, you'll have to delete socket manually).

But, a better solution would be - not to create socket in dialog, but to pass it there:

DialogSetup::DialogSetup(QTcpSocket *socket_, QWidget *parent) :
    QDialog(parent),
    ui(new Ui::DialogSetup),
    socket(socket_)

...

void MainWindow::on_actionSetup_triggered()
{
    dialogsetup = new DialogSetup(socket, this);
    dialogsetup->show();
}
Amartel
  • 4,248
  • 2
  • 15
  • 21
  • You can't defined Constructor with parameters `DialogSetup(QObject *parent = 0, QTcpSocket *socket)`, change parameters orders. – t3ft3l--i Jun 30 '15 at 09:26
  • @t3ft3l--i forgot about header - my bad. Thanks for correcting. – Amartel Jun 30 '15 at 09:30
  • Thanks for your help!! However, I have this error `error: C2511: 'DialogSetup::DialogSetup(QTcpSocket *,QWidget *)' : overloaded member function not found in 'DialogSetup' ` . , does this means I need to add something to the declaration? I have tried public QTcpSocket `class DialogSetup : public QDialog, public QTcpSocket { Q_OBJECT`, but also got errors. – Ryan Jun 30 '15 at 09:45
  • @Ryan in `DialogSetup.h` you need to change `DialogSetup` constructor's definition to `explicit DialogSetup(QTcpSocket *socket, QWidget *parent = 0);` – Amartel Jun 30 '15 at 10:04