Hi Shuchi,
Do the following:
in header file
protected:
private:
in cpp file:
in constructor:
m_pTxBox->installEventFilter(this);
{
if (target == m_pTxBox)
{
if (event
->type
() == QEvent::KeyPress) {
QKeyEvent *keyEvent
= static_cast<QKeyEvent
*>
(event
);
std::string str = keyEvent->text().toStdString();
char ch = str[0];
emit keyPressed(ch); // emit signal keyPressed with char entered
}
}
return QWidget::eventFilter(target, event
);
}
in header file
protected:
bool eventFilter(QObject *target, QEvent *event);
private:
QTextEdit * m_pTxBox;
in cpp file:
in constructor:
m_pTxBox->installEventFilter(this);
bool myWindow::eventFilter(QObject *target, QEvent *event)
{
if (target == m_pTxBox)
{
if (event->type() == QEvent::KeyPress)
{
QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
std::string str = keyEvent->text().toStdString();
char ch = str[0];
emit keyPressed(ch); // emit signal keyPressed with char entered
}
}
return QWidget::eventFilter(target, event);
}
To copy to clipboard, switch view to plain text mode
or you can make your own class like:
{
protected:
signals:
void keyPressed(int);
...
}
void TextEdit
::keyPressEvent(QKeyEvent *event
) {
std::string str = event->text().toStdString();
int ch = str[0];
emit keyPressed(ch);
}
class TextEdit : public QTextEdit
{
protected:
void keyPressEvent(QKeyEvent *event);
signals:
void keyPressed(int);
...
}
void TextEdit::keyPressEvent(QKeyEvent *event)
{
std::string str = event->text().toStdString();
int ch = str[0];
emit keyPressed(ch);
QTextEdit::keyPressEvent(event);
}
To copy to clipboard, switch view to plain text mode
Bookmarks