Trolltech Home | Qt-jambi-interest Home | Recent Threads | All Threads | Author | Date
All threads index page 1

Qt-jambi-interest Archive, April 2007
QTextEdit and QTextBlock


Message 1 in thread

hello and good morning

I found a strange behavior of QTextEdit / QTextBlock, I did not really 
understand. The problem ist, that the original c++ source was written 
for Qt.3x, so I have no prove, that it works on Qt 4.x

As far as I understand, QTextEdit.findBlock(index) gets the textblock, 
labeled by index. Reading the text of this textblock always return the 
data from the first textblock, no matter of the index (wich as I 
understand is similar to a paragraph). In the followin source ... 
findBlock(index).text()always retrieve the text of the first paragraph

I changed the code working to an iteration, that's working. But the 
error (if it is an error an not my fault) might be interesting.

greetings
Arne

----------------------------------------------------------------------------------------
// this example does not work
public List<DbEintrag> GetDatenbankListe() {
     List<DbEintrag> db_liste = new LinkedList<DbEintrag>();
     int zeilen = m_ui.DatenbankEdit.document().blockCount();
     for (int i = 0; i < zeilen; i++) {
	// PROBLEM, always the text from the first block
         String datenbankString =
                m_ui.DatenbankEdit.document().findBlock(i).text();
         if (datenbankString.isEmpty() == false) {
             String datenbank = "";
             String server = "";
             datenbankString.replace(" ","");
             int atPos = datenbankString.indexOf("@");
             if (atPos > 0) {
                 datenbank = datenbankString.substring(atPos);
                 server = datenbankString.substring(atPos +1);
             }
             else {
                 datenbank = datenbankString;
             }
             db_liste.add(new DbEintrag(datenbank,server));
         }
     }
     return db_liste;
}

----------------------------------------------------------------------------------------
// this example works
public List<DbEintrag> GetDatenbankListe() {
     List<DbEintrag> db_liste = new LinkedList<DbEintrag>();
     QTextBlock block = m_ui.DatenbankEdit.document().begin();
     while (block.isValid()) {
         String datenbankString = block.text(); // FINE
         if (datenbankString.isEmpty() == false) {
             String datenbank = "";
             String server = "";
             datenbankString.replace(" ","");
             int atPos = datenbankString.indexOf("@");
             if (atPos > 0) {
                 datenbank = datenbankString.substring(atPos);
                 server = datenbankString.substring(atPos +1);
             }
             else {
                 datenbank = datenbankString;
             }
             db_liste.add(new DbEintrag(datenbank,server));
         }
         block = block.next();
     }
     return db_liste;
}


Message 2 in thread

On Sunday 29 April 2007 08:43:10 Arne Stocker wrote:
> hello and good morning
>
> I found a strange behavior of QTextEdit / QTextBlock, I did not really
> understand. The problem ist, that the original c++ source was written
> for Qt.3x, so I have no prove, that it works on Qt 4.x
>
> As far as I understand, QTextEdit.findBlock(index) gets the textblock,
> labeled by index. Reading the text of this textblock always return the
> data from the first textblock, no matter of the index (wich as I
> understand is similar to a paragraph). In the followin source ...
> findBlock(index).text()always retrieve the text of the first paragraph
>
> I changed the code working to an iteration, that's working. But the
> error (if it is an error an not my fault) might be interesting.

Your second example is correct. As the documentation of findPos() tries to 
indicate the position argument is an absolute position of characters within 
the document, not a block index. Qt 3's QTextEdit API was indeed 
paragraph /index/ based on the other hand. But anyway, your way of iterating 
with QTextBlock.isValid() and .next() is just fine :)

Simon

> ---------------------------------------------------------------------------
>------------- // this example does not work
> public List<DbEintrag> GetDatenbankListe() {
>      List<DbEintrag> db_liste = new LinkedList<DbEintrag>();
>      int zeilen = m_ui.DatenbankEdit.document().blockCount();
>      for (int i = 0; i < zeilen; i++) {
> 	// PROBLEM, always the text from the first block
>          String datenbankString =
>                 m_ui.DatenbankEdit.document().findBlock(i).text();
>          if (datenbankString.isEmpty() == false) {
>              String datenbank = "";
>              String server = "";
>              datenbankString.replace(" ","");
>              int atPos = datenbankString.indexOf("@");
>              if (atPos > 0) {
>                  datenbank = datenbankString.substring(atPos);
>                  server = datenbankString.substring(atPos +1);
>              }
>              else {
>                  datenbank = datenbankString;
>              }
>              db_liste.add(new DbEintrag(datenbank,server));
>          }
>      }
>      return db_liste;
> }
>
> ---------------------------------------------------------------------------
>------------- // this example works
> public List<DbEintrag> GetDatenbankListe() {
>      List<DbEintrag> db_liste = new LinkedList<DbEintrag>();
>      QTextBlock block = m_ui.DatenbankEdit.document().begin();
>      while (block.isValid()) {
>          String datenbankString = block.text(); // FINE
>          if (datenbankString.isEmpty() == false) {
>              String datenbank = "";
>              String server = "";
>              datenbankString.replace(" ","");
>              int atPos = datenbankString.indexOf("@");
>              if (atPos > 0) {
>                  datenbank = datenbankString.substring(atPos);
>                  server = datenbankString.substring(atPos +1);
>              }
>              else {
>                  datenbank = datenbankString;
>              }
>              db_liste.add(new DbEintrag(datenbank,server));
>          }
>          block = block.next();
>      }
>      return db_liste;
> }


Attachment:

Attachment: pgpIPBILmQ5r7.pgp
Description: PGP signature