Russian Qt Forum
Ноябрь 25, 2024, 04:55 *
Добро пожаловать, Гость. Пожалуйста, войдите или зарегистрируйтесь.
Вам не пришло письмо с кодом активации?

Войти
 
  Начало   Форум  WIKI (Вики)FAQ Помощь Поиск Войти Регистрация  

Страниц: [1]   Вниз
  Печать  
Автор Тема: Проблема с добавлением текста в QTextEdit  (Прочитано 12558 раз)
xor
Гость
« : Февраль 06, 2006, 14:58 »

Есть следующая проблема. Мне нужно добавить текстовый блок в начало документа, приатачченого к текст эдиту без автоскроллинга на этот текст блок.

Я делаю следующее

Код:
QTextCursor cursor = textEdit->textCursor();
cursor.movePosition(QTextCursor::Start);

QTextBlockFormat bf;
QTextCharFormat cf;

cursor.insertBlock(bf, cf);
cursor.insertText(text);


После этого текст эдит автоматически проскролливается на новый текст блок. Как избежать этого?

PS: cursor.scrollToAnchor() не подходит, так как операция слишком медленная и при частом добавлении новых текст блоков текст эдит очень тормозит.
Записан
ElderOrb
Гость
« Ответ #1 : Февраль 06, 2006, 21:14 »

Огромная просьба к счастливым обладателям лицензии: Если вас не затруднит, спросите это у trolltrech-а, пожалуйста.. Этот вопрос похоже не разрешим путём простых плясок с бубном.
Записан
Steven_Orko
Гость
« Ответ #2 : Февраль 06, 2006, 22:09 »

Ты напиши вопрос на английском языке, я спрошу.
Записан
ElderOrb
Гость
« Ответ #3 : Февраль 06, 2006, 23:20 »

There are some problems with a QTextEdit, QTextDocument and other text-related classes..

1. How to make QTextDocument without first empty line? (I want my first inserted block be in the first line of QTextEdit, not second!)

2. QTextEdit isn't fast enough to show the whole document I need (more than 10 mb xml),  that is why I try to show it by blocks. When user drags custom scrollbar, I've implemented the function which create new QTextDocument and fill it with 100 QTextBlock from xml file. When user presses scrollbar arrows  two another functions are called, which prepend or append QTextBlock to the beginning or to the end of QTextDocument and removes block from other side of document.

For examples I do the following:
   ui.textEdit->document()->documentLayout()->blockSignals(true);

   QTextCursor cursor(textDocument);
   cursor.movePosition(QTextCursor::Start);
   QTextCursor c(cursor.block().next());
   c.select(QTextCursor::BlockUnderCursor);
   c.removeSelectedText();

   c.movePosition(QTextCursor::End);
   c.insertBlock();
   c.insertText("sometext\nsometext");

   ui.textEdit->document()->documentLayout()->blockSignals(false);

The problem here is autoscrolling. No matter how many lines are in block which I've deleted and inserted, I want to scroll by one line. Autoscrolling doesn't allow me to do it. How to disable it ?

3. How can I aquire more speed from QTextEdit, QTextDocument and so on? Any hints would be greatly appreciated !

Thank you in advance !...
Записан
Steven_Orko
Гость
« Ответ #4 : Февраль 07, 2006, 19:36 »

Цитировать

> There are some problems with a QTextEdit, QTextDocument and other
> text-related classes..
>
> 1. How to make QTextDocument without first empty line? (I want my
> first inserted block be in the first line of QTextEdit, not second!)



I cannot reproduce this problem here with the following example, does it
work for you?

Код:
#include <QtGui>

int main(int argc, char **argv)
{
    QApplication a(argc, argv);
    QTextEdit d;
    QTextDocument td;
    QTextCursor tc(&td);
    tc.insertText("This is a test");
    d.setDocument(&td);
    d.show();
    return a.exec();
}


If it does then can you modify the example to reproduce your problem?
If it does not then can you tell me what platform you are on exactly
and with which version of Qt?  Thanks.

Цитировать

> 2. QTextEdit isn't fast enough to show the whole document I need (more
> than 10 mb xml), that is why I try to show it by blocks. When user
> drags custom scrollbar, I've implemented the function which create new
> QTextDocument and fill it with 100 QTextBlock from xml file. When user
> presses scrollbar arrows two another functions are called, which
> prepend or append QTextBlock to the beginning or to the end of
> QTextDocument and removes block from other side of document.
>
> For examples I do the following:
> ui.textEdit->document()->documentLayout()->blockSignals(true);
>
> QTextCursor cursor(textDocument);
> cursor.movePosition(QTextCursor::Start);
> QTextCursor c(cursor.block().next());
> c.select(QTextCursor::BlockUnderCursor);
> c.removeSelectedText();
>
> c.movePosition(QTextCursor::End);
> c.insertBlock();
> c.insertText("sometext\nsometext");
>
> ui.textEdit->document()->documentLayout()->blockSignals(false);
>
> The problem here is autoscrolling. No matter how many lines are in
> block which I've deleted and inserted, I want to scroll by one line.
> Autoscrolling doesn't allow me to do it. How to disable it ?



