Related to Qt3--Beginner


Iam doing an editor using Qt3-C++..and i want to display the contents of a existing file in a text editor.the code i tried out is...


FILE *pFile;
char *mystring=new char[100];

SysInit::SysInit( ) : QMainWindow( 0, "..........", WDestructiveClose )
{
e = new QTextEdit( this );
e->setGeometry(0,0,1000,1000);
timer.start( 1 );
connect( &timer, SIGNAL( timeout() ), this, SLOT( fileread() ) );
}

void SysInit::fileread()
{
pFile = fopen ("main.cpp" , "r");
if (pFile == NULL) perror ("Error opening file");

while(!feof)
{
fgets((char *)mystring, 100,pFile);
e->setText((char *)mystring);

fclose (pFile);

}

I want to display the contents of the read file to the text editor..but when i use the while loop, its not displaying anything...but when i delete the loop, its just printing the first line of the file only..

when i use simple cpp coding for reading and displaying the file using while loop, its works properly and displays the contents in the console..

It seems problem with Qt3..what to do for displaying the contents in the texteditor.
The QTextEdit::setText() function does not append text, it sets all the text. The correct function is QTextEdit::append().

Frankly, though, I think you are going about it the hard way. Qt provides classes that will handle files and stream them into the editor for you.

The online Qt documentation is really the best source for finding answers like this. Quite often you will find examples that do something at least similar to what you want to do. There is a whole section in the QTextEdit page about Using QTextEdit as a Display Widget
http://doc.trolltech.com/3.3/qtextedit.html#1-1

Hope this helps.
Topic archived. No new replies allowed.