Results 1 to 3 of 3

Thread: Textedit - can't reset charFormat after highlight

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #3
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 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
  •  
Qt is a trademark of The Qt Company.