Reputation: 279
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();
}
Upvotes: 0
Views: 624
Reputation: 548
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();
}
Upvotes: 2
Reputation: 4286
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();
}
Upvotes: 0