Results 1 to 4 of 4

Thread: setText using a string read from file

  1. #1
    Join Date
    Oct 2010
    Posts
    54
    Thanks
    8
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Question setText using a string read from file

    Hi all, I hope this is an easy question. How do I go about solving this problem I have with setText

    This is my code:

    Qt Code:
    1. string line;
    2.  
    3. ifstream myfile("example.txt");
    4. if (myfile.is_open()) // Checks to see whether myfile opens
    5. {
    6. getline(myfile, line);
    7. ui->QuestionBox->setText(line);
    8.  
    9. } else ui->QuestionBox->setText("An error has occured!");
    To copy to clipboard, switch view to plain text mode 


    It keeps throwing up an error. Is this because setText is supposed to have a QString associated with it... if so how do i overcome this problem. C++ didn't confuse me until I started learning QT, now nothing seems to work

    Thanks for your help

  2. #2
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: setText using a string read from file

    Quote Originally Posted by Splatify View Post
    It keeps throwing up an error. Is this because setText is supposed to have a QString associated with it...
    Try it, test it, ...
    But I think you're on to something ;-)

    if so how do i overcome this problem.
    Use a QString. See the documentation.

    C++ didn't confuse me until I started learning QT, now nothing seems to work
    Then you still have things to learn. Qt doesn't change how you use C++, it's just a collection of classes. The only thing extra is an extra precompiler for the signals and slots.

  3. #3
    Join Date
    Oct 2010
    Posts
    54
    Thanks
    8
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Wink Re: setText using a string read from file

    Hi I have solved my problem now

    Thanks for the help... I think. :/

    It was very easy. I just converted the string to QString using
    Qt Code:
    1. line.c_str()
    To copy to clipboard, switch view to plain text mode 

    Thanks very much

  4. #4
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: setText using a string read from file

    Quote Originally Posted by Splatify View Post
    I just converted the string to QString using
    Qt Code:
    1. line.c_str()
    To copy to clipboard, switch view to plain text mode 
    c_str() returns a QString?

Similar Threads

  1. Replies: 1
    Last Post: 23rd May 2011, 05:53
  2. [s60] file 'string' cannot be opened
    By estel in forum Newbie
    Replies: 4
    Last Post: 2nd July 2009, 00:25
  3. is qt phonon can read realmedia file and divx file
    By fayssalqt in forum Qt Programming
    Replies: 1
    Last Post: 27th January 2009, 16:42
  4. writing in to a string via FILE*
    By caduel in forum General Programming
    Replies: 3
    Last Post: 12th July 2008, 10:08
  5. read input string --> text mode
    By eleanor in forum Qt Programming
    Replies: 1
    Last Post: 25th October 2007, 19:08

Tags for this Thread

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.