PDA

View Full Version : why the qftp failed in qmainwindow?where is its signal?



cxl2253
9th April 2007, 04:33
I use qftp to upload a test file in qmainwindow,but failed,Even i cannot pick up the signal,I think the code is right,why?


////////////////cfrmmain.h
class frmMainWindow:public QMainWindow,public Ui::MainWindow
{
Q_OBJECT
public:
frmMainWindow();
private:

QFtp *ftp; //连接FTP
QFile *file;
QStringList fileList;

private slots:
void btnStartClicked();
void ftpListInfo(const QUrlInfo &urlInfo);
void ftpCommandFinished(int commandId, bool error);
void updateDataTransferProgress(qint64 readBytes,qint64 totalBytes);
};
/////////////////cfrmmain.cpp/////////
frmMainWindow::frmMainWindow()
{
setupUi(this);
connect(startSCP,SIGNAL(clicked()),this ,SLOT(btnStartClicked()));
ftp=0;
}
void frmMainWindow::btnStartClicked()
{
if (ftp) {
ftp->abort();
ftp->deleteLater();
ftp = 0;
return;
}

ftp = new QFtp;
connect(ftp, SIGNAL(commandFinished(int, bool)),
this, SLOT(ftpCommandFinished(int, bool)));
connect(ftp, SIGNAL(listInfo(const QUrlInfo &)),
this, SLOT(ftpListInfo(const QUrlInfo &)));
connect(ftp, SIGNAL(dataTransferProgress(qint64, qint64)),
this, SLOT(updateDataTransferProgress(qint64, qint64)));

ftp->connectToHost("ftp.trolltech.com");
ftp->login();
ftp->list();

}


/////////////////all the SLOT function cannot performed ,why????????
void frmMainWindow::ftpCommandFinished(int, bool error)
{

if (ftp->currentCommand() == QFtp::ConnectToHost) {
if (error) {
QMessageBox::information(0, tr("FTP"),
tr("Unable to connect to the FTP server "
"at %1. Please check that the host "
"name is correct.")
.arg("192.168.1.3"));
return;
}
return;
}

if (ftp->currentCommand() == QFtp::Get) {


}
if (ftp->currentCommand() == QFtp::List) {
QMessageBox::information(0, tr("FTP"),"LIST");

}
if (ftp->currentCommand() == QFtp::Put) {

}
}

