Results 1 to 3 of 3

Thread: Textedit - can't reset charFormat after highlight

  1. #1
    Join Date
    Jun 2020
    Posts
    2
    Qt products
    Qt5
    Platforms
    Windows

    Default Textedit - can't reset charFormat after highlight

    I'm trying to implement a shared text editor using c++ and qt. I would like to highlight the text inserted from different users with different colors. I can do that but the problem is that than I can't reset the charFormat and all the subsequent text I wrote is hihghlighted

    Code I use to highlight and change charFormat
    Qt Code:
    1. fmt.setBackground(Qt::yellow);
    2. insertCursor.setPosition(std::stoi(pos));
    3. insertCursor.insertText(character.c_str(), fmt);
    4. fmt.setBackground(Qt::white);
    5. insertCursor.setCharFormat(fmt);
    To copy to clipboard, switch view to plain text mode 

    some images to explain better
    same file opened in 2 clients
    Annotazione 2020-06-17 133258.jpg
    left client wrote and right client updated text with highlight
    Annotazione 2020-06-17 133259.jpg
    right client wrote but remain highlighted
    Annotazione 2020-06-17 133260.jpg

    I also tried to use 2 different cursors one with the previous charFormat and one with one highlight settings and

    moving the cursor locking the anchor this way
    Qt Code:
    1. fmt.setBackground(Qt::yellow);
    2.  
    3. QTextCursor cursor(ui->textEdit->document());
    4. cursor.setPosition(begin, QTextCursor::MoveAnchor);
    5. cursor.setPosition(end, QTextCursor::KeepAnchor);
    6. cursor.setCharFormat(fmt);
    7. fmt.setBackground(Qt::white);
    8. cursor.setCharFormat(fmt);
    To copy to clipboard, switch view to plain text mode 
    (this is a function so I pass end and begin as values)

    I suppose the problem could be related to the anchor that doesn't follow anymore the cursor or the fact the cursor use the previous character charFormat when I type but I really can't find out how to solve this problem

    thanks to everyone in advice

  2. #2
    Join Date
    Jun 2020
    Posts
    2
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Textedit - can't reset charFormat after highlight

    There's anyone that can help me?

    I can add a video to explain better the problem and maybe reword the question





    How can I change properly the charFormat of my text edit runtime?
    And which is the best way to highlight a text?

    also I modified the code a bit

    Insert function:
    Qt Code:
    1. insertCursor.setPosition(std::stoi(pos));
    2. ui->textEdit->setTextCursor(insertCursor);
    3. ui->textEdit->insertPlainText(character.c_str());
    4. HighlightText(user, std::stoi(pos), std::stoi(pos) + 1);
    5.  
    6. fmt.setBackground(Qt::white);
    7. insertCursor.setCharFormat(fmt);
    8. insertCursor.setPosition(std::stoi(pos) + 1, QTextCursor::MoveAnchor);
    To copy to clipboard, switch view to plain text mode 

    HighlightText function
    Qt Code:
    1. void MainWindow::HighlightText(std::string user, int begin, int end){
    2. fmt.setBackground(Qt::yellow);
    3.  
    4. QTextCursor cursor(ui->textEdit->document());
    5. cursor.setPosition(begin, QTextCursor::MoveAnchor);
    6. cursor.setPosition(end, QTextCursor::KeepAnchor);
    7. cursor.setCharFormat(fmt);
    8.  
    9. }
    To copy to clipboard, switch view to plain text mode 

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

    Default Re: Textedit - can't reset charFormat after highlight

    I generated a basic QMainWindow project with aQTextEdit as the central widget. This code caches the formats and generates a stream of foreign text that switches from plain to highlighted and back without issue. run it an type continously for a few seconds. Useful?
    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3.  
    4. #include <QTimer>
    5.  
    6. MainWindow::MainWindow(QWidget *parent)
    7. : QMainWindow(parent)
    8. , ui(new Ui::MainWindow)
    9. , m_timer(nullptr)
    10. {
    11. ui->setupUi(this);
    12.  
    13. m_base = ui->textEdit->textCursor().charFormat();
    14. m_highlighted = m_base;
    15. m_highlighted.setBackground(Qt::yellow);
    16.  
    17. m_timer = new QTimer(this);
    18. connect(m_timer, &QTimer::timeout, this, &MainWindow::writeForeignText);
    19. m_timer->setInterval(5000);
    20. m_timer->start();
    21. }
    22.  
    23. MainWindow::~MainWindow()
    24. {
    25. delete ui;
    26. }
    27.  
    28. void MainWindow::writeForeignText()
    29. {
    30. QTextCursor cursor = ui->textEdit->textCursor();
    31. cursor.setCharFormat(m_highlighted);
    32. cursor.insertText("Plugh");
    33. cursor.setCharFormat(m_base);
    34. ui->textEdit->setTextCursor(cursor);
    35. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by ChrisW67; 27th June 2020 at 07:17.

Similar Threads

  1. Highlight current line in textedit
    By aaditya190 in forum Newbie
    Replies: 2
    Last Post: 22nd November 2013, 16:30
  2. CharFormat how set backgroud color ??
    By Pablik in forum Qt Programming
    Replies: 6
    Last Post: 25th July 2012, 07:15
  3. How to get CharFormat from each char
    By Pablik in forum Qt Programming
    Replies: 2
    Last Post: 16th July 2012, 22:07
  4. Replies: 1
    Last Post: 9th February 2010, 12:11

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.