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
#include <QApplication>
#include "TestForm.h"
int main(int argc, char *argv[])
{
TestForm form;
form.show();
return app.exec();
}
#include <QApplication>
#include "TestForm.h"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
TestForm form;
form.show();
return app.exec();
}
To copy to clipboard, switch view to plain text mode
TestForm.h
#ifndef TEST_FORM_H
#define TEST_FORM_H
#include <QDialog>
{
Q_OBJECT
public:
public slots:
void postKeyEvent();
private:
};
#endif // TEST_FORM_H
#ifndef TEST_FORM_H
#define TEST_FORM_H
#include <QDialog>
class QLineEdit;
class TestForm : public QDialog
{
Q_OBJECT
public:
TestForm(QDialog* parent = 0);
public slots:
void postKeyEvent();
private:
QLineEdit* _line_edit;
};
#endif // TEST_FORM_H
To copy to clipboard, switch view to plain text mode
TestForm.h
#include <QDialog>
#include <QFrame>
#include <QHBoxLayout>
#include <QPushButton>
#include <QLineEdit>
#include <QSpacerItem>
#include <QVBoxLayout>
#include <QKeyEvent>
#include <QApplication>
#include "TestForm.h"
TestForm
::TestForm(QDialog* parent
){
frame
->setFrameShape
(QFrame::StyledPanel);
frame
->setFrameShadow
(QFrame::Raised);
hboxLayout_2->setSpacing(6);
hboxLayout_2->setMargin(0);
hboxLayout_2->addWidget(_line_edit);
button->setText("Post key event");
hboxLayout_2->addWidget(button);
QObject::connect(button,
SIGNAL(clicked
()),
this,
SLOT(postKeyEvent
()));
hboxLayout->setSpacing(6);
hboxLayout->setMargin(0);
hboxLayout->addItem(spacerItem);
okButton->setText("OK");
hboxLayout->addWidget(okButton);
cancelButton->setText("Cancel");
hboxLayout->addWidget(cancelButton);
vboxLayout->setSpacing(6);
vboxLayout->setMargin(9);
vboxLayout->addWidget(frame);
vboxLayout->addLayout(hboxLayout);
// Connect the button clicked signals to the form slots
QObject::connect(okButton,
SIGNAL(clicked
()),
this,
SLOT(accept
()));
QObject::connect(cancelButton,
SIGNAL(clicked
()),
this,
SLOT(reject
()));
}
void TestForm::postKeyEvent()
{
Qt::Key_A,
Qt::NoModifier));
}
#include <QDialog>
#include <QFrame>
#include <QHBoxLayout>
#include <QPushButton>
#include <QLineEdit>
#include <QSpacerItem>
#include <QVBoxLayout>
#include <QKeyEvent>
#include <QApplication>
#include "TestForm.h"
TestForm::TestForm(QDialog* parent)
: QDialog(parent)
{
QFrame* frame = new QFrame(this);
frame->setFrameShape(QFrame::StyledPanel);
frame->setFrameShadow(QFrame::Raised);
QHBoxLayout* hboxLayout_2 = new QHBoxLayout(frame);
hboxLayout_2->setSpacing(6);
hboxLayout_2->setMargin(0);
_line_edit = new QLineEdit(frame);
hboxLayout_2->addWidget(_line_edit);
QPushButton* button = new QPushButton(frame);
button->setText("Post key event");
hboxLayout_2->addWidget(button);
QObject::connect(button, SIGNAL(clicked()), this, SLOT(postKeyEvent()));
QHBoxLayout* hboxLayout = new QHBoxLayout();
hboxLayout->setSpacing(6);
hboxLayout->setMargin(0);
QSpacerItem* spacerItem = new QSpacerItem(131, 31, QSizePolicy::Expanding, QSizePolicy::Minimum);
hboxLayout->addItem(spacerItem);
QPushButton* okButton = new QPushButton(this);
okButton->setText("OK");
hboxLayout->addWidget(okButton);
QPushButton* cancelButton = new QPushButton(this);
cancelButton->setText("Cancel");
hboxLayout->addWidget(cancelButton);
QVBoxLayout *vboxLayout = new QVBoxLayout(this);
vboxLayout->setSpacing(6);
vboxLayout->setMargin(9);
vboxLayout->addWidget(frame);
vboxLayout->addLayout(hboxLayout);
// Connect the button clicked signals to the form slots
QObject::connect(okButton, SIGNAL(clicked()), this, SLOT(accept()));
QObject::connect(cancelButton, SIGNAL(clicked()), this, SLOT(reject()));
}
void TestForm::postKeyEvent()
{
QApplication::postEvent(_line_edit,
new QKeyEvent(QEvent::KeyPress,
Qt::Key_A,
Qt::NoModifier));
}
To copy to clipboard, switch view to plain text mode
Bookmarks