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

  • SEARCH
  • Home
  • 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 4125834
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T23:55:17+00:00 2026-05-20T23:55:17+00:00

there! I want to find out how to change current line format in QTextEdit?

  • 0

there! I want to find out how to change current line format in QTextEdit?

In the document I read that

“Formatting can be applied to the
current text document using the
setCharFormat(), mergeCharFormat(),
setBlockFormat() and
mergeBlockFormat() functions. If the
cursor has no selection, current block
format will be changed.”

But in my application, the current block in which cursor is couldn’t be changed. May I miss something? Then how could I change current block format which has no selection?

Here is my code:

QTextCursor cursor = this->textCursor();
QTextBlockFormat blockFmt;
blockFmt.setNonBreakableLines(true);
blockFmt.setPageBreakPolicy(QTextFormat::PageBreak_AlwaysBefore);
QTextCharFormat charFmt;
charFmt.setFont(data->visualFont());
if(!cursor.hasSelection()) {
    cursor.beginEditBlock();
    cursor.setBlockFormat(blockFmt);
    cursor.mergeBlockCharFormat(charFmt);
    QTextBlock block = cursor.block();
    block.setUserData(data);
    cursor.endEditBlock();
}

What I want to do is: change current line’s format if there is no selection. So if cursor.hasSelection() is false, I just merge new format to block chars. But this does not work.

I also tried add setTextCorsor(cursor); after cursor.endEditBlock();, but it still doesn’t work. In fact, after adding this, the whole block becomes invisible.

So how could I change current block format which has no selection?

  • 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-05-20T23:55:18+00:00Added an answer on May 20, 2026 at 11:55 pm

    Pls, check if an example below would work for you, it should change the current text block format and font.

    QTextCursor cursor(myTextEdit->textCursor());
    
    // change block format (will set the yellow background)
    QTextBlockFormat blockFormat = cursor.blockFormat();
    blockFormat.setBackground(QColor("yellow"));
    blockFormat.setNonBreakableLines(true);
    blockFormat.setPageBreakPolicy(QTextFormat::PageBreak_AlwaysBefore);
    cursor.setBlockFormat(blockFormat);
    
    // change font for current block's fragments
    for (QTextBlock::iterator it = cursor.block().begin(); !(it.atEnd()); ++it)
    {
        QTextCharFormat charFormat = it.fragment().charFormat();
        charFormat.setFont(QFont("Times", 15, QFont::Bold));
    
        QTextCursor tempCursor = cursor;
        tempCursor.setPosition(it.fragment().position());
        tempCursor.setPosition(it.fragment().position() + it.fragment().length(), QTextCursor::KeepAnchor);
        tempCursor.setCharFormat(charFormat);
    }
    

    hope this helps, regards

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

Sidebar

Related Questions

I WANT to find out if a there is a way to check if
I just want to find out if there is any way to block a
Have got an NSString *str = @12345.6789 and want to find out if there
I want to find path in 2d platform game, teeworlds . The player there
i want to split the searchrequest into parts, if there's nothing to find. example:
How can I make layout almost fullscreen ? I mean I dont want there
There is a command I want to call that returns a string, I want
I was wondering if there is a way to find out the size of
Change an item in the navigation? Sure I can do that in 15 minutes.
Currently I have a problem - I want to switch out of current frame

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.