PDA

View Full Version : QT Desktop Screenshot not working



harleyskater
26th June 2010, 07:58
I am working on a QT program, its my first QT C++ app and I am using MSVS 2008 as my IDE.

My app runs great when I debug in Release mode and it takes a screenshot and puts it in the apps DIR. Now I want to pass it to a friend to make sure it works on Vista. So I go to test it on my comp outside of the IDE by just clicking the generated exe in my Apps Release folder and it starts up and all the code is working great with no errors except the screenshots. It wasnt generating errors but it also wasn't taking the screenshots even though I know that section of code is being hit.

I tried to switch from an implicit app path for the scrrenshot file, to an explicit C:\test.jpeg and it works again within the IDE and not outside of it. Im at a loss right now for why this would happen. I also deleted my Release folder and recompiling to make sure it was refreshing all the files.




#include "thewge.h"
#include "ui_thewge.h"
#include <QtGui/QApplication>
#include <QSignalMapper>
#include <QPushButton>
#include <QWidget>
#include <QTimer>
#include <QDesktopWidget>
#include <QPixmap>
#include <windows.h>
#include <iostream>
#include "md5.h"
#include "mysql++.h"

TheWGE::TheWGE(QWidget *parent, Qt::WFlags flags)
: QMainWindow(parent, flags)
{

ui.setupUi(this);

QObject::connect(ui.LoginBtn, SIGNAL(clicked()), this, SLOT(login()));

}

TheWGE::~TheWGE()
{
//ui.usrLineEdit->clear();
//ui.pwdLineEdit->clear();
}

void TheWGE::login()
{

if (ui.usrLineEdit->text().trimmed().isEmpty() )
{
ui.usrLineEdit->setFocus();
}
else if (ui.pwdLineEdit->text().trimmed().isEmpty() )
{
ui.pwdLineEdit->setFocus();
}

mysqlpp::Connection con(false);

if (!con.connect("db", "host", "user", "pass"))
{
qApp->exit(0);

}

// Query Mysql
mysqlpp::Query query = con.query();

QString Qstr1 = ui.usrLineEdit->text();
QString Qstr2 = ui.pwdLineEdit->text();

std::string acsUserName2 = Qstr1.toUtf8().data();
std::string acsPassword2 = Qstr2.toUtf8().data();


query << "SELECT username, password FROM wge_db_user WHERE username = '" << acsUserName2 << "' and password = '" << md5(acsPassword2) << "'";

mysqlpp::StoreQueryResult res = query.store();
mysqlpp::Row row;


if (res.num_rows() > 0)
{
//goodlogin - show new form and hide this form
shootScreen();
QTimer::singleShot (60000, this, SLOT(shootScreen()));
}
else
{
//badlogin- code more here laterrrrrrrrr
qApp->exit(0);

}

mysqlpp::Connection::thread_end();


return;
}

void TheWGE::shootScreen()
{

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

originalPixmap.save("test.jpeg");

}

tbscope
26th June 2010, 08:08
The first thing I would check is the return value of originalPixmap.save("test.jpeg");
Maybe show a message box when it failed.

When you run a program outside an IDE and it doesn't perform in the same way as in the IDE, then the first thing I check is if the environments are the same. Thus: check if you can actually find the Qt plugins from outside the IDE. The IDE might have an environment to automatically find them, but outside it, they might not be found.

Therefor, check the saving, as it makes use of such a plugin. If it fails to save, you know it is a problem with your environment, and you need to change some variables. If it doesn't fail, the problem is somewhere else.

harleyskater
26th June 2010, 08:55
when you say check the value of originalPixmap.save("test.jpeg");

I am not sure how to do that or how to catch an error.

Here is a messagebox I added, I could add the error details there if I knew how. I am going to research more!

I also want to say that I have the QtGui4.dll visable




void TheWGE::shootScreen()
{

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

originalPixmap.save("test.jpeg");

QMessageBox::critical( 0, "TheWGE",
QString("ERROR: ") + errorDetails +
"\n\nApplication will now exit." );


}

harleyskater
26th June 2010, 09:24
I came up with this idea. Its also having an error saying that isnull is not a member of Qpixmap




if(originalPixmap.isnull())
(
QMessageBox::critical( 0, "TheWGE",
QString("ERROR: ") +
"\n\n Screenshot." );
)

squidge
26th June 2010, 09:29
try isNull instead of isnull

harleyskater
26th June 2010, 18:25
alright, I got the if statement working thx to Juicy.

It doesn't display the message box, outside or inside the IDE, but if I comment this like out:

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

It displays the messagebox. I am still at a loss to why my screenshots would only be working inside the IDE and not from the actual build.exe