void frmMainWindow::updateDataTransferProgress(qint64 readBytes,
qint64 totalBytes)
{


void frmMainWindow::ftpListInfo(const QUrlInfo &urlInfo)
{
fileList<<urlInfo.name() ;
}

marcel
9th April 2007, 05:31
Because your slots are all private.
You use private slots only if signals from the same class are connected to them. Otherwise use protected or public slots.

cxl2253
9th April 2007, 06:09
no,it's maybe not right.In the demo of QT examples\FTP, its slot is private and it works well. I rewrite the code and moved the private slot to public slot. it's just the same ,not work.:confused:

marcel
9th April 2007, 06:17
Does it execute the btnStartClicked slot when you click the button?
Also, check the console, often Qt leaves some messages when something goes wrong.

If btnStartClicked is not called, then you ftp object is not created.

cxl2253
9th April 2007, 06:26
yes,it executed.But the console reports nothing!

marcel
9th April 2007, 06:42
Well, the only difference between your code and the ftp example is that the ftp object is created with a parent (this ). But I do not believe this is the reason.= your slots don't get called.

Can you post your moc_cfrmain.cpp? We can see there if the signals are mapped correctly.

jacek
9th April 2007, 13:36
You use private slots only if signals from the same class are connected to them.
There's nothing wrong in connecting non-local signals to private slots.


yes,it executed.But the console reports nothing!
Make sure your application is compiled in debug mode (on windows you will also have to add CONFIG += console to your .pro file).

Also check whether all connect() statements return true.

cxl2253
16th April 2007, 15:45
yes,my compiler flag is MDD. But i donot understand what's wrong with the code.I even want to give up qt, why there are so many problem?:D

marcel
16th April 2007, 17:19
I just tested your code and it works.

The header:


#ifndef FTP3_H
#define FTP3_H

#include <QtGui/QMainWindow>
#include "ui_ftp3.h"
#include <QUrlInfo>

class QFtp;
class QFile;

class ftp3 : public QMainWindow
{
Q_OBJECT

public:
ftp3(QWidget *parent = 0, Qt::WFlags flags = 0);
~ftp3();

private:
Ui::ftp3Class ui;
QFtp *ftp;
QFile *file;
QStringList fileList;
private slots:
void ftpListInfo(const QUrlInfo &urlInfo);
void ftpCommandFinished(int commandId, bool error);
void updateDataTransferProgress(qint64 readBytes,qint64 totalBytes);
};

#endif // FTP3_H


The cpp:


#include "ftp3.h"
#include <QFtp>
#include <QMessageBox>

ftp3::ftp3(QWidget *parent, Qt::WFlags flags)
: QMainWindow(parent, flags)
{
ui.setupUi(this);
ftp = 0;
file = 0;

if (ftp)
{
ftp->abort();
ftp->deleteLater();
ftp = 0;
return;
}

ftp = new QFtp;
connect(ftp, SIGNAL(commandFinished(int, bool)),
this, SLOT(ftpCommandFinished(int, bool)));
connect(ftp, SIGNAL(listInfo(const QUrlInfo &)),
this, SLOT(ftpListInfo(const QUrlInfo &)));
connect(ftp, SIGNAL(dataTransferProgress(qint64, qint64)),
this, SLOT(updateDataTransferProgress(qint64, qint64)));

ftp->connectToHost("ftp.trolltech.com");
ftp->login();
ftp->list();
}

ftp3::~ftp3()
{
}

void ftp3::ftpCommandFinished(int, bool error)
{

if (ftp->currentCommand() == QFtp::ConnectToHost) {
if (error) {
QMessageBox::information(0, tr("FTP"),
tr("Unable to connect to the FTP server "
"at %1. Please check that the host "
"name is correct.")
.arg("192.168.1.3"));
return;
}
return;
}

if (ftp->currentCommand() == QFtp::Get)
{
}
if (ftp->currentCommand() == QFtp::List)
{
QMessageBox::information(0, tr("FTP"),"LIST");
}
if (ftp->currentCommand() == QFtp::Put)
{
}
}
void ftp3::updateDataTransferProgress(qint64 readBytes, qint64 totalBytes)
{
}


void ftp3::ftpListInfo(const QUrlInfo &urlInfo)
{
fileList<<urlInfo.name() ;
}


The only difference is that I didn't use the button. I create the ftp client in the constructor of the main window.

Could you test this? Do you get the same result?

regards

cxl2253
22nd April 2007, 08:37
there are still problem! I used qt422 open source and vc2003.I found when i compiled its example in ..\examples\network\ftp in vc enviroment, not used qmake, the exe cannot emit the signal and not even to connect to ftp server. but when used qmake ,all is right. why??

cxl2253
22nd April 2007, 08:42
Can anybody send me a project about qftp demo in vc ?thanks. my email is cxl2253@gmail.com

marcel
22nd April 2007, 08:51
Try this one... It's the example I attached. It worked with 4.2.2 and VS 2003.

Regards

marcel
22nd April 2007, 08:58
Hey, are you sure you don't have a firewall on or anything else that could block the ftp client? Or have you waited enough? Last time I tested the requestFinished response came a little late ( about 10 seconds )?

Regards

cxl2253
22nd April 2007, 12:27
thanks,I will test your provided demo. I used the exe compiled by vc and the other compiled by qmake. The first one cannot response, the other can .

cxl2253
22nd April 2007, 13:16
your code can work well, thanks , but i am surprised that why my code from the QT demo cannot emit the signal? can you help me ? All the code is from QT demo ,I only created a vc project files.
attached file :
1106

cxl2253
22nd April 2007, 13:51
marcel,I have made a lower mistake, I used the QtNetwork4.lib, not the QtNetworkd4.lib in debug mode .Thaks for your help again.