Display a cursor on QTextEdit in QT4.8.5

1.5k Views Asked by At

I am newbie working on QT. Basically I am creating a QTextEdit box in QT and I want the Cursor to display at initial position. enter image description here

My Simple Code is :

    #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 on a window w.

I am not using mouse or a keyboard because it for a embedded Hardware board.

But after displaying the text the cursor should be displayed.

I am tried various method to get cursor displayed on QTextEdit like :

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 method displays cursor. I have done through most of the posts in SO.

Can anyone help? Thank you very much.

P.S : No solutions obtained in QT forums till now.

1

There are 1 best solutions below

7
Kay Kay On

You should pass the underlying document of the text editor i.e. txt->document() to the constructor of QTextCursor before you can use QTextCursor to do anything on QTextEdit. I guess it makes QTextCursor see it as a document. Then you use QTextCursor to insert the text into QTextEdit and also position the cursor where you want by using beginEditBlock() after inserting the 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