PDA

View Full Version : fetching each char entered in Qtextedit??



Shuchi Agrawal
15th February 2007, 09:12
I want to retrieve the char as and when they are entered in the QTextedit box. can anyone help me how to do it? plz specify some relevant code if possible.

Thanks

rajesh
16th February 2007, 07:15
Hi Shuchi,

Do the following:




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


or you can make your own class like:



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

rajesh
17th February 2007, 09:45
Hi Shuchi,
Is your problem solved? Which one you used?

installEventFilter or reimplementation of keyPressEvent?
Or your problem was something else?

Regards
Rajesh

Shuchi Agrawal
17th February 2007, 11:07
hi,
i simply fetched the last char of the string returned by "toPlainText()".
but thanks for ur reply.