Trolltech Home | Qt-interest Home | Recent Threads | All Threads | Author | Date
All threads index page 5

Qt-interest Archive, June 2007
The RIGHT WAY to append a file to a path via QDir


Message 1 in thread

Hi. I want to create a string containing an absolute file path for a  
file - let's say its called "foo.txt" and I want to place it in the  
system temporary directory.

So, currently I have code that looks like this:

QString myFileName = QString("foo.txt");
QString myPath = QString("%1%2").arg(QDir::tempPath()).arg(myFileName);

On MacOS X, code like this was working, but on Windows it fails  
because I guess the tempPath() method returns a path that doesn't end  
with a separator.

Therefore, I tried this:

QString myFileName = QString("foo.txt");
QString myPath;
QString & tempPath = QDir::tempPath;

if (tempPath.endsWith(QDir::separator())) {
	myPath = QString("%1%2").arg(tempPath).arg(myFileName);
} else {
	myPath =  
QString("%1%2%3).arg(tempPath).arg(QDir::separator()).arg(myFileName);
}

This doesn't work either because for whatever reason, the string that  
QDir::tempPath() returns uses forward slash characters and the  
QDir::separator() call produces a backslash.

Is there an actual facility for doing this extremely common task that  
I am just not seeing in the docs?

Brant Sears

--
 [ signature omitted ] 

Message 2 in thread

How about this:
QFileInfo fileInfo (QDir::tempPath(), "foo.txt")
QString myPath = fileInfo.absoluteFilePath();

Clint

On Tuesday 19 June 2007 3:30:08 pm Brant Sears wrote:
> Hi. I want to create a string containing an absolute file path for a
> file - let's say its called "foo.txt" and I want to place it in the
> system temporary directory.
>
> So, currently I have code that looks like this:
>
> QString myFileName = QString("foo.txt");
> QString myPath = QString("%1%2").arg(QDir::tempPath()).arg(myFileName);
>
> On MacOS X, code like this was working, but on Windows it fails
> because I guess the tempPath() method returns a path that doesn't end
> with a separator.
>
> Therefore, I tried this:
>
> QString myFileName = QString("foo.txt");
> QString myPath;
> QString & tempPath = QDir::tempPath;
>
> if (tempPath.endsWith(QDir::separator())) {
> 	myPath = QString("%1%2").arg(tempPath).arg(myFileName);
> } else {
> 	myPath =
> QString("%1%2%3).arg(tempPath).arg(QDir::separator()).arg(myFileName);
> }
>
> This doesn't work either because for whatever reason, the string that
> QDir::tempPath() returns uses forward slash characters and the
> QDir::separator() call produces a backslash.
>
> Is there an actual facility for doing this extremely common task that
> I am just not seeing in the docs?
>
> Brant Sears
>
> --
> To unsubscribe - send a mail to qt-interest-request@xxxxxxxxxxxxx with
> "unsubscribe" in the subject or the body. List archive and information:
> http://lists.trolltech.com/qt-interest/


--
 [ signature omitted ] 

Message 3 in thread

Hi,

Brant Sears wrote:
> Hi. I want to create a string containing an absolute file path for a 
> file - let's say its called "foo.txt" and I want to place it in the 
> system temporary directory.

<snip>

> Is there an actual facility for doing this extremely common task that I 
> am just not seeing in the docs?

How about:

QDir::temp().absoluteFilePath( "foo.txt" );

Tim

--
 [ signature omitted ]