PDA

View Full Version : setText using a string read from file



Splatify
28th October 2010, 13:23
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:


string line;

ifstream myfile("example.txt");
if (myfile.is_open()) // Checks to see whether myfile opens
{
getline(myfile, line);
ui->QuestionBox->setText(line);

} else ui->QuestionBox->setText("An error has occured!");


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

tbscope
28th October 2010, 15:00
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.

Splatify
28th October 2010, 15:28
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
line.c_str()

Thanks very much :)

tbscope
28th October 2010, 15:39
I just converted the string to QString using
line.c_str()

c_str() returns a QString?