Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8306485
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T18:20:27+00:00 2026-06-08T18:20:27+00:00

I have a problem: my project is a very simple one with a QTextEdit

  • 0

I have a problem: my project is a very simple one with a QTextEdit and a QSyntaxHighlighter, I’m trying to load a .cpp file and highlighting just the eighth line of that file, but the QTextEdit can’t load the entire file if I ask it to highlight the line.

The following image shows the problem:

enter image description here

The relevant code of the application is the following:

void MainWindow::openFile(const QString &path)
{
    QString fileName = path;

    if (fileName.isNull())
        fileName = QFileDialog::getOpenFileName(this,
            tr("Open File"), "", "C++ Files (*.cpp *.h)");

    if (!fileName.isEmpty()) {
        QFile file(fileName);
        if (file.open(QFile::ReadOnly | QFile::Text))
            editor->setPlainText(file.readAll());

        QVector<quint32> test;
        test.append(8); // I want the eighth line to be highlighted
        editor->highlightLines(test);
    }
}

and

#include "texteditwidget.h"

TextEditWidget::TextEditWidget(QWidget *parent) :
    QTextEdit(parent)
{
    setAcceptRichText(false);
    setLineWrapMode(QTextEdit::NoWrap);

}



// Called to highlight lines of code
void TextEditWidget::highlightLines(QVector<quint32> linesNumbers)
{

    // Highlight just the first element
    this->setFocus();
    QTextCursor cursor = this->textCursor();
    cursor.setPosition(0);
    cursor.movePosition(QTextCursor::Down, QTextCursor::MoveAnchor, linesNumbers[0]);
    this->setTextCursor(cursor);
    QTextBlock block = document()->findBlockByNumber(linesNumbers[0]);
    QTextBlockFormat blkfmt = block.blockFormat();
    // Select it
    blkfmt.setBackground(Qt::yellow);
    this->textCursor().mergeBlockFormat(blkfmt);
}

However if you want to test the project with the cpp file I used (in the directory FileToOpen\diagramwidget.cpp), here’s the complete source

http://idsg01.altervista.org/QTextEditProblem.zip

I’ve been trying to solve this for a lot of time and I’m starting to wonder if this isn’t a bug or something similar

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T18:20:31+00:00Added an answer on June 8, 2026 at 6:20 pm

    It seems that the QTextEdit control needs time after each loading, setting a QApplication:processEvents(); after setPlainText() solves the problem although it’s not an elegant solution.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a problem building a fairly simple local CGO project. Consider this very
i have a problem to distributing application with 2 project (one project with sub-project
I have a very simple .NET MVC3 project set up using jquery mobile for
I am trying to do something very simple. I have a com.mypackage.Logger logger class
This problem is very simple i think... I dont have experience with big scalable
I've run into a problem with my very simple Ramaze project. My project is
I have a problem with a simple included file. The file being included is
I have made a very simple NSIS plugin that has one function in it.
i have a problem on a project I'm working on. I have to create
I working on project and have problem with threading and update of UI. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.