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 8028271
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T00:05:26+00:00 2026-06-05T00:05:26+00:00

I want to indent text of a QPlainTextEdit whan a menu button is pressed.

  • 0

I want to indent text of a QPlainTextEdit whan a menu button is pressed. When the button is pressed I ask if there is a selection if not I just indent the current line, if there is I want to indent all the lines in the selection. Right now the code works for the single line, but when indenting a selection is like the last part of the line disappears. For example, if I have the line: "Artificial Intelligence stands no chance against Natural Stupidity.", after the indentation it’s just: " Artificial Intelligence stands no chance against Natural Stupidi and after that if I start writing in that line the text starts to disappear when it reaches what now is the end of the sentence. Also, the program crashes if I click or put the cursor in that line after the part of the sentence that disappear.

The code:

void MainWindow::on_action_Indent_triggered()
{
    Document* doc = dynamic_cast<Document*>(ui->tabsManager->currentWidget());
    QTextCursor cursor = doc->textArea->textCursor();
    cursor.beginEditBlock();

    // If ther is no text selected...
    if (cursor.selection().isEmpty()) {
        cursor.movePosition(QTextCursor::StartOfLine);
        cursor.insertText(this->tabLength);
    } else { // If the selection is not empty...
        cursor.beginEditBlock();

        // Save selection start and end
        int start = cursor.selectionStart();
        int end = cursor.selectionEnd();
        cursor.clearSelection();

        // Set end to the end of line of the selected line
        cursor.setPosition(end);
        cursor.movePosition(QTextCursor::EndOfLine);
        end = cursor.position();

        // Set cursor to the start of the first selected line
        cursor.setPosition(start);
        cursor.movePosition(QTextCursor::StartOfLine);
        start = cursor.position();

        // While still in the selection, add "    " to the start of each line
        do {
            cursor.movePosition(QTextCursor::StartOfLine);
            cursor.insertText(this->tabLength);
            end += this->tabLength.count();
            cursor.movePosition(QTextCursor::EndOfLine);
        } while (cursor.position() < end && cursor.movePosition(QTextCursor::Down));

        // Select the changed areatabLenght
        cursor.clearSelection();
        cursor.setPosition(start);
        while (cursor.position() < end)
            cursor.movePosition(QTextCursor::NextCharacter, QTextCursor::KeepAnchor);
    }
    // Set the cursor in the GUI
    doc->textArea->setTextCursor(cursor);
    cursor.endEditBlock();
}

Document is a Class and textArea is a QTextPlainEdit. this->tabLength is a QString with a value of ” “

  • 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-05T00:05:28+00:00Added an answer on June 5, 2026 at 12:05 am

    The problem is quite simple: you’re calling beginEditBlock() more times than you call endEditBlock(). Remove the beginEditBlock() call right after } else {. I could reproduce this problem, and matching [begin|end]EditBlock() calls indeed fixes it.

    Below is a self-contained example.

    # indenttest.pro
    CONFIG += qt gui
    SOURCES += indenttest.cpp
    
    // indenttest.cpp
    #include <cmath>
    #include <QWidget>
    #include <QVBoxLayout>
    #include <QPlainTextEdit>
    #include <QPushButton>
    #include <QTextCursor>
    #include <QTextDocumentFragment>
    #include <QApplication>
    
    //
    
    class QPlainTextEdit;
    class MainWindow : public QWidget
    {
        Q_OBJECT
    public:
        explicit MainWindow(QWidget *parent = 0);
    
    public slots:
        void on_indent();
    
    private:
        const QString tabLength;
        QPlainTextEdit * textArea;
    };
    
    //
    
    MainWindow::MainWindow(QWidget *parent) :
        QWidget(parent),
        tabLength("    ")
    {
        QVBoxLayout * layout = new QVBoxLayout(this);
        QPushButton * btn = new QPushButton("Indent", this);
        layout->addWidget(btn);
        textArea = new QPlainTextEdit(this);
        textArea->setPlainText("foo\nbar\nbaz\nbat");
        layout->addWidget(textArea);
        connect(btn, SIGNAL(clicked()), SLOT(on_indent()));
    
    }
    
    void MainWindow::on_indent()
    {
        QTextCursor cursor = textArea->textCursor();
        cursor.beginEditBlock();
    
        // If ther is no text selected...
        if (cursor.selection().isEmpty()) {
            cursor.movePosition(QTextCursor::StartOfLine);
            cursor.insertText(this->tabLength);
        } else { // If the selection is not empty...
            //cursor.beginEditBlock();
    
            // Save selection start and end
            int start = cursor.selectionStart();
            int end = cursor.selectionEnd();
            //cursor.clearSelection();
    
            // Set end to the end of line of the selected line
            cursor.setPosition(end);
            cursor.movePosition(QTextCursor::EndOfLine);
            end = cursor.position();
    
            // Set cursor to the start of the first selected line
            cursor.setPosition(start);
            cursor.movePosition(QTextCursor::StartOfLine);
            start = cursor.position();
    
            // While still in the selection, add "    " to the start of each line
            do {
                cursor.movePosition(QTextCursor::StartOfLine);
                cursor.insertText(this->tabLength);
                end += this->tabLength.count();
                cursor.movePosition(QTextCursor::EndOfLine);
            } while (cursor.position() < end && cursor.movePosition(QTextCursor::Down));
    
            // Select the changed areatabLenght
            cursor.clearSelection();
            cursor.setPosition(start);
            while (cursor.position() < end)
                cursor.movePosition(QTextCursor::NextCharacter, QTextCursor::KeepAnchor);
        }
        // Set the cursor in the GUI
        textArea->setTextCursor(cursor);
        cursor.endEditBlock();
    }
    
    int main(int argc, char** argv)
    {
        QApplication a(argc, argv);
        MainWindow w;
        w.show();
        return a.exec();
    }
    
    #include "indenttest.moc"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to indent multi-line in 'vim/gvim', is there any shortcut in the vim/gvim?
I want to indent the selected text in a <textarea> by 4 spaces, just
a { float: left; width: 32px; height: 32px; text-align: left; text-indent:-9999px; background: url('../img/button.png') no-repeat
I have a text block made whith sIFR and I want to indent the
I have a div and I want to indent my text, say 40px from
I want to use a technique like text-indent:-9999em or negative margin to replace my
Edit: I'm not an Html pro. I want to add this text : AIM
1) I want to auto wrap a text by words so that each line
I want to indent my text in the article I am writing in docbook
I am using the text-indent technique to replace my <h1/> tag with my website's

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.