difference between setFocus() and setFocusPolicy()
-
Hi,
setFocus to set the keyboard focus on the widget, setFocusPolicy to tell how the widget should accept keyboard focus.
-
@SGaist
i see thanks.i have a problem:
i have a class that inherits QTextEdit called ChatBox, i promoted QTextEdit object to ChatBox
on the ChatBox constructor i called this function:setFocusPolicy(Qt::FocusPolicy::ClickFocus);
heres the reimplemntation of keyPressEvent
void ChatBox::keyPressEvent(QKeyEvent *event)
{
if (event->key() == Qt::Key_Return)
{
qDebug() << "enter";
}
else
{
qDebug() << "notenter"
QTextEdit::keyPressEvent(event);
}
}when i pressed enter, theres only a time that if statement will be executed.
sometimes when i press enter, the else statemet is executed i dont know why.
sometimes i need to click again on the widget to make it work, and etc.if i pressed enter it should and should only execute the if statement.
im lost now :( -
Why are you calling keyPressEvent in keyReleaseEvent ?
-
You're welcome !
Don't forget to either accept or ignore the event when you don't call the base class implementation of keyPressEvent.