Results 1 to 11 of 11

Thread: Using QString in C function ...

  1. #1
    Join Date
    Mar 2006
    Posts
    26
    Thanks
    2
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Using QString in C function ...

    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.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Using QString in C function ...

    Convert that QString to QByteArray and use QByteArray::data() (remember that the returned pointer will be valid only while that QByteArray exists).

  3. #3
    Join Date
    Mar 2006
    Posts
    26
    Thanks
    2
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Using QString in C function ...

    Can you write me example how can i convert QString to QByteArray ?

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Using QString in C function ...

    http://doc.trolltech.com/4.1/qstring.html

    Look for methods that return QByteArray.

  5. #5
    Join Date
    Mar 2006
    Posts
    26
    Thanks
    2
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Using QString in C function ...

    Brrr, i dont get this QT doc at all, some help please

  6. #6
    Join Date
    Mar 2006
    Posts
    26
    Thanks
    2
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Using QString in C function ...

    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

  7. #7
    Join Date
    Mar 2006
    Posts
    26
    Thanks
    2
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Using QString in C function ...

    Tried this aswell: char *file2 = fileName.toAscii().data(); also get empty.


  8. #8
    Join Date
    Mar 2006
    Posts
    26
    Thanks
    2
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Using QString in C function ...

    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 ??


  9. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Using QString in C function ...

    Quote Originally Posted by Godlike
    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:
    Qt Code:
    1. fileName = QFileDialog::getOpenFileName( this, tr( "Open File" ), QDir::currentPath() );
    To copy to clipboard, switch view to plain text mode 

  10. #10
    Join Date
    Mar 2006
    Posts
    26
    Thanks
    2
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Using QString in C function ...

    Yep, works like a charm. Thanx again.

  11. #11
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Using QString in C function ...

    Quote Originally Posted by Godlike
    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.

    Qt Code:
    1. QByteArray data( fileName.toLocal8Bit() );
    2. char *file2 = qstrdup( data.data() );
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. QPSQL problem
    By LoneWolf in forum Installation and Deployment
    Replies: 60
    Last Post: 4th November 2009, 14:22
  2. Regading Driver to connect Postgresql Database
    By dummystories in forum Installation and Deployment
    Replies: 38
    Last Post: 12th March 2009, 07:19
  3. QPSQL driver in windows
    By brevleq in forum Installation and Deployment
    Replies: 31
    Last Post: 14th December 2007, 12:57
  4. how to add static library into qmake
    By Namrata in forum Qt Tools
    Replies: 1
    Last Post: 20th November 2007, 17:33
  5. Convert from iso-8859-1 to... Something else :-)
    By Nyphel in forum Qt Programming
    Replies: 4
    Last Post: 7th March 2007, 17:59

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.