关闭DialogWindow后如何让QTcpsocket保持连接?

时间:2015-06-30 09:15:29

标签: c++ qt sockets qtcpsocket

在我的Qt应用程序中,我有一个MainWindow和一个DialogWindowDialogWindow用于设置服务器的IP地址和端口。虽然MainWindow用于在成功连接后执行通信。

但是,在QTcpSocket *socket中设置DialogWindow表示当我关闭DialogWindow时,socket将被销毁,因此socket将与服务器。

我希望在DialogWindow关闭后将套接字连接到服务器。有没有什么方法可以达到这个效果?

请就此提出一些意见和建议。我是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();
}

2 个答案:

答案 0 :(得分:2)

将套接字移到对话框类之外。这样的事情:

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

对话框类存储指向套接字的指针并使用它进行操作:

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

答案 1 :(得分:0)

更改它的父级,或者完全删除父级(但是,你必须手动删除套接字)。

但是,更好的解决方案是 - 不要在对话框中创建套接字,而是将其传递到那里:

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