PDA

View Full Version : open a file



aekilic
28th October 2008, 07:43
Dear All

I have file in c:\windows\local files\temp\picture.jpg, I would like to open the file. How can I do it?

spirit
28th October 2008, 07:47
if you need just open the file then use QFile, if you need to show this image then use QPixmap.

aekilic
28th October 2008, 07:55
the problem is not only a image, we have word files, excel files, we could read the file, but we could not open it.

spirit
28th October 2008, 08:00
please, show code what we do.

aekilic
28th October 2008, 08:04
QFile *tmpFile = new QFile ( QString ( QDir::tempPath() + "/" + tmpFile->fileName() ) );
if ( !tmpFile->open ( QIODevice::WriteOnly) )
{
return;
}
qint64 size = tmpFile->write ( ba1 );
if ( size != -1 )
{
tmpFile->setFileName(realname);
QProcess::startDetached ( tmpFile->fileName() );
}

After this it saves the file to temp folder but I would like to open the file after that.

spirit
28th October 2008, 08:11
if you cooment this line


...
QProcess::startDetached ( tmpFile->fileName() );
...

and then try to open this file, does it open?

Kumosan
28th October 2008, 08:14
What do you mean with 'open the file after that'? What you do is write it to disk and afterwards you try to start it as executable.

aekilic
28th October 2008, 08:17
if you cooment this line


...
QProcess::startDetached ( tmpFile->fileName() );
...

and then try to open this file, does it open?

Yes, I try to open the file, I mean like if it is a openoffice file I would like to start open office to see the file.

But what I think is QProcess::startDetached ( tmpFile->fileName() ); doesnt work...

Kumosan
28th October 2008, 08:20
But what I think is QProcess::startDetached ( tmpFile->fileName() ); doesnt work...

Of course not. If we take an open office file as example, you are trying to execute the .odf file with QProcess, not the .exe.

aekilic
28th October 2008, 08:33
ok then should I write,



QProcess::execute( tmpFile->fileName() );

??

spirit
28th October 2008, 08:40
your file isn't executable. you need specify needed program and your file as an argument.
somethig like this will work


QProcess::execute("notepad.exe", QStringList() << "d:/phones.txt");

aekilic
28th October 2008, 08:44
for the file, I never know what the user saves,

It could be pdf, odt, dwg? So I dont know the program, any way to do it withour writing the program.

Kumosan
28th October 2008, 08:53
For what os do you write the program? Windows? Linux?

aekilic
28th October 2008, 08:56
windows xp ...

spirit
28th October 2008, 09:00
then try to use ShellExecute or ShellExecuteEx.

chaoticbob
28th October 2008, 09:17
If you're trying to open files in their respective applications, you might want to take a look at
openUrl in the QDesktopServices class:


QDesktopServices::openUrl

spirit
28th October 2008, 09:21
If you're trying to open files in their respective applications, you might want to take a look at
openUrl in the QDesktopServices class:


QDesktopServices::openUrl
yep, it will work too. :)

aekilic
28th October 2008, 10:19
tried this


QUrl my_url;
my_url = QUrl::fromLocalFile(tmpFile->fileName());
QDesktopServices::openUrl(my_url);

didnt work...

aekilic
28th October 2008, 11:21
Dear all thank you very much, I have just understand I made a mistake for the QUrl

Thank you very much again...