PDA

View Full Version : Simulate mouse click inside QTextEdit in Qt?



vk41286
10th June 2015, 09:23
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.

wysota
10th June 2015, 09:56
I am trying to simulate a mouse click inside a QTextEdit in Qt

Are you sure this is really what you want? Maybe instead you want the widget to gain focus or maybe you want to position the text cursor in a given position? Which is it then?

vk41286
10th June 2015, 13:11
11199

Thank you. Please check the image attached.
Actually I want the cursor to blink inside the text edit box. But its not happening. I do not have mouse or keyboard attached.

Just a cursor should blink after Text 1.

Thank you once again !

Even the below code does not work. There is no Cursor at the start or end of QTextEdit box.

Please help me. Thank you


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

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

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();
}

wysota
10th June 2015, 14:49
Actually I want the cursor to blink inside the text edit box.

You want the cursor to blink or you want the widget to accept key events? The cursor blinks when the widget has focus. Then you can post key events to it either artificially or by properly implementing an input method handler that will use a virtual keyboard.

vk41286
10th June 2015, 19:11
All I want is the Cursor to blink in my widget (QTextEdit).

Thank you for helping. Waiting for your code :D

wysota
10th June 2015, 23:38
All I want is the Cursor to blink in my widget (QTextEdit).
Well... In that case subclass the widget, start a timer and draw or not draw the cursor during paintEvent. cursorRect() will tell you where to draw.

vk41286
11th June 2015, 07:24
Could you please provide me some sample code. I am completely new to QT. I have 4 days of exp in QT :D

wysota
11th June 2015, 08:03
Could you please provide me some sample code. I am completely new to QT. I have 4 days of exp in QT :D

There are well over 300 examples bundled with Qt. Many of them tell you how to draw a rectangle. I don't want to give you ready code because in my opinion what you are trying to do is wrong and is not what you are supposed to do so I don't want to encourage you to go the wrong way. If you really want to burn your fingers, you will have to make some effort to do it.