Show cursor on QTextEdit in QT4.8.5

I am a newbie working on QT. Basically I am creating a QTextEdit window in QT and I want the cursor to be displayed in its original position. enter image description here

My simple code:

    #include "mainwindow.h"
    #include <QApplication>
    #include <QLabel>
    #include <QFont>
    #include <QtGui>
    #include <QPixmap>
    #include <QTextEdit>
    #include <QTextCursor>
    #include <QLineEdit>

    int main(int argc, char *argv[])
    {

        QApplication a(argc, argv);
        MainWindow w;
        w.setWindowFlags(Qt::Window | Qt::FramelessWindowHint);
        w.setStyleSheet("background-color: yellow;");
        w.show();

        QTextEdit *txt = new QTextEdit();
        txt->setText("Text 1");
        txt->setWindowFlags(Qt::Window | Qt::FramelessWindowHint);
        txt->setFocus();
        txt->setStyleSheet("background-color: rgba(255, 255, 255,      200);");
        txt->setGeometry(10,20,100,30);
        txt->show();
        return a.exec();
}

      

This creates a simple text box in the w window.

I am not using a mouse or keyboard because this is for an embedded hardware board.

But after displaying the text, the cursor should be displayed.

I have tried various methods to display the cursor on the QTextEdit, for example:

QTextCursor cursor;
QTextEdit *editor = new QTextEdit();

QTextCursor cursor(editor->textCursor());
cursor.movePosition(QTextCursor::Start);
cursor.setPosition(5);
cursor.setPosition(9, QTextCursor::KeepAnchor);

txt->moveCursor(QTextCursor::End,QTextCursor::MoveAnchor);
txt->setCursorWidth(20);
txt->setTextCursor(cursor);

      

But none of the methods display the cursor. I've done through most of the posts on SO.

Can anyone please help? Thank you very much.

PS: So far no solutions have been found on the QT forums.

+3


source to share


1 answer


You should pass the basic text editor document, i.e. txt->document()

, into the constructor QTextCursor

before you can use QTextCursor

to do anything on QTextEdit

. I guess it makes QTextCursor

seeing it as a document. Then you use QTextCursor

to insert text into QTextEdit

and also position the cursor wherever you want with beginEditBlock()

after pasting text or movePosition(QTextCursor::End)

.

#include <QLabel>
#include <QFont>
#include <QtGui>
#include <QPixmap>
#include <QTextEdit>
#include <QTextCursor>
#include <QLineEdit>

int main( int argc, char **argv ) {
    QApplication app( argc, argv );

    MainWindow w;
    w.setWindowFlags(Qt::Window | Qt::FramelessWindowHint);
    w.setStyleSheet("background-color: yellow;");


    QTextEdit *txt = new QTextEdit();
    txt->setWindowFlags(Qt::Window | Qt::FramelessWindowHint);
    txt->setFocus();
    txt->setStyleSheet("background-color: rgba(255, 255, 255,      200);");
    txt->setGeometry(10,20,100,30);


    QTextCursor cursor = QTextCursor(txt->document());
    cursor.insertText("Text 1");
    cursor.beginEditBlock();
    // OR
    //In your case, either methods below will work since the text has been inserted already
    //cursor.movePosition(QTextCursor::End);
    //cursor.movePosition(QTextCursor::Start);

    txt->show();

    return app.exec();
  }

      



enter image description here

+2


source







All Articles