How to Login to my FTP Account
-
Hi;
i want to design a GUI to access my FTP account;
for first step i need a GUI with only one Login button;
i know how to create the gui and assign actions to buttons but my problem is that i dont know how to use QFtp class.
can any one give me a very simple sample code?(or any help?)
(Qt 4.8.6) -
Hi and welcome to devnet,
What about the example provided with the module ?
-
Where you will use an object that class.
-
i want to have a "Login" button in my GUI and login to my account when i press that.
so i have to include QFtp class in "mainwindow.cpp" .Now i want to create new QFtp object with this command:
QFtp *ftp=new Qftp();
but i receive this error:expected type-specifier before 'Qftp'
QFtp *ftp=new Qftp(); -
i want to have a "Login" button in my GUI and login to my account when i press that.
so i have to include QFtp class in "mainwindow.cpp" .Now i want to create new QFtp object with this command:
QFtp *ftp=new Qftp();
but i receive this error:expected type-specifier before 'Qftp'
QFtp *ftp=new Qftp(); -
my mainwindow.cpp contents:
#include "ui_mainwindow.h" #include <QtNetwork/QFtp> QFtp *ftp=new QFtp(); MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); } MainWindow::~MainWindow() { delete ui; } void MainWindow::on_pushButton_clicked() { }
and my 3 Errors:
In function `_static_initialization_and_destruction_0':undefined reference to `_imp___ZN4QFtpC1EP7QObject'
error: ld returned 1 exit status
-
my mainwindow.cpp contents:
#include "ui_mainwindow.h" #include <QtNetwork/QFtp> QFtp *ftp=new QFtp(); MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); } MainWindow::~MainWindow() { delete ui; } void MainWindow::on_pushButton_clicked() { }
and my 3 Errors:
In function `_static_initialization_and_destruction_0':undefined reference to `_imp___ZN4QFtpC1EP7QObject'
error: ld returned 1 exit status
@rezaMSLM said in How to Login to my FTP Account:
QFtp *ftp=new QFtp();
This is bad!
Don't do this with Qt classes.
ftp should be class member in your MainWindow class. -
@rezaMSLM said in How to Login to my FTP Account:
QFtp *ftp=new QFtp();
This is bad!
Don't do this with Qt classes.
ftp should be class member in your MainWindow class.@jsulm said in How to Login to my FTP Account:
@rezaMSLM said in How to Login to my FTP Account:
QFtp *ftp=new QFtp();
This is bad!
Don't do this with Qt classes.
ftp should be class member in your MainWindow class.would you please correct my above Code?
-
@jsulm said in How to Login to my FTP Account:
@rezaMSLM said in How to Login to my FTP Account:
QFtp *ftp=new QFtp();
This is bad!
Don't do this with Qt classes.
ftp should be class member in your MainWindow class.would you please correct my above Code?
// In your header file class MainWindow... private: QFtp ftp; } // cpp file MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); } MainWindow::~MainWindow() { delete ui; } void MainWindow::on_pushButton_clicked() { }
-
my header file:
#ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> namespace Ui { class MainWindow; } class MainWindow : public QMainWindow { Q_OBJECT /*added Code:*/ private: QFtp ftp; /***********/ public: explicit MainWindow(QWidget *parent = 0); ~MainWindow(); private slots: void on_pushButton_clicked(); private: Ui::MainWindow *ui; }; #endif // MAINWINDOW_H
my Errors:
'QFtp' does not name a type
QFtp ftp;
^ -
my header file:
#ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> namespace Ui { class MainWindow; } class MainWindow : public QMainWindow { Q_OBJECT /*added Code:*/ private: QFtp ftp; /***********/ public: explicit MainWindow(QWidget *parent = 0); ~MainWindow(); private slots: void on_pushButton_clicked(); private: Ui::MainWindow *ui; }; #endif // MAINWINDOW_H
my Errors:
'QFtp' does not name a type
QFtp ftp;
^ -
@jsulm said in How to Login to my FTP Account:
@rezaMSLM Didn't you forget to include the header file?
No,
my mainwindow.h file content:#ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> namespace Ui { class MainWindow; } class MainWindow : public QMainWindow { Q_OBJECT /*added Code:*/ private: QFtp ftp; /***********/ public: explicit MainWindow(QWidget *parent = 0); ~MainWindow(); private slots: void on_pushButton_clicked(); private: Ui::MainWindow *ui; }; #endif // MAINWINDOW_H
main.cpp:
#include "mainwindow.h" #include <QApplication> int main(int argc, char *argv[]) { QApplication a(argc, argv); MainWindow w; w.show(); return a.exec(); }
mainwindow.cpp:
#include "mainwindow.h" #include "ui_mainwindow.h" #include <QtNetwork/QFtp> //QFtp *ftp=new QFtp(); MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); } MainWindow::~MainWindow() { delete ui; } void MainWindow::on_pushButton_clicked() { }
.pro contents:
#------------------------------------------------- # # Project created by QtCreator 2018-02-28T21:06:27 # #------------------------------------------------- QT += core gui greaterThan(QT_MAJOR_VERSION, 4): QT += widgets TARGET = ftp2 TEMPLATE = app # The following define makes your compiler emit warnings if you use # any feature of Qt which has been marked as deprecated (the exact warnings # depend on your compiler). Please consult the documentation of the # deprecated API in order to know how to port your code away from it. DEFINES += QT_DEPRECATED_WARNINGS # You can also make your code fail to compile if you use deprecated APIs. # In order to do so, uncomment the following line. # You can also select to disable deprecated APIs only up to a certain version of Qt. #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 SOURCES += \ main.cpp \ mainwindow.cpp HEADERS += \ mainwindow.h FORMS += \ mainwindow.ui
-
@jsulm said in How to Login to my FTP Account:
@rezaMSLM Didn't you forget to include the header file?
No,
my mainwindow.h file content:#ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> namespace Ui { class MainWindow; } class MainWindow : public QMainWindow { Q_OBJECT /*added Code:*/ private: QFtp ftp; /***********/ public: explicit MainWindow(QWidget *parent = 0); ~MainWindow(); private slots: void on_pushButton_clicked(); private: Ui::MainWindow *ui; }; #endif // MAINWINDOW_H
main.cpp:
#include "mainwindow.h" #include <QApplication> int main(int argc, char *argv[]) { QApplication a(argc, argv); MainWindow w; w.show(); return a.exec(); }
mainwindow.cpp:
#include "mainwindow.h" #include "ui_mainwindow.h" #include <QtNetwork/QFtp> //QFtp *ftp=new QFtp(); MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); } MainWindow::~MainWindow() { delete ui; } void MainWindow::on_pushButton_clicked() { }
.pro contents:
#------------------------------------------------- # # Project created by QtCreator 2018-02-28T21:06:27 # #------------------------------------------------- QT += core gui greaterThan(QT_MAJOR_VERSION, 4): QT += widgets TARGET = ftp2 TEMPLATE = app # The following define makes your compiler emit warnings if you use # any feature of Qt which has been marked as deprecated (the exact warnings # depend on your compiler). Please consult the documentation of the # deprecated API in order to know how to port your code away from it. DEFINES += QT_DEPRECATED_WARNINGS # You can also make your code fail to compile if you use deprecated APIs. # In order to do so, uncomment the following line. # You can also select to disable deprecated APIs only up to a certain version of Qt. #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 SOURCES += \ main.cpp \ mainwindow.cpp HEADERS += \ mainwindow.h FORMS += \ mainwindow.ui
-
@rezaMSLM You did forget.
You're using QFtp class now in your header file, so you need to move#include <QtNetwork/QFtp>
to the header file, else compiler has no idea what QFtp is.
@jsulm said in How to Login to my FTP Account:
@rezaMSLM You did forget.
You're using QFtp class now in your header file, so you need to move#include <QtNetwork/QFtp>
to the header file, else compiler has no idea what QFtp is.
now i have moved it to mainwindow.h
doesn't solved and here are the errors:
-
@jsulm said in How to Login to my FTP Account:
@rezaMSLM You did forget.
You're using QFtp class now in your header file, so you need to move#include <QtNetwork/QFtp>
to the header file, else compiler has no idea what QFtp is.
now i have moved it to mainwindow.h
doesn't solved and here are the errors:
@rezaMSLM Do you have this in your pro file:
QT += ftp
You do not link your app against QFtp currently.
Here is an example: https://github.com/qt/qtftp/tree/master/examples/qftp -
@rezaMSLM Do you have this in your pro file:
QT += ftp
You do not link your app against QFtp currently.
Here is an example: https://github.com/qt/qtftp/tree/master/examples/qftp@jsulm said in How to Login to my FTP Account:
@rezaMSLM Do you have this in your pro file:
QT += ftp
You do not link your app against QFtp currently.
Here is an example: https://github.com/qt/qtftp/tree/master/examples/qftpThank you jsulm;
QT+=ftp didnt work
but QT+=network worked!
Now i receive No Error -
I built an object in mainwindow.h file using this command:
class MainWindow... private: QFtp *ftp; }
and call these commands when clicking on "Login" Button:
void MainWindow::on_pushButton_clicked() { ftp->connectToHost("my Host IP"); ftp->login("my user name" , "my password"); }
and when I Run the project and click on Login button it crashes.
but if i change the code like this it won't crash:
class MainWindow... private: QFtp ftp; }
void MainWindow::on_pushButton_clicked() { ftp.connectToHost("my Host IP"); ftp.login("my user name" , "my password"); }
why?
-
I built an object in mainwindow.h file using this command:
class MainWindow... private: QFtp *ftp; }
and call these commands when clicking on "Login" Button:
void MainWindow::on_pushButton_clicked() { ftp->connectToHost("my Host IP"); ftp->login("my user name" , "my password"); }
and when I Run the project and click on Login button it crashes.
but if i change the code like this it won't crash:
class MainWindow... private: QFtp ftp; }
void MainWindow::on_pushButton_clicked() { ftp.connectToHost("my Host IP"); ftp.login("my user name" , "my password"); }
why?
@rezaMSLM Because if you use a pointer you need to create an instance and assign the pointer, else you're using a dangling pointer (a pointer not pointing to a valid chunk of memory). You should read about memory management in C/C++.
MainWindow::MainWindow() { ftp = new QFtp(); }
Why do you want to use a pointer?
-
@rezaMSLM Because if you use a pointer you need to create an instance and assign the pointer, else you're using a dangling pointer (a pointer not pointing to a valid chunk of memory). You should read about memory management in C/C++.
MainWindow::MainWindow() { ftp = new QFtp(); }
Why do you want to use a pointer?