[Solved] question about signals and slots
-
hello
I started today a new Qt application and i wanted to use QFtp.
I'm stuck when I want to link QFtp signals to my own slots, sources compile but no signals are catch.
I read the signals and slots doc and use the ftp client example to start.here is my code, i make it as simple as possible:
the my_ftp.h:
@
#include <QtNetwork/QFtp>
#include <QObject>
#include <iostream>class my_ftp : public QObject
{
Q_OBJECT
public:my_ftp() {} ~my_ftp() {} void run();
public slots:
void cmd_end(int i, bool j); void cmd_start(int i);
public:
QFtp *ff;
};
@the main:
@
void my_ftp::cmd_end(int i, bool j)
{
std::cout << i << ": " << j << std::endl;}
void my_ftp::cmd_start(int i)
{
std::cout << "start" << i << std::endl;
}void my_ftp::run()
{
std::cout << "running" << std::endl;
this->ff = new QFtp(this);this->connect(this->ff, SIGNAL(commandFinished(int,bool)), this, SLOT(cmd_end(int,bool))); this->connect(this->ff, SIGNAL(commandStarted(int)), this, SLOT(cmd_start(int)));
}
int main()
{
my_ftp *f = new my_ftp();f->run(); f->ff->connectToHost("***", 21); while (true) { }
}
@nothing never happened after the "running" i don't know why.
-
You need to at least start an event loop (using Q(Core)Application) for this to work properly:
@
int main(int argc, char **argv)
{
QApplication app(argc, argv);
my_ftp *f = new my_ftp();f->run(); f->ff->connectToHost("***", 21); return app.exec();
}
@Don't forget to connect a finished signal to the QApplication's quit slot.
-
I don't know what you are doing but i think your application is blocking!
Maybe you can try add
@
while(true){QCoreApplication::instance()->processEvents(QEventLoop::ExcludeUserInputEvents);}
@or use
@
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
my_ftp *f = new my_ftp();f->run(); f->ff->connectToHost("***", 21); return a.exec();
}
@This is idea i don't know if this can work correctly...
-
this is working, thanks you ! I was thinking the QCoreApplication was only for graphical application ^^
i just add this to my main :
@
int main(int ac, char **av)
{
QCoreApplication my_app(ac, av);
my_ftp *f = new my_ftp();f->run(); f->ff->connectToHost("***", 21); my_app.exec();
}
@ -
[quote author="Franzk" date="1298572684"]QApplication is required for graphical applications. QCoreApplication for all Qt applications.[/quote]
This is just false. The point however is that QFtp is event (signal) driven, therefore it requires an event loop.