Results 1 to 15 of 15

Thread: Posting a QKeyEvent to a QLineEdit

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    May 2006
    Posts
    16
    Thanks
    7
    Qt products
    Qt4
    Platforms
    Unix/X11

    Question Posting a QKeyEvent to a QLineEdit

    I am trying to post a QKeyEvent to a QLineEdit. The line edit seems to receive the event (I have tested this with an event filter), but the character is not displayed in the QLineEdit. Does anyone know why the QLineEdit does not display the character? Here is the code:

    main.cpp
    Qt Code:
    1. #include <QApplication>
    2. #include "TestForm.h"
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6.  
    7. QApplication app(argc, argv);
    8.  
    9. TestForm form;
    10. form.show();
    11.  
    12. return app.exec();
    13. }
    To copy to clipboard, switch view to plain text mode 

    TestForm.h
    Qt Code:
    1. #ifndef TEST_FORM_H
    2. #define TEST_FORM_H
    3.  
    4. #include <QDialog>
    5.  
    6. class QLineEdit;
    7.  
    8. class TestForm : public QDialog
    9. {
    10. Q_OBJECT
    11.  
    12. public:
    13. TestForm(QDialog* parent = 0);
    14.  
    15. public slots:
    16. void postKeyEvent();
    17.  
    18. private:
    19. QLineEdit* _line_edit;
    20. };
    21.  
    22. #endif // TEST_FORM_H
    To copy to clipboard, switch view to plain text mode 

    TestForm.h
    Qt Code:
    1. #include <QDialog>
    2. #include <QFrame>
    3. #include <QHBoxLayout>
    4. #include <QPushButton>
    5. #include <QLineEdit>
    6. #include <QSpacerItem>
    7. #include <QVBoxLayout>
    8. #include <QKeyEvent>
    9. #include <QApplication>
    10.  
    11.  
    12. #include "TestForm.h"
    13.  
    14.  
    15. TestForm::TestForm(QDialog* parent)
    16. : QDialog(parent)
    17. {
    18. QFrame* frame = new QFrame(this);
    19. frame->setFrameShape(QFrame::StyledPanel);
    20. frame->setFrameShadow(QFrame::Raised);
    21.  
    22. QHBoxLayout* hboxLayout_2 = new QHBoxLayout(frame);
    23. hboxLayout_2->setSpacing(6);
    24. hboxLayout_2->setMargin(0);
    25.  
    26. _line_edit = new QLineEdit(frame);
    27.  
    28. hboxLayout_2->addWidget(_line_edit);
    29.  
    30. QPushButton* button = new QPushButton(frame);
    31. button->setText("Post key event");
    32.  
    33. hboxLayout_2->addWidget(button);
    34.  
    35. QObject::connect(button, SIGNAL(clicked()), this, SLOT(postKeyEvent()));
    36.  
    37.  
    38. QHBoxLayout* hboxLayout = new QHBoxLayout();
    39. hboxLayout->setSpacing(6);
    40. hboxLayout->setMargin(0);
    41.  
    42. QSpacerItem* spacerItem = new QSpacerItem(131, 31, QSizePolicy::Expanding, QSizePolicy::Minimum);
    43.  
    44. hboxLayout->addItem(spacerItem);
    45.  
    46. QPushButton* okButton = new QPushButton(this);
    47. okButton->setText("OK");
    48.  
    49. hboxLayout->addWidget(okButton);
    50.  
    51. QPushButton* cancelButton = new QPushButton(this);
    52. cancelButton->setText("Cancel");
    53.  
    54. hboxLayout->addWidget(cancelButton);
    55.  
    56. QVBoxLayout *vboxLayout = new QVBoxLayout(this);
    57. vboxLayout->setSpacing(6);
    58. vboxLayout->setMargin(9);
    59.  
    60. vboxLayout->addWidget(frame);
    61.  
    62. vboxLayout->addLayout(hboxLayout);
    63.  
    64. // Connect the button clicked signals to the form slots
    65. QObject::connect(okButton, SIGNAL(clicked()), this, SLOT(accept()));
    66. QObject::connect(cancelButton, SIGNAL(clicked()), this, SLOT(reject()));
    67.  
    68. }
    69.  
    70.  
    71.  
    72. void TestForm::postKeyEvent()
    73. {
    74.  
    75. QApplication::postEvent(_line_edit,
    76. new QKeyEvent(QEvent::KeyPress,
    77. Qt::Key_A,
    78. Qt::NoModifier));
    79. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by cocheci; 30th May 2006 at 21:13.

Similar Threads

  1. a box around QLineEdit?
    By GreyGeek in forum Qt Tools
    Replies: 13
    Last Post: 8th February 2006, 15:40

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
  •  
Qt is a trademark of The Qt Company.