save contents QTextEdit, QStream

256 views
Skip to first unread message
Assigned to luigfe...@gmail.com by me

Luigi Ferraris

unread,
Apr 25, 2016, 4:42:41 AM4/25/16
to QtContribs
Hi guys,
I want save a file using Qt classes. Contents is stored in a QTextEdit

To read, I follow next code and works fine
LOCAL oQfile
     
oQfile := QFile()
     oQfile:setFile( hb_FnameMerge( <thePath>, <theName>, "theExt" ) )
IF oQFile:exists() .AND. ;
         oQFile:open( hb_BitOr( QIODevice_ReadOnly, QIODevice_Text ) )
         myTextEdit:setPlainText( oQFile:readAll() )
      END IF


Bu to write out... I found next code

LOCAL oQfile := QFile()
LOCAL oQStream

oQfile:setFile( hb_FnameMerge( <thePath>, <theName>, "theExt" ) )
oQFile:open( hb_BitOr( QIODevice_ReadWrite, QIODevice_Text ) )
oQStream := QTextStream( oQFile )

but I dont' know how to replicate next line (is C++ copied from example on internet)
out << ui->QTextEdit->toPlainText() << endl;

oQfile:flush()
oQfile:close()


or if is there another way to save.

Many thanks for any help

Regards
Luigi Ferraris



Dušan D. Majkić

unread,
Apr 26, 2016, 10:57:01 AM4/26/16
to qtcon...@googlegroups.com
> ui->QTextEdit->toPlainText()

oQFile:write( QTextEdit:toPlainText() )

Luigi Ferraris

unread,
Apr 26, 2016, 2:28:05 PM4/26/16
to qtcon...@googlegroups.com
Il 26/04/2016 16.56, Dušan D. Majkić ha scritto:
ui->QTextEdit->toPlainText()
oQFile:write( QTextEdit:toPlainText() )
Many thanks for your answer, but I have a doubt

oQStream := QTextStream( oQFile )

but I dont' know how to replicate next line (is C++ copied from example on internet)
out << ui->QTextEdit->toPlainText() << endl;

In the last line out is oQStream object and it "receives" data using "<<" (I'm not a C programmer) that is an operator.
From Qt doc: "Writes the string to the stream and returns a reference to the QTextStream. The string is first encoded using...." etc.
So, I think it can be (more or less, but it is wrong because I'm using CLIPPER assignment) like oQStream := QTextEdit:toPlainText()

Anyway, I will try your suggestion.

Many thanks.
Luigi

Dušan D. Majkić

unread,
Apr 27, 2016, 4:55:11 AM4/27/16
to qtcon...@googlegroups.com
> QStream object and it "receives" data using "<<"

QStream is proxy object that supports operator overloading - eg that
fancy "<<" thing

So the line here:

oQStream := QTextStream( oQFile )

makes a stream to QFile so it can receive data via "<<" operator.
HB does not support operator overloading (or no one wrote command for that)
So this simple C++ line:

stream = QTextStream(file);
stream << something << endl;

is the same as:

oFile:write(something)
oFile:write(endl)

There is, perhaps, potential issue of character encoding. But that is
also HB specific.
Reply all
Reply to author
Forward
0 new messages