PDA

View Full Version : Event Filtering on Widgets



QbelcorT
26th November 2008, 01:24
Hi,
This is my first widget implementation and I have to tweak a few things to accompany touchscreen input. I installed an event filter on an IP form. When the groupbox OR the textEdit field is touched a number pad will show and the selected text is bolded. The number pad will simulate keypress entry events to the lineedit widget. It is working. Can you please review my implementation and provide suggestions?

1) An easier way to find which target is selected. I want this event filter to be generic, so I don't want it to know about the ui.form names. (i.e ui.PortText) Each lineEdit is inside a groupbox.
2) Is there something I am missing? Is this filter to exhaustive?
Thank you.



bool NetworkConfigurationForm::eventFilter(QObject *target, QEvent *event)
{

// if the mouse/ts area is pressed on the group box or edit line then show numpad
// start the lineEdit at the beginning, make sure there is no selected text
QLineEdit *textEdit = qobject_cast<QLineEdit *>(target);
QGroupBox *groupBox = qobject_cast<QGroupBox *>(target);

if (event->type() == QEvent::MouseButtonPress)
{
// should only be QlineEdit or GroupBox
// but if more filters are added, this needs to be here
if (textEdit || groupBox)
{
// if groupbox widget was touched ignore if same widget, otherwise
// clear font on old widget, set bold on new widget
if(groupBox)
{
QFont font;
QWidget *parent;

qDebug() << groupBox << "GROUPBOX";

// if mousepress on the same widget, ignore it
// Otherwise clear font on old item, set on new item.
if(currentTextSelection)
{
if(currentTextSelection->parentWidget() == groupBox)
return true;
parent = currentTextSelection->parentWidget();
font = parent->font();
font.setBold(false);
parent->setFont(font);
}
font = groupBox->font();
font.setBold(true);
groupBox->setFont(font);
if (ui.ServerIPText->parentWidget() == groupBox)
currentTextSelection = ui.ServerIPText;
else if (ui.TerminalIPText->parentWidget() == groupBox)
currentTextSelection = ui.TerminalIPText;
else if (ui.SubnetText->parentWidget() == groupBox)
currentTextSelection = ui.SubnetText;
else if (ui.GatewayText->parentWidget() == groupBox)
currentTextSelection = ui.GatewayText;
else if (ui.PortText->parentWidget() == groupBox)
currentTextSelection = ui.PortText;
}

// if textEdit widget was touched ignore if same widget, otherwise
// clear font on old widget, set bold on new widget
if (textEdit)
{
QFont font;
QWidget *parent;
qDebug() << textEdit << "textEdit";
if(currentTextSelection)
{
if (textEdit == currentTextSelection)
return true;
parent = currentTextSelection->parentWidget();
font = parent->font();
font.setBold(false);
parent->setFont(font);
}
parent = textEdit->parentWidget();
font = parent->font();
font.setBold(true);
parent->setFont(font);
currentTextSelection = textEdit; // switch to new widget
}
// Start at the beginning of the text
ui.ServerIPText->home(true);
ui.TerminalIPText->home(true);
ui.SubnetText->home(true);
ui.GatewayText->home(true);
// make sure no text has been selected by mistake
ui.ServerIPText->deselect();
ui.TerminalIPText->deselect();
ui.SubnetText->deselect();
ui.GatewayText->deselect();

if(currentTextSelection)
{

if(currentTextSelection != ui.ServerIPText && currentTextSelection != ui.PortText)
padEntry->move(400,0);
else
padEntry->move(0,0);
padEntry->show();
}

return true;
}
}

// this prevents a mouse moving event(dragging) which selects the text
else if (event->type() == QEvent::MouseMove)
return true;


return false;
}

// slot for key entered - from number pad widget //
void NetworkConfigurationForm::keyEntered(QKeyEvent *event)
{
QCoreApplication::sendEvent(currentTextSelection,e vent);
if(event->key() == Qt::Key_Enter)
{
currentTextSelection->clearFocus();
currentTextSelection = 0;
padEntry->hide();
}
}

ktk
28th November 2008, 22:27
Would using QObject::findChildren() be helpful?