Results 1 to 4 of 4

Thread: Need idea how to implement my own SetFormat function

  1. #1

    Default Need idea how to implement my own SetFormat function

    Hi,
    i would like to ask you for help. I'm creating simple editor working with large texts. I want to have there simple formating possibility like change color of text. I'm using QPlainTextEdit (because it's faster then QTextEdit) as main input widget and it should have simple possibility to change color of existing text. Function should look like this:
    Qt Code:
    1. //position - char's position in text where we want to start with formatting
    2. //length - to how many characters we want to apply format
    3. //format - charformat which we want to apply to characters from position to position+length
    4. SetFormat(int position,int length,QTextCharFormat &format) {
    5. //implementation
    6. }
    To copy to clipboard, switch view to plain text mode 

    If user push some button, i'll call this function and pass there parameters to make already written text colorful or something. This function already exists in QSyntaxHighlighter, but there is lot of limitation which are not acceptable for me.
    I have tried something, but it's crashing (i have asked already in this forum why it's crashing,but i can't get there for replies, because there is some error in link or something):

    Qt Code:
    1. void MainWindow::SetFormat(int position,int length,QTextCharFormat &format) {
    2. QTextCursor *curA=new QTextCursor(this->edit->document());//this->edit is QTextPlainEdit class
    3. curA->setPosition(position,QTextCursor::MoveAnchor);
    4. curA->setPosition(length,QTextCursor::KeepAnchor);
    5. curA->beginEditBlock();
    6. if (curA->hasSelection()) {
    7. curA->mergeCharFormat(format);
    8. curA->clearSelection();
    9. }
    10. curA->endEditBlock();
    11. }
    To copy to clipboard, switch view to plain text mode 

    Hope, there are not many mistakes in my first english text

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,376
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: Need idea how to implement my own SetFormat function

    Make sure "edit" and "edit->document()" return valid values (not null and properly initialized with valid objects). Also don't allocate the cursor on heap, currently you have a memory leak. You can safely allocate the cursor on stack.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Wiki edits
    17

    Default Re: Need idea how to implement my own SetFormat function

    Your original thread is quite accessible to me:
    http://www.qtcentre.org/threads/3845...-..-..-src-gui

    As far I can tell, a QPlainTextEdit and its simplified layout engine doesn't support anything except unformatted plain text and behaves poorly when you try to merge a format into it.

  4. #4

    Default Re: Need idea how to implement my own SetFormat function

    OK, so i little changed my original code as you adviced so now i have
    Qt Code:
    1. MainWindow::MainWindow(QWidget *parent)
    2. : QMainWindow(parent)
    3. {
    4. this->edit=new QPlainTextEdit(this);
    5. this->setCentralWidget(this->edit);
    6. connect(edit->document(),SIGNAL(contentsChange(int,int,int)),this,SLOT(contentsChange(int,int,int)));
    7. }
    8.  
    9. void MainWindow::SetFormat(int position,int length,QTextCharFormat &format) {
    10. if (this->edit->document()!=NULL) {
    11. QTextCursor curA(this->edit->document());
    12. curA.setPosition(position,QTextCursor::MoveAnchor);
    13. curA.setPosition(length,QTextCursor::KeepAnchor);
    14. curA.beginEditBlock();
    15. if (curA.hasSelection()) {
    16. curA.mergeCharFormat(format);
    17. curA.clearSelection();
    18. }
    19. curA.endEditBlock();
    20. } else {
    21. qDebug("qplaintextedit->document() is null.");
    22. }
    23. }
    24.  
    25. void MainWindow::contentsChange(int position,int charsRemoved,int charsAdded) {
    26. if (this->edit->toPlainText().length()>10) {
    27. QTextCharFormat charFormat;
    28. charFormat.setFontItalic(true);
    29. this->SetFormat(0,3,charFormat);
    30. }
    31. }
    To copy to clipboard, switch view to plain text mode 

    But app is still crashing with
    Qt Code:
    1. ASSERT: "n" in file ..\..\include\QtGui/private/../../../src/gui/text/qfragmentmap_p.h, line 291
    To copy to clipboard, switch view to plain text mode 

    I think ChrisW67 is true about simple layout text engine, but if u use QSyntaxHighlighter with QPlainTextEdit, then u can use colored text, so i think there has to be way how to do it.

    P.S. I would like to delete my previous thread, but can't. Thread is accessible via link in prev.post, but not via threads list using chrome hmmm.
    Attached Files Attached Files

Similar Threads

  1. A Hippie Idea
    By qtoptus in forum Qt Programming
    Replies: 9
    Last Post: 17th December 2010, 23:28
  2. ???any one have idea about this ???
    By damodharan in forum Qt Programming
    Replies: 1
    Last Post: 29th May 2010, 09:08
  3. No idea about QGraphicsView
    By Shawn in forum Qt Programming
    Replies: 2
    Last Post: 27th June 2007, 10:02
  4. UI Idea please
    By munna in forum General Discussion
    Replies: 1
    Last Post: 9th May 2006, 11:14

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.