void TheWGE::shootScreen()
{

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

if(originalPixmap.isNull())
{
QMessageBox::critical( 0, "TheWGE",
QString("ERROR: ") +
"\n\nApplication will now exit." );
}

originalPixmap.save("test.jpeg");


}



Here is my Build info also.




Build Log Build started: Project: TheWGE, Configuration: Release|Win32
Command Lines Creating temporary file "c:\Documents and Settings\Harleys\My Documents\Visual Studio 2008\Projects\trunk\TheWGE\Release\RSP000001213228 0.rsp" with contents
[
/OUT:"c:\Documents and Settings\Harleys\My Documents\Visual Studio 2008\Projects\trunk\TheWGE\Release\TheWGE.exe" /LIBPATH:"C:\Qt\4.6.3\lib" /LIBPATH:"C:\mysql++\vc2005\Release" /LIBPATH:"C:\Program Files\MySQL\MySQL Server 5.1\lib\opt" /MANIFEST /MANIFESTFILE:"Release\TheWGE.exe.intermediate.manifest" /MANIFESTUAC:"level='asInvoker' uiAccess='false'" /DEBUG /PDB:"c:\Documents and Settings\Harleys\My Documents\Visual Studio 2008\Projects\trunk\TheWGE\Release\TheWGE.pdb" /SUBSYSTEM:WINDOWS /DYNAMICBASE:NO qtmain.lib QtCore4.lib QtGui4.lib QtSql4.lib mysqlpp.lib libmysql.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib

".\Release\main.obj"

".\Release\md5.obj"

".\Release\thewge.obj"

".\Release\qrc_thewge.obj"

".\Release\moc_thewge.obj"

".\Release\TheWGE.res"
]
Creating command line "link.exe @"c:\Documents and Settings\Harleys\My Documents\Visual Studio 2008\Projects\trunk\TheWGE\Release\RSP000001213228 0.rsp" /NOLOGO /ERRORREPORT:PROMPT"
Creating temporary file "c:\Documents and Settings\Harleys\My Documents\Visual Studio 2008\Projects\trunk\TheWGE\Release\RSP000002213228 0.rsp" with contents
[
/outputresource:".\Release\TheWGE.exe;#1" /manifest

".\Release\TheWGE.exe.intermediate.manifest"
]
Creating command line "mt.exe @"c:\Documents and Settings\Harleys\My Documents\Visual Studio 2008\Projects\trunk\TheWGE\Release\RSP000002213228 0.rsp" /nologo"
Creating temporary file "c:\Documents and Settings\Harleys\My Documents\Visual Studio 2008\Projects\trunk\TheWGE\Release\BAT000003213228 0.bat" with contents
[
@echo Manifest resource last updated at %TIME% on %DATE% > ".\Release\mt.dep"
]
Creating command line """c:\Documents and Settings\Harleys\My Documents\Visual Studio 2008\Projects\trunk\TheWGE\Release\BAT000003213228 0.bat"""
Output Window Linking...
LINK : c:\Documents and Settings\Harleys\My Documents\Visual Studio 2008\Projects\trunk\TheWGE\Release\TheWGE.exe not found or not built by the last incremental link; performing full link
Embedding manifest...
Results Build log was saved at "file://c:\Documents and Settings\Harleys\My Documents\Visual Studio 2008\Projects\trunk\TheWGE\Release\BuildLog.htm"
TheWGE - 0 error(s), 0 warning(s)

harleyskater
26th June 2010, 19:28
I took the next step in trying to solve this, and started a new project for just the screenshot. I built it as simple as possible and I am getting the same result. I am providing a link to my zipped soulution.

http://www.mediafire.com/?gkkdzkne3nj

harleyskater
26th June 2010, 22:24
Development.

I came across the Example Desktop Screenshot demo from QT and it used a label to display the screenshot, I have used this code to problemsolve if the screenshot is actually being taken, and it is! so the screenshot is working! its just the save command!

Which means I am at the last step of getting this to work!

does anyone see anything wrong with this line of code? or have any ideas why this would only be working inside the IDE build.exe?

originalPixmap.save("test.jpeg");

tbscope
26th June 2010, 22:49
Yes, like I said, plugin problems.
Your IDE finds the plugins, your exe on its own doesn't.



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

harleyskater
26th June 2010, 22:59
thanks Tbscope, Do you have an idea how which plugin it might be and how to add it?

totem
27th June 2010, 02:04
and if you try to place $QTDIR/plugins/imageformats contents (dlls or corresponding dir tree) beside deployed executable ?

harleyskater
27th June 2010, 04:25
Totem! Hi buddy. I just tried to take the imageformats DIR and copied everything into my release folder containing everything else that is being used with the exe and I am getting the warning message still. I also tried adding the DIR to my additional include directories.


ANOTHER thing I found is, if I change the MIME type from jpeg to png.... IT WORKS!!!!!!!!!!!!