3

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.

vk41286
  • 113
  • 1
  • 1
  • 10
  • 1
    You seem to be missing at least `return a.exec()` at the end of your main() – Oleg Pyzhcov May 22 '15 at 07:31
  • I have added return a.exec() but forgot to paste it while copying here. I have edited my code. Thanks – vk41286 May 22 '15 at 07:35
  • 1
    The documentation for http://doc.qt.io/qt-4.8/qwidget.html#setFocus say it only sets focus if the widget is in the active window. Have you tried this method to make sure the QTextEdit is the active window? http://doc.qt.io/qt-4.8/qwidget.html#activateWindow – Bradley T. Hughes May 22 '15 at 07:43
  • @Bradley T. Hughes - I tried it. But never worked me. I have attached for reference ! – vk41286 May 22 '15 at 08:18

1 Answers1

2

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