【发布时间】:2017-06-03 16:36:45
【问题描述】:
main.cpp中的代码
Client c;
c.start("ip-address", port, "final folder/file");
它工作正常,客户端下载我需要的文件。
但是,如果我在 mainwindow.cpp 按钮单击操作中放置相同的代码
void MainWindow::on_btn_connect_clicked()
{
Client c;
c.start("ip-address", port, "final folder/file");
}
它不起作用。这是为什么?我是 Qt 和网络编码的新手。一些源代码
void Client::start(QString address, qint16 port, QString file)
{
qDebug() << "client started";
QHostAddress addr(address);
filename = file;
client->connectToHost(addr, port);
qDebug() << client->socketDescriptor();
}
我有 readyRead() 但程序没有到达那里。
Client::Client(QObject *parent) :
QObject(parent)
{
client = new QTcpSocket(this);
client->abort();
connect(client, SIGNAL(readyRead()), this, SLOT(ReadData()));
connect(client, SIGNAL(disconnected()), this, SLOT(Completed()));
}
【问题讨论】:
-
显示
mainwindow.cpp中的代码 -on_button_connect_clicked()必须是一个插槽,并且必须连接到QPushButton::clicked(bool)信号。看到你已经让on_button_connect_clicked()没有参数,你可能没有将它连接到信号,或者没有建立连接,因为签名不匹配。