Why has my Qt widget focus changed?
I basically have a function that checks focus to / from the edit line. From here, it makes a text animation to do the google-style input. Here's the function:
QParallelAnimationGroup *group = new QParallelAnimationGroup;
QPropertyAnimation *posAnimation = new QPropertyAnimation(label, "pos");
posAnimation->setStartValue(QPoint(0, 0));
posAnimation->setEndValue(QPoint(5, 5));
group->addAnimation(posAnimation);
QPropertyAnimation *sizeAnimation = new QPropertyAnimation(label, "size");
sizeAnimation->setStartValue(label->size());
sizeAnimation->setEndValue(QSize(QFontMetrics(pix16).width(label->text()), QFontMetrics(pix16).height()));
group->addAnimation(sizeAnimation);
QPropertyAnimation *fontAnimation = new QPropertyAnimation(label, "fontPixelSize");
fontAnimation->setStartValue(12);
fontAnimation->setEndValue(16);
group->addAnimation(fontAnimation);
group->start(QAbstractAnimation::DeleteWhenStopped);
label->setAttribute(Qt::WA_TransparentForMouseEvents);
updateRect();
return lineEdit->eventFilter(watched, event);
The only problem is that when you push one line to another edit, focus goes away from the first (which is good), then it goes to the second, but quickly goes away from the second.
Not sure why this is happening, but it renders my animation useless.
I am using Qt 5.9 IDE with C ++
EDIT: I subclassed the QLabel to get a custom property:
class Label: public QLabel {
Q_OBJECT
Q_PROPERTY (double fontPixelSize READ fontPixelSize WRITE setFontPixelSize)
public:
Label(QWidget* parent = 0): QLabel(parent)
{
}
double fontPixelSize() const
{
return font().pixelSize();
}
Q_SLOT void setFontPixelSize(int size)
{
QFont f = font();
f.setPixelSize(size);
setFont(f);
}
};
EDIT: Added GitHub link: HERE
source to share
Here's what I found after examining the code from the link:
Problem: In a method GFrame::updateRect
, at least the call QWidget::adjustSize
causes the focus to be stolen from QLineEdit. Probably setMinimumSize
has something to do with this too, but I haven't tested it.
Decision. ... I would suggest that you comment on both calls GFrame::updateRect
to GFrame::eventFilter
. Then it should work as you expect.
Further advice: you can comment on the calls QLineEdit::setFocus
in GFrame::eventFilter
as they are not needed.
source to share