The problem is that you move the position of the cursor on the text
document and this causes it to scroll.  Rather than using a cursor on
the textdocument to get the first block why not just call begin() to
get the first block and then next() on that?

Цитировать

> 3. How can I aquire more speed from QTextEdit, QTextDocument and so
> on? Any hints would be greatly appreciated !



We are always optimizing QTextEdit and the richtext classes,
unfortunatly there is nothing more than I can suggest that you can do
to make it faster at all.  You may find upgrading to Qt 4.1.1 will show
some improvement when it is released in the next few weeks.



Если еще что-то, то можно продолжить переписку с ними по этому вопросу. Я не вчитывался в письмо, потому предлагаю сделать это тебе. Следующее письмо аналогично первому пиши сюда.
Записан
ElderOrb
Гость
« Ответ #5 : Февраль 08, 2006, 00:54 »

Цитировать
Если еще что-то, то можно продолжить переписку с ними по этому вопросу. Я не вчитывался в письмо, потому предлагаю сделать это тебе. Следующее письмо аналогично первому пиши сюда.


Большое спасибо!.. Конечно же я воспользуюсь представленной возможностью ж).

Цитировать

I cannot reproduce this problem here with the following example, does it
work for you?


Yes, it works for me, but if do the following
  QApplication a(argc, argv);
    QTextEdit d;
    QTextDocument td;
    QTextCursor tc(&td);

    QTextBlockFormat bf;
    QTextCharFormat cf;
    cf.setAnchor(true);
    cf.setAnchorName(QString::number(0));

    tc.insertBlock(bf, cf);
    tc.insertText("This is a test");
    d.setDocument(&td);
    d.show();
    return a.exec();

then the problem appears. (I need to use "void insertBlock ( const QTextBlockFormat & format, const QTextCharFormat & charFormat )"
because I need anchors. "void insertText ( const QString & text, const QTextCharFormat & format )" allows me insert text with anchors, but there is no such function for images and tables). It seems like just created QTextDocument is already have one empty QTextBlock, and I can't delete it.. Of course I can check if I insert first element (text, table, image) into QTextDocument and do not call insertBlock in this case... But maybe there is a way to create QTextDocument without first empty block ?

Цитировать

The problem is that you move the position of the cursor on the text
document and this causes it to scroll. Rather than using a cursor on
the textdocument to get the first block why not just call begin() to
get the first block and then next() on that?


Maybe it would solve my problem, but the it seems that QTextDocument::end() returns not the last block of document..

QApplication a(argc, argv);
    QTextEdit d;
    QTextDocument td;
    QTextCursor tc(td.begin());

   QTextBlockFormat bf;
    QTextCharFormat cf;
    cf.setAnchor(true);
    cf.setAnchorName(QString::number(0));

    tc.insertBlock(bf, cf);
    tc.insertText("This is a test");

   QTextCursor appendCursor(td.end());
   appendCursor.insertBlock();
   appendCursor.insertText("test");

    d.setDocument(&td);
    d.show();
    return a.exec();

In QTextEdit I see:

test
This is a test
Записан
Steven_Orko
Гость
« Ответ #6 : Февраль 08, 2006, 18:07 »

Цитировать
> Yes, it works for me, but if do the following
> QApplication a(argc, argv);
> QTextEdit d;
> QTextDocument td;
> QTextCursor tc(&td);

> QTextBlockFormat bf;
> QTextCharFormat cf;
> cf.setAnchor(true);
> cf.setAnchorName(QString::number(0));

> tc.insertBlock(bf, cf);
> tc.insertText("This is a test");
> d.setDocument(&td);
> d.show();
> return a.exec();

> then the problem appears. (I need to use "void insertBlock ( const
> QTextBlockFormat & format, const QTextCharFormat & charFormat )"
> because I need anchors. "void insertText ( const QString & text, const
> QTextCharFormat & format )" allows me insert text with anchors, but
> there is no such function for images and tables). It seems like just
> created QTextDocument is already have one empty QTextBlock, and I
> can't delete it.. Of course I can check if I insert first element
> (text, table, image) into QTextDocument and do not call insertBlock in
> this case... But maybe there is a way to create QTextDocument without
> first empty block ?


