Results 1 to 8 of 8

Thread: Simulate mouse click inside QTextEdit in Qt?

  1. #1
    Join Date
    May 2015
    Posts
    5
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Android

    Default Simulate mouse click inside QTextEdit in Qt?

    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.

    Qt Code:
    1. int main(int argc, char *argv[])
    2. {
    3.  
    4. QApplication a(argc, argv);
    5. MainWindow w;
    6. w.setWindowFlags(Qt::Window | Qt::FramelessWindowHint);
    7. w.setStyleSheet("background-color: Black;");
    8. w.startcomthread();
    9. w.show();
    10.  
    11.  
    12. QTextEdit *txt = new QTextEdit();
    13. txt->setWindowFlags(Qt::Window | Qt::FramelessWindowHint);
    14. txt->setFocus();
    15. txt->setStyleSheet("background-color: rgb(255, 255, 255,200);");
    16. txt->setGeometry(10,20,100,30);
    17. txt->setText("Text 1");
    18.  
    19. QCursor::setPos((txt->pos()+=QPoint(10,10)));
    20. QMouseEvent * event1 = new QMouseEvent ((QEvent::MouseButtonPress), QPoint(10,10),
    21. Qt::LeftButton,
    22. Qt::LeftButton,
    23. Qt::NoModifier);
    24.  
    25. qApp->postEvent((QObject*)txt,(QEvent *)event1);
    26.  
    27. QMouseEvent * event2 = new QMouseEvent ((QEvent::MouseButtonRelease), QPoint(10,10),
    28. Qt::LeftButton,
    29. Qt::LeftButton,
    30. Qt::NoModifier);
    31.  
    32. qApp->postEvent((QObject*)txt,(QEvent *)event2);
    33.  
    34.  
    35. txt->show();
    36.  
    37. return a.exec();
    38. }
    To copy to clipboard, switch view to plain text mode 



    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.
    Last edited by vk41286; 10th June 2015 at 08:32.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Simulate mouse click inside QTextEdit in Qt?

    Quote Originally Posted by vk41286 View Post
    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?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    May 2015
    Posts
    5
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Android

    Default Re: Simulate mouse click inside QTextEdit in Qt?

    IMG_20150610_112142.jpg

    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

    Qt Code:
    1. #include <QLabel>
    2. #include <QFont>
    3. #include <QtGui>
    4. #include <QPixmap>
    5. #include <QTextEdit>
    6. #include <QTextCursor>
    7. #include <QLineEdit>
    8.  
    9. int main( int argc, char **argv ) {
    10. QApplication app( argc, argv );
    11.  
    12. QTextEdit *txt = new QTextEdit();
    13. txt->setWindowFlags(Qt::Window | Qt::FramelessWindowHint);
    14. txt->setFocus();
    15. txt->setStyleSheet("background-color: rgba(255, 255, 255, 200);");
    16. txt->setGeometry(10,20,100,30);
    17.  
    18. QTextCursor cursor = QTextCursor(txt->document());
    19. cursor.insertText("Text 1");
    20. cursor.beginEditBlock();
    21. // OR
    22. //In your case, either methods below will work since the text has been inserted already
    23. //cursor.movePosition(QTextCursor::End);
    24. //cursor.movePosition(QTextCursor::Start);
    25. txt->show();
    26. return app.exec();
    27. }
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Simulate mouse click inside QTextEdit in Qt?

    Quote Originally Posted by vk41286 View Post
    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    May 2015
    Posts
    5
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Android

    Default Re: Simulate mouse click inside QTextEdit in Qt?

    All I want is the Cursor to blink in my widget (QTextEdit).

    Thank you for helping. Waiting for your code

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Simulate mouse click inside QTextEdit in Qt?

    Quote Originally Posted by vk41286 View Post
    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    May 2015
    Posts
    5
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Android

    Default Re: Simulate mouse click inside QTextEdit in Qt?

    Could you please provide me some sample code. I am completely new to QT. I have 4 days of exp in QT

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Simulate mouse click inside QTextEdit in Qt?

    Quote Originally Posted by vk41286 View Post
    Could you please provide me some sample code. I am completely new to QT. I have 4 days of exp in QT
    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. How to simulate QTouchEvent using mouse
    By dpatel in forum Qt Programming
    Replies: 5
    Last Post: 28th April 2016, 01:40
  2. Simulate mouse click
    By Merylin in forum Qt Programming
    Replies: 8
    Last Post: 18th March 2014, 03:27
  3. how to simulate mouse click behavior?
    By hashb in forum Qt Programming
    Replies: 2
    Last Post: 31st August 2010, 07:53
  4. Replies: 4
    Last Post: 15th March 2010, 14:49
  5. Replies: 1
    Last Post: 18th December 2009, 20:57

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.