PDA

View Full Version : Using QString in C function ...



Godlike
8th April 2006, 13:15
How can i get QString fileName which contains a name of a opened file, to be stored in
char file[] = ""; ?

so that file[] stores the Qstring value of fileName

thx.

jacek
8th April 2006, 13:35
Convert that QString to QByteArray and use QByteArray::data() (remember that the returned pointer will be valid only while that QByteArray exists).

Godlike
8th April 2006, 14:44
Can you write me example how can i convert QString to QByteArray ?

jacek
8th April 2006, 15:09
http://doc.trolltech.com/4.1/qstring.html

Look for methods that return QByteArray.

Godlike
8th April 2006, 15:19
Brrr, i dont get this QT doc at all, some help please :)

Godlike
8th April 2006, 15:44
Bah i dont get docs correctly it seems:

QByteArray fil;
fileName.append(fil);
char *file = fil.data();

fileName is defined in .h file as QString fileName and it gets value later.

If i have done that what i wrote above i get in file nothing.
What have i read wrong in docs. Would need s example explanation of docs too ;)

Godlike
8th April 2006, 16:08
Tried this aswell: char *file2 = fileName.toAscii().data(); also get empty.

:confused:

Godlike
8th April 2006, 16:28
Found the problem. I have defined QString fileName; in .h as private. Now i assign a value to it in function when i open the file. But how do i acess it in another function ? I thought its already visible inside that function, but its not, its empty, so how do i assigne the value in that function so i can see it in another function too. Now i get the value like this:

QString fileName = QFileDialog::getOpenFileName(this,tr("Open File"), QDir::currentPath());

In function which i read the file. So how do i see the same string in another function ??

:o :confused:

jacek
8th April 2006, 16:52
QString fileName = QFileDialog::getOpenFileName(this,tr("Open File"), QDir::currentPath());
Here you create a new fileName variable, if you have declared fileName as a member variable of your class, use this:
fileName = QFileDialog::getOpenFileName( this, tr( "Open File" ), QDir::currentPath() );

Godlike
8th April 2006, 17:00
Yep, works like a charm. Thanx again.

jacek
8th April 2006, 17:01
Tried this aswell: char *file2 = fileName.toAscii().data(); also get empty.
Because you create a temporary QByteArray that immediately goes out of scope and leaves file2 as a dangling pointer.

There was a thread about this problem, but I can't find it now.


QByteArray data( fileName.toLocal8Bit() );
char *file2 = qstrdup( data.data() );