Simulate mouse click inside QTextEdit in Qt?

1k views Asked by At

I am trying to simulate a mouse click inside a QTextEdit in Qt as my application does not have any mouse or keyboard. This is an Embedded hardware board.

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

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


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

       QCursor::setPos((txt->pos()+=QPoint(10,10)));                                                                                                                  
       QMouseEvent * event1 = new QMouseEvent ((QEvent::MouseButtonPress), QPoint(10,10),
        Qt::LeftButton,
        Qt::LeftButton,
        Qt::NoModifier);

        qApp->postEvent((QObject*)txt,(QEvent *)event1);

        QMouseEvent * event2 = new QMouseEvent ((QEvent::MouseButtonRelease), QPoint(10,10),
        Qt::LeftButton,
        Qt::LeftButton,
        Qt::NoModifier);

        qApp->postEvent((QObject*)txt,(QEvent *)event2);


        txt->show();

        return a.exec();
}

When I run the application, I just see a Cursor on my textedit box.I want the cursor to be clicked / at-least cursor should be blinking inside textedit widget.

Thank you.

Edit :

Image is attached Example

1

There are 1 answers

3
Alexander V On

Moving mouse pointer is not necessary (though you know why you need that) but for setting the cursor inside text edit:

editWidget->activateWindow(); // some cases require
QFocusEvent* eventFocus = new QFocusEvent(QEvent::FocusIn);
qApp->postEvent(editWidget, (QEvent*)eventFocus);

And your editWidget is 'txt'.