How to Login to my FTP Account
-
@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?
-
when I click on login button and connectToHost() Command is executed how I notice that it's connected?
I know that some signals are emitted but i dont know how to use themMainWindow::MainWindow() { connect(&ftp, SIGNAL(stateChanged(int)), this, SLOT(checkState(int))); } void MainWindow::checkState(int state) { switch(state) { case QFtp::Connected: break; ... } } // From QFtp header file: enum State { Unconnected, HostLookup, Connecting, Connected, LoggedIn, Closing };
-
MainWindow::MainWindow() { connect(&ftp, SIGNAL(stateChanged(int)), this, SLOT(checkState(int))); } void MainWindow::checkState(int state) { switch(state) { case QFtp::Connected: break; ... } } // From QFtp header file: enum State { Unconnected, HostLookup, Connecting, Connected, LoggedIn, Closing };
-
my program has a BUG:
when I'm already Connected to server and I Click on Connect button again none of the buttons will work.
neither login nor disconnect buttonsmy mainwindow.cpp:
#include "mainwindow.h" #include "ui_mainwindow.h" #include <QtNetwork/QFtp> MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); ftp = new QFtp(parent); connect(ftp, SIGNAL(stateChanged(int)), this, SLOT(checkState(int))); } MainWindow::~MainWindow() { delete ui; delete ftp; } void MainWindow::checkState(int state) { switch(state) { case QFtp::Connected: ui->Connect_Status_Label->setText("Connected"); break; case QFtp::Unconnected: ui->Connect_Status_Label->setText("UnConnected"); ui->Login_Status_Label->setText("Logged Out"); break; case QFtp:: LoggedIn: ui->Login_Status_Label->setText("Logged in"); break; } } void MainWindow::on_pushButton_Connect_clicked() { QString Host = ui->lineEdit_Host->text(); ftp->connectToHost(Host); } void MainWindow::on_pushButton_Login_clicked() { QString UserName = ui->lineEdit_User->text(); QString PassWord = ui->lineEdit_Pass->text(); ftp->login(UserName,PassWord); } void MainWindow::on_pushButton_DisConnect_clicked() { ftp->close(); }
what is the reason?
-
my program has a BUG:
when I'm already Connected to server and I Click on Connect button again none of the buttons will work.
neither login nor disconnect buttonsmy mainwindow.cpp:
#include "mainwindow.h" #include "ui_mainwindow.h" #include <QtNetwork/QFtp> MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); ftp = new QFtp(parent); connect(ftp, SIGNAL(stateChanged(int)), this, SLOT(checkState(int))); } MainWindow::~MainWindow() { delete ui; delete ftp; } void MainWindow::checkState(int state) { switch(state) { case QFtp::Connected: ui->Connect_Status_Label->setText("Connected"); break; case QFtp::Unconnected: ui->Connect_Status_Label->setText("UnConnected"); ui->Login_Status_Label->setText("Logged Out"); break; case QFtp:: LoggedIn: ui->Login_Status_Label->setText("Logged in"); break; } } void MainWindow::on_pushButton_Connect_clicked() { QString Host = ui->lineEdit_Host->text(); ftp->connectToHost(Host); } void MainWindow::on_pushButton_Login_clicked() { QString UserName = ui->lineEdit_User->text(); QString PassWord = ui->lineEdit_Pass->text(); ftp->login(UserName,PassWord); } void MainWindow::on_pushButton_DisConnect_clicked() { ftp->close(); }
what is the reason?
@rezaMSLM I don't understand: you're connected and then click connected again? Afterwards disconnect doesn't work? Does disconnect work if you don't click connect when already connected? You should disable the connect button if already connected and enable it when disconnected.
-
@rezaMSLM I don't understand: you're connected and then click connected again? Afterwards disconnect doesn't work? Does disconnect work if you don't click connect when already connected? You should disable the connect button if already connected and enable it when disconnected.
@jsulm said in How to Login to my FTP Account:
you're connected and then click connected again? Afterwards disconnect doesn't work?
yes
@jsulm said in How to Login to my FTP Account:
Does disconnect work if you don't click connect when already connected?
yes
@jsulm said in How to Login to my FTP Account:
You should disable the connect button if already connected and enable it when disconnected.
OK