PDA

View Full Version : QPlainTextEdit shortcut



Sölve
10th May 2011, 15:51
Hi.
I writing app (text editor), where I need to commit changes by clicking a return/enter key.
To edit text I using QPlainTextEdit, but my shortcut not working.
I don't need to have new lines in text.
I tried to replace QPlainTextEdit with QLineEdit, but QLineEdit don't have line wrapping.
How can I implement this shortcut or how can I get line wrapping in QLineEdit.

Sorry for my bad english.

Code for shortcut and QLineEdit:



QShortcut* shortcut = new QShortcut(QKeySequence("Return"), ui->lineEdit);
connect(shortcut, SIGNAL(activated()), this, SLOT(confirm()));

Santosh Reddy
10th May 2011, 17:48
Try this, if it fits your need,

Sub-class QPlainTextEdit, and reimplement QPlainTextEdit::keyPressEvent() and QPlainTextEdit::keyReleaseEvent() Protected Functions.

Sölve
10th May 2011, 22:33
I created sub-class for QPlainTextEdit and reimplemented QPlainTextEdit::keyPressEvent(), but when I trying to write something, there's nothing appears.

I tried to reimplement QPlainTextEdit::keyReleaseEvent(), but when I clicking "Return" new line appears and then changes are confirming.

Code:



void EPlainTextEdit::keyPressEvent(QKeyEvent *e)
{
if(e->key() == 0x01000004 or e->key() == 0x01000005)
{
eustia->confirm();
}
}


"eustia" is object of main class.

Santosh Reddy
12th May 2011, 17:48
You may also need to call the base class implementation QPlainTextEdit::keyPressEvent(e); before you implmentation, but record the key before calling it, just to be sure.

Sölve
13th May 2011, 18:15
I called QPlainTextEdit:keyPressEvent, and now all is done ;)

Code:


void EPlainTextEdit::keyPressEvent(QKeyEvent *e)
{
if(e->key() == 0x01000004 or e->key() == 0x01000005)
{
eustia->confirm();
return;
}
QPlainTextEdit::keyPressEvent(e);
}

Thanks for the help.

vtraveller
17th May 2014, 07:40
Thanks for the post - solved my problem. I decided to walk the controls such that normal Windows enter key behaviour could be implemented. So my handler looks like this:



void TextEdit::keyPressEvent(QKeyEvent * in_pEvent)
{
switch (in_pEvent->key())
{
case Qt::Key_Return:
case Qt::Key_Enter:
{
// Find next button in tab order
QPushButton * pButton = NULL;
while (!pButton)
{
focusNextChild();
pButton = dynamic_cast<QPushButton *>(parentWidget()->focusWidget());
}

// Click the button
pButton->click();

// Restore the focus
setFocus();
break;
}

default:
QTextEdit::keyPressEvent(in_pEvent);
}
}