2
#include <qftp.h>
#include <qapplication.h>
#include "test.h"
#include "ui_test.h"
#include <qfile.h>

int main()
{
    QApplication a(argc, argv);
    Test w;
    ftp.connectToHost("192.168.26.129", 21);
    w.show();
    return a.exec();
}

I am new to Qt, ftp.connectToHost("192.168.26.129", 21) works well from within main, but whenever called from class Test, it fails (with same header inclusion ) as below.

Test::Test(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::Test)
{   
    ui->setupUi(this);
    QFtp ftp;

    int x = ftp.connectToHost("192.168.26.129", 21);
    ftp.login("User1", "passwd");
}

Any suggestions would be helpful. Thanks!

László Papp
  • 51,870
  • 39
  • 111
  • 135
user3110438
  • 95
  • 1
  • 7

2 Answers2

3

When your Test class constructor goes out of scope, your ftp object gets destroyed. QFtp::connectToHost function does not block and returns immediately. Same goes for QFtp::login.

To solve this problem you can allocate your QFtp object using new:

QFtp *ftp = new QFtp(this);
thuga
  • 12,601
  • 42
  • 52
  • @user3110438 Note that if you want to use `ftp` object in other functions of your `Test` class, you should make it a member variable of your `Test` class like Laszlo Papp stated. – thuga Dec 19 '13 at 08:50
0

I would suggest to use a class member since it is an async operation. You are having an object on the stack which will be unwound when the function, and for that matter the life cycle of the object, ends.

You have three alternatives to handle this:

Class member

Test::Test(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::Test)
{   
    ui->setupUi(this);

    int x = m_ftp.connectToHost("192.168.26.129", 21);
    m_ftp.login("User1", "passwd");
}

This version is useful when you need the QFtp object in other method as well.

Smart pointer

Test::Test(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::Test)
{   
    ui->setupUi(this);
    QPointer m_ftp = new QFtp());

    int x = ftp->connectToHost("192.168.26.129", 21);
    ftp->login("User1", "passwd");
}

This version is good when you only need the QFtp object in this method, and the Test class is not a QObject.

QObject parent-child relation

Test::Test(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::Test)
{   
    ui->setupUi(this);
    QFtp ftp = new Ftp(this);    

    int x = ftp->connectToHost("192.168.26.129", 21);
    ftp->login("User1", "passwd");
}

This will not work if the "Test" class becomes a non-QObject, but it is good enough when Test inherits that, and you only need the QFtp object in this method.

László Papp
  • 51,870
  • 39
  • 111
  • 135
  • @Laslzo the above code works fine but theres a problem that ftp goes to connect to host only after a.exec(); in the main code is executed. Now the problem is that I first generate a app window which is displayed after application is executed(a.exec), and from the Main Window generated I have to perform the FTP operation but it fails there, I applied your suggestions but still fails from my Window.......My problem could be silly...being naive in Qt Iam sorry for that..Any suggestions might be helpful – user3110438 Dec 19 '13 at 11:52