Hi, Chris.
Perhaps I didn't explain well the problem.
In my project, attached to the first message, I have one Designer file, mainwindow.ui. If you open this file with QtDesigner, you have three QLineEdit objects and three TextLabel objects.
Don't modify the QlineEdit: first you run the project (with QtCreator is the best) setting the three TextLine objects focusPolicy property on NoFocus. With this settings the executable highlights the problem.
Afterwards modify the focusPolicy on the three TextLabels only to StrongFocus. Now the program runs fine.
My question is: why?
Below I show the file I created with QtDesigner:
schermata1.jpg
and this is the code I wrote on mainwindow.cpp:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QMessageBox>
MainWindow
::MainWindow(QWidget *parent
) : ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->lineEdit_1->installEventFilter(this);
ui->lineEdit_2->installEventFilter(this);
ui->lineEdit_3->installEventFilter(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
{
if (event
->type
() == QEvent::FocusOut) {
if (obj == ui->lineEdit_1)
if (obj == ui->lineEdit_2)
if (obj == ui->lineEdit_3)
}
return false;
}
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QMessageBox>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->lineEdit_1->installEventFilter(this);
ui->lineEdit_2->installEventFilter(this);
ui->lineEdit_3->installEventFilter(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
bool MainWindow::eventFilter(QObject *obj, QEvent *event)
{
if (event->type() == QEvent::FocusOut)
{
if (obj == ui->lineEdit_1)
QMessageBox::information(this, "", "line1");
if (obj == ui->lineEdit_2)
QMessageBox::information(this, "", "line2");
if (obj == ui->lineEdit_3)
QMessageBox::information(this, "", "line3");
}
return false;
}
To copy to clipboard, switch view to plain text mode
Don't modify the code: simply download the file I attached to the first message and run it on QtCreator, switching only the focusPolicy property on the three TextLabels and you have the strange behavior of the QLineEdit widget.
Now I'm attaching two zipped files, with the two different settings and that show the behaviors.
Or, if in your code change the lines:
ui->lineEdit_1->setFocusPolicy(Qt::NoFocus); // line 14
ui->lineEdit_2->setFocusPolicy(Qt::NoFocus); // line 15
ui->lineEdit_3->setFocusPolicy(Qt::NoFocus); // line 16
ui->lineEdit_1->setFocusPolicy(Qt::NoFocus); // line 14
ui->lineEdit_2->setFocusPolicy(Qt::NoFocus); // line 15
ui->lineEdit_3->setFocusPolicy(Qt::NoFocus); // line 16
To copy to clipboard, switch view to plain text mode
with these:
ui->label->setFocusPolicy(Qt::NoFocus); // line 14
ui->label_2->setFocusPolicy(Qt::NoFocus); // line 15
ui->label_3->setFocusPolicy(Qt::NoFocus); // line 16
ui->label->setFocusPolicy(Qt::NoFocus); // line 14
ui->label_2->setFocusPolicy(Qt::NoFocus); // line 15
ui->label_3->setFocusPolicy(Qt::NoFocus); // line 16
To copy to clipboard, switch view to plain text mode
or
ui->label->setFocusPolicy(Qt::StrongFocus);
ui->label_2->setFocusPolicy(Qt::StrongFocus);
ui->label_3->setFocusPolicy(Qt::StrongFocus);
ui->label->setFocusPolicy(Qt::StrongFocus);
ui->label_2->setFocusPolicy(Qt::StrongFocus);
ui->label_3->setFocusPolicy(Qt::StrongFocus);
To copy to clipboard, switch view to plain text mode
and have what I say without modify the .ui file.
Added after 17 minutes:
Ok, I'm a troll, I admit it.
The bug is only in my code.
When I add the QMessageBox on the handling of the FocusOut event, happens what follows:
1. the focus is passed from the actual widget (e.g. Line_1) to the next QLineEdit widget (e.g. Line_2);
2. QMessageBox got the focus;
3. Line_2 lose focus and throw the event message (Line_2::FocusOut);
4. I close the QMessageBox;
5. Line_1::FocusOut event, queued from Qt to Line_2::FocusOut, is thrown;
6. and so on.
The bottom line is:
in the management function of a focus event, never use an object that manages the focus!!
Thank you, Chris, examining your code I understand my fault.
------------------------------------------------
A genius learns from the mistakes of others, a smart man learns from their own, the fool never learns.
Bookmarks