PDA

View Full Version : Process Enter Key on QTableWidget



kandalf
8th May 2007, 21:25
Hi guys, it's been so long, unfortunately I'm coming and going from Qt due to work issues.
I'm trying to insert an Enter/Return Key within a QTableWidgetItem.
When I'm editing a value in a QTableWidget and press Enter, it commits the data but I need a multiline control.
I already tried to set a QTextEdit, a QTextEdit derived class as cell widget, overloading keyPressEvent, but I had no success.

Is it possible to do?

Cheers and thanx in advance.

marcel
8th May 2007, 21:29
If you have a custom QTableWidget then install an event filter on it, catch enter/return on it and if you have an editor opened on a cell just forward the key press event to the editor.

BTW: how will you commit the editor data if the editor will get the returns?

Regards

kandalf
8th May 2007, 21:44
If you have a custom QTableWidget then install an event filter on it, catch enter/return on it and if you have an editor opened on a cell just forward the key press event to the editor.
Ok, how can I know if an editor is opened on a cell?


BTW: how will you commit the editor data if the editor will get the returns?
I'm planning to catch a Ctrl+Enter and close the editor.

marcel
8th May 2007, 21:51
Set a flag in your class when an editor is opened ( you'll have to detect that ).
You might want to explore:


QAbstractItemModel * model, const QStyleOptionViewItem & option, const QModelIndex & index ) [virtual]
maybe you can do something with it ( unfortunately I have never used it ).



I'm planning to catch a Ctrl+Enter and close the editor
OK then, sounds good.

Regards

kandalf
8th May 2007, 21:59
Ok, Thanx a lot for a really quickly and accurate response. I'll see what I can do and I will post the solution (if I find some) for someone who may need it.

Cheers and thanx again!!!

kandalf
9th May 2007, 15:31
Hi guys, unfortunately, I still have problems, even with the event filter.
It seems to not work fine for me, because even when the eventFilter method return true the event is forwarded to the QTableWidget derived object.

This is my code of the event filter object:

#include <QKeyEvent>
#include "TextProcessor.h"

bool TextProcessor::eventFilter(QObject *obj, QEvent *evt)
{
if (evt->type() == QEvent::KeyPress)
{
QKeyEvent *keyEvent = static_cast<QKeyEvent *>(evt);
if (keyEvent->key() == Qt::Key_Return)
{
if (keyEvent->modifiers() == Qt::ControlModifier)
{
qWarning("Ctrl");
emit escapeCharPressed();
return false;
}
else
{
qWarning("Enter");
// emit lineFeed();
return true;
}
}
else
{
return false;
}
}
else
{
return QObject::eventFilter(obj, evt);
}
}
The case is that when I press Enter Key I got the line in debugging, but the editor is still closing, so I guess the event is still forwarding to the monitored object.
What am I doing wrong?

Thanx a lot in advance.