PDA

View Full Version : Qftp Put Image to FTP



harleyskater
28th June 2010, 06:06
I am working with a screenshot upload method for a gaming client, its my first QT project and QT is growing on me fast, I like it! While the client is running, I want to upload screenshots to our ftp. I have looked into the documentation and have set up my project to add the QtNetwork for QFtp.
The problem is everything seems to be working correctly, but after the code has been executed, there is not a image.png in my ftp.





void PGC::shootScreen()
{

originalPixmap = QPixmap(); // clear image for low memory situations

originalPixmap = QPixmap::grabWindow(QApplication::desktop()->winId());

QImage image;
image = originalPixmap.toImage();
QByteArray ba;
QBuffer buffer(&ba);
buffer.open(QIODevice::WriteOnly);
image.save(&buffer, "PNG"); // writes image into ba in PNG format



if (!originalPixmap.save("test.png")) {
// Saving didn't work
QMessageBox::warning(this, "Saving error", "Could not save the file. Check the plugins!");
}

QFtp* connection = new QFtp();
QObject::connect(connection, SIGNAL(done(bool)), this, SLOT(quit()));
connection->connectToHost(ftphost);
connection->login(user, password);
//connection->cd("SS");

if (!connection->put(ba, "test.png", QFtp::Binary)) {
// FTP SS didn't work
QMessageBox::warning(this, "FTP SS Error", "Could not save the SS to FTP!");
}

connection->close( );

}

harleyskater
28th June 2010, 06:29
I also just tried this route. Nothing in the ftp either




void PGC::shootScreen()
{

QDateTime dt = QDateTime::currentDateTime();

QString *dtStr = new QString();
*dtStr = dt.toString("ddMMyyyy_hhmmss");

QString format = "png";
QString filename = "";

filename = *dtStr + tr("_PGC.") + format;


originalPixmap = QPixmap(); // clear image for low memory situations
// on embedded devices.
originalPixmap = QPixmap::grabWindow(QApplication::desktop()->winId());
updateScreenshotLabel();

QImage image;
image = originalPixmap.toImage();
QByteArray ba;
QBuffer buffer(&ba);
buffer.open(QIODevice::WriteOnly);
image.save(&buffer, "PNG"); // writes image into ba in PNG format



if (!originalPixmap.save("test.png")) {
// Saving didn't work
QMessageBox::warning(this, "Saving error", "Could not save the file. Check the plugins!");
}

QFtp* connection = new QFtp();
QObject::connect(connection, SIGNAL(done(bool)), this, SLOT(quit()));
connection->connectToHost(ftphost);
connection->login(user, password);
//connection->cd("SS");

if (!connection->put(ba, "test.png", QFtp::Binary)) {
// FTP SS didn't work
QMessageBox::warning(this, "FTP SS Error", "Could not save the SS to FTP!");
}

QFile *file = new QFile("test.png", this);
file->open(QFile::ReadOnly);

if (!connection->put(file, "test.png")) {
// FTP SS didn't work
QMessageBox::warning(this, "FTP SS Error2", "Could not save the SS to FTP!");
}

connection->close( );

}

harleyskater
28th June 2010, 18:02
Is there a way to check the ftp->put command for progress? or file size or mime type?

amoswood
28th June 2010, 18:59
Is there a way to check the ftp->put command for progress? or file size or mime type?
What version of Qt are you running? In 4.6.3, there is a dataTransferProgress() function in QFtp.

As for the QFtp::put() (http://doc.trolltech.com/latest/qftp.html#put) function, your if statement is going to return immediately if the command was scheduled. You need to check the QFtp::done() (http://doc.trolltech.com/latest/qftp.html#done) and QFtp::error() (http://doc.trolltech.com/latest/qftp.html#error) functions for the status of the transfer.

harleyskater
28th June 2010, 21:17
I went through the documentation and I set some breakpoints on the ftp.error and ftp.currentcommand so I could let some time pass between hits.

They all return the same thing

error = 0 //no error
current = 3 // QFtp::ConnectToHost - connectToHost() is being executed.






void PGC::shootScreen()
{

QDateTime dt = QDateTime::currentDateTime();

QString *dtStr = new QString();
*dtStr = dt.toString("ddMMyyyy_hhmmss");

QString format = "png";
QString filename = "";

filename = *dtStr + tr("_PGC.") + format;


originalPixmap = QPixmap(); // clear image for low memory situations
// on embedded devices.
originalPixmap = QPixmap::grabWindow(QApplication::desktop()->winId());
updateScreenshotLabel();

QImage image;
image = originalPixmap.toImage();
QByteArray ba;
QBuffer buffer(&ba);
buffer.open(QIODevice::WriteOnly);
image.save(&buffer, "PNG"); // writes image into ba in PNG format



if (!originalPixmap.save("test.png")) {
// Saving didn't work
QMessageBox::warning(this, "Saving error", "Could not save the file. Check the plugins!");
}

QFtp* connection = new QFtp();
QObject::connect(connection, SIGNAL(done(bool)), this, SLOT(quit()));
connection->connectToHost(ftphost);
connection->login(user, password);

//connection->cd("SS");

if (!connection->put(ba, "test.png", QFtp::Binary)) {
// FTP SS didn't work
QMessageBox::warning(this, "FTP SS Error", "Could not save the SS to FTP!");
}

/*QFile *file = new QFile("test.png", this);

if (!file->open(QFile::ReadOnly))
{
// FTP SS didn't work
QMessageBox::warning(this, "FTP SS Error3", "Could not open the SS !");
file->close();
delete file;
}

if (!connection->put(file, "test.png")) {
// FTP SS didn't work
QMessageBox::warning(this, "FTP SS Error2", "Could not save the SS to FTP!");
} */

QVariant ftperror = connection->error();
QVariant ftpcurrent = connection->currentCommand();
ftperror = connection->error();
ftpcurrent = connection->currentCommand();
ftperror = connection->error();
ftpcurrent = connection->currentCommand();
ftperror = connection->error();
ftpcurrent = connection->currentCommand();
ftperror = connection->error();
ftpcurrent = connection->currentCommand();


connection->close( );

}

amoswood
28th June 2010, 21:36
The QFtp class works asynchronously to transfer data. So, calling the connecToHost, put, error, currentCommand, and close functions sequentially will never actually execute any of the commands. What you need to do is write a class so that you can use signals and slots. Catching the signals after you start transferring is the key. The example listed in the QFtp (http://doc.trolltech.com/latest/qftp.html) detailed description gives some clarification to your issue.

Hope this helps.

harleyskater
28th June 2010, 23:16
I also noticed, because I tried to show a friend what I was working on by passing him the build exe that anywhere I put "ftp = new QFtp();" it crashes the app but when I execute it through the IDE it doesn't crash. I have the project setup for ftp.

Vit Stepanek
29th June 2010, 09:57
Just put the command connection->close( ); to a slot catching the done() signal. You're closing the connection before the actions are executed.

harleyskater
29th June 2010, 19:56
Thank you Vit! I wasn't sure how that connection->close( ); would execute and when! but you are right. works great now! except for the problem where it won't execute outside of the IDE. I debugged in release mode and it runs from the IDE and it works, uploads to the FTP, but if I execute it outside of the IDE it crashes when it hits the FTP code.

harleyskater
30th June 2010, 19:09
I found this page for documentation,

http://doc.trolltech.com/4.3/deployment-windows.html

The only thing I saw for Visual Studios that I wasn't doing was copying the plugins DIR to my release folder.

I copied the entire Plugins dir to my release dir and I have the same problem

harleyskater
5th July 2010, 20:09
still no advice on this? because im still stuck.

squidge
5th July 2010, 22:48
Well, Qt Creator executes the exe too, so if it crashes outside of Qt Creator, then your environment is different (eg. path)

harleyskater
7th July 2010, 04:29
Okay I went back and set my Output Directory so my exe goes into the same folder as the rest of my release

now when I compile, my exe goes to my Projects /Release Folder and not my Solutions.

My program is still crashing outside of my IDE, and I believe you are right that its my envirornment outside of my IDE that is causing the crash but I am not sure what part of my environment.

It is crashing at this line. I have in my project the need to include a lot of dll's but I am thinking it would be the Network DLL that would be causing the problem since it is crashing at this line: QFtp *connectionftp = new QFtp(this);

I am attaching a screenshot of my release folder

harleyskater
17th July 2010, 07:26
project stilllll isn't working did anyone take a look at that screenshot? is it possible its the library and not the dll?

squidge
17th July 2010, 07:46
Where do you output that error?

harleyskater
22nd July 2010, 19:48
that error is actually just a message box I put before and after the line of code that is failing to track down where the app is failing, kinda like a die statement.

QFtp *connectionftp = new QFtp(this);

squidge
22nd July 2010, 20:26
So you took a picture of a message box you created yourself and are asking us why that message box appears?

harleyskater
22nd July 2010, 22:25
no, I am wondering why my program crashes when it reaches this line of code

QFtp *connectionftp = new QFtp(this);

I took the screenshot only to show that I have all the dll's located in the builds dir.

It executes great when ran within MSVS2008 but the exe outside of the IDE crashes on that line. I took the screenshot to show I am including what I need to.

squidge
22nd July 2010, 22:56
Well, MSVS2008 doesn't run your program from the "Release" dir, it's runs it from your projects home directory (which is typically where your source code is) so it's not using the DLLs in your release directory when its running from within the IDE.

So, delete all the files in the release directory. All of them. Then do a full rebuild and see if it works then, from a VS2008 command prompt. If it does, there's something wrong with one of the DLLs which was there.

neveffects
5th January 2011, 07:09
Any body please look into my thread "Slow FTP Program using QFtp" Its very Urgent and Please help me.

Thanks in advance