This is because the first block is the one that you inserted with the
insertBlock() call, then the second block is inserted with the
insertText() call.  Since you want that text to have the anchor, then
you should simply do:

       
Код:
 tc.insertText("This is a test", cf);


instead of the insertBlock().  Then only one block will be created and
it will have the format you want.  See:

        http://doc.trolltech.com/4.1/qtextcursor.html#insertText-2

for more information.


Цитировать
> Maybe it would solve my problem, but the it seems that
> QTextDocument::end() returns not the last block of document..

 
Код:
QApplication a(argc, argv);
> QTextEdit d;
> QTextDocument td;
> QTextCursor tc(td.begin());

> QTextBlockFormat bf;
> QTextCharFormat cf;
> cf.setAnchor(true);
> cf.setAnchorName(QString::number(0));

> tc.insertBlock(bf, cf);
> tc.insertText("This is a test");

> QTextCursor appendCursor(td.end());
> appendCursor.insertBlock();
> appendCursor.insertText("test");

> d.setDocument(&td);
> d.show();
> return a.exec();


> In QTextEdit I see:

> test
> This is a test


In this case you can simply move the QTextCursor to the end of the
document using movePosition() like this:

       
Код:
appendCursor.movePosition(QTextCursor::End);


and everything will be inserted after the text.

I hope this helps and have a nice day!
Записан
ElderOrb
Гость
« Ответ #7 : Февраль 08, 2006, 22:17 »

Цитировать
This is because the first block is the one that you inserted with the
insertBlock() call, then the second block is inserted with the
insertText() call. Since you want that text to have the anchor, then
you should simply do:

 tc.insertText("This is a test", cf);


I want not only text, but tables and image with anchors.. If I use insertBlock first, and then insert a data (text, image, table) into created block then I can get anchors for all types of data. But what I must do for images and tables if I want them with anchors and can't use insertBlock ?

Цитировать
In this case you can simply move the QTextCursor to the end of the
document using movePosition() like this:
appendCursor.movePosition(QTextCursor::End);


I've tried it already, but it causes QTextEdit to autoscroll inserted text, and I want to scroll only when I need it. (I try to implement dynamic loading of new blocks when user drags my scrollBar. For example there was 50 blocks in QTextDocument, attached to QTextEdit, each block has some data of differenet type: text, image or table. When user draggs scrollbar, QTextEdit scrolls QTextDocument by fixed value of pixels and this event also serves as a signal to append some new blocks to the beginning or to the end of document. So from user's side it must be like scrolling of one huge document). Is it possible at all to achieve such behavior of QTextEdit ?

p.s. 2Steven Orko: Ещё раз если можно ж)

p.s. Жаль что trolltech-и упорно отказываются меня понимать. Наверное пора учить албанский раз с английским совсем плохо.. ж(
Записан
Steven_Orko
Гость
« Ответ #8 : Февраль 09, 2006, 18:56 »

Цитировать
I want not only text, but tables and image with anchors.. If I use
insertBlock first, and then insert a data (text, image, table) into
created block then I can get anchors for all types of data. But what I
must do for images and tables if I want them with anchors and can't
use insertBlock ?


You can still use insertBlock(), just ensure that you create it with the
relevant format.  In the case of an image you can use insertImage() and
set the anchor name on the image format.  Same with insertTable() you
can set the Anchor property to have the value you want.  Simply look at
the documentation for QTextFormat and its subclasses for more
information.

        http://doc.trolltech.com/4.1/qtextformat.html


Цитировать
I've tried it already, but it causes QTextEdit to autoscroll inserted
text, and I want to scroll only when I need it. (I try to implement
dynamic loading of new blocks when user drags my scrollBar. For
example there was 50 blocks in QTextDocument, attached to QTextEdit,
each block has some data of differenet type: text, image or table.
When user draggs scrollbar, QTextEdit scrolls QTextDocument by fixed
value of pixels and this event also serves as a signal to append some
new blocks to the beginning or to the end of document. So from user's
side it must be like scrolling of one huge document). Is it possible
at all to achieve such behavior of QTextEdit ?


What you can try is to subclass QTextEdit and reimplement
scrollContentsBy() to simply do not do anything when you are inserting
into the document.  This should enable you to control when it scrolls.
See:

        http://doc.trolltech.com/4.1/qabstractscrollarea.html#scrollContentsBy

for more information.
Записан
Страниц: [1]   Вверх
  Печать  
 
Перейти в:  


Страница сгенерирована за 0.107 секунд. Запросов: 21.