Results 1 to 3 of 3

Thread: There is a bug on QlineEdit?

  1. #1
    Join Date
    Nov 2010
    Milazzo, Italy
    Qt products
    Unix/X11 Windows

    Default There is a bug on QlineEdit?

    Hi, folks.
    I'm in trouble from yesterday for a strange behavior of QLineEdit.
    I need to keep trace (and manage) the moment when this widget (QLineEdit) lose its focus. I need three consecutive QlineEdit widgets, and I must control the FocusOut event of every widget.

    I followed two lines:

    1. install the InstallEventFilter for each widget, and then declare and code the EventFilter method for the window that manage the events


    2. subclass QLineEdit and, within the QLineEdit::FocusOut event, emit a new signal. This signal is linked through a connection to a slot, in the window where I need to manage the event.

    Both lines had the same problem: when one widget loses the focus, two event raises: the first from the next widget (!!), then the correct one. This behavior repeats for every widget.

    To better explain, suppose we have three consecutive QLineEdit widget, named Line1, Line2 and Line3. When Line1 loses focus, first raise Line2 event and then Line1 event.
    When Line2 loses focus, first raise Line3 event and then Line2 event.
    When Line3 loses focus, first raise Line1 event and then Line2 event.

    This strange behavior ceases when beneath the three widgets I insert other widgets that don't handle their events.

    I attach to this post one example project that explaines what I say.
    To point out this problem, switch the three TextLabel widgets in the main window Designer file (MainWindow.ui) from focusPolicy::NoFocus to focusPolicy::StrongFocus. When the focus policy is set to NoFocus the error raises, but when the focus policy is set to StrongFocus all goes well.

    Notice that the same problem raises if I subclass QLineEdit, managing a new signal from FocusOut event.
    Attached Files Attached Files

  2. #2
    Join Date
    Mar 2009
    Brisbane, Australia
    Thanked 1,587 Times in 1,516 Posts
    Qt products
    Qt4 Qt5
    Unix/X11 Windows
    Wiki edits

    Default Re: There is a bug on QlineEdit?

    I really don't see the point of a QLineEdit that cannot have focus; without focus editing it will be difficult. I see no focus in or out events for such widgets. This is the case with your example as modified:
    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3. #include <QMessageBox>
    4. #include <QDebug>
    6. MainWindow::MainWindow(QWidget *parent) :
    7. QMainWindow(parent),
    8. ui(new Ui::MainWindow)
    9. {
    10. ui->setupUi(this);
    11. ui->lineEdit_1->installEventFilter(this);
    12. ui->lineEdit_2->installEventFilter(this);
    13. ui->lineEdit_3->installEventFilter(this);
    14. ui->lineEdit_1->setFocusPolicy(Qt::NoFocus);
    15. ui->lineEdit_2->setFocusPolicy(Qt::NoFocus);
    16. ui->lineEdit_3->setFocusPolicy(Qt::NoFocus);
    17. }
    19. MainWindow::~MainWindow()
    20. {
    21. delete ui;
    22. }
    24. bool MainWindow::eventFilter(QObject *obj, QEvent *event)
    25. {
    26. if (event->type() == QEvent::FocusOut)
    27. qDebug() << "Focus out" << obj;
    28. else if (event->type() == QEvent::FocusIn)
    29. qDebug() << "Focus in" << obj;
    30. return false;
    31. }
    To copy to clipboard, switch view to plain text mode 
    With the default StrongFocus policy on the line edits is see correct behaviour: one focus out followed by one focus in.

    What are you trying to achieve by setting StrongFocus on the labels?

  3. #3
    Join Date
    Nov 2010
    Milazzo, Italy
    Qt products
    Unix/X11 Windows

    Cool Re: There is a bug on QlineEdit?

    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:


    and this is the code I wrote on mainwindow.cpp:

    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3. #include <QMessageBox>
    5. MainWindow::MainWindow(QWidget *parent) :
    6. QMainWindow(parent),
    7. ui(new Ui::MainWindow)
    8. {
    9. ui->setupUi(this);
    10. ui->lineEdit_1->installEventFilter(this);
    11. ui->lineEdit_2->installEventFilter(this);
    12. ui->lineEdit_3->installEventFilter(this);
    13. }
    15. MainWindow::~MainWindow()
    16. {
    17. delete ui;
    18. }
    20. bool MainWindow::eventFilter(QObject *obj, QEvent *event)
    21. {
    22. if (event->type() == QEvent::FocusOut)
    23. {
    24. if (obj == ui->lineEdit_1)
    25. QMessageBox::information(this, "", "line1");
    27. if (obj == ui->lineEdit_2)
    28. QMessageBox::information(this, "", "line2");
    30. if (obj == ui->lineEdit_3)
    31. QMessageBox::information(this, "", "line3");
    33. }
    35. return false;
    36. }
    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:

    Qt Code:
    1. ui->lineEdit_1->setFocusPolicy(Qt::NoFocus); // line 14
    2. ui->lineEdit_2->setFocusPolicy(Qt::NoFocus); // line 15
    3. ui->lineEdit_3->setFocusPolicy(Qt::NoFocus); // line 16
    To copy to clipboard, switch view to plain text mode 

    with these:

    Qt Code:
    1. ui->label->setFocusPolicy(Qt::NoFocus); // line 14
    2. ui->label_2->setFocusPolicy(Qt::NoFocus); // line 15
    3. ui->label_3->setFocusPolicy(Qt::NoFocus); // line 16
    To copy to clipboard, switch view to plain text mode 


    Qt Code:
    1. ui->label->setFocusPolicy(Qt::StrongFocus);
    2. ui->label_2->setFocusPolicy(Qt::StrongFocus);
    3. 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.
    Attached Files Attached Files
    Last edited by glafauci; 21st November 2012 at 10:54. Reason: [solved]

Similar Threads

  1. QLineEdit to Int
    By kais khelifa in forum Newbie
    Replies: 3
    Last Post: 27th November 2011, 20:36
  2. Replies: 1
    Last Post: 12th October 2010, 23:20
  3. Tab QLineEdit
    By jaca in forum Qt Programming
    Replies: 1
    Last Post: 13th May 2008, 14:01
  4. QLineEdit
    By coderbob in forum Qt Programming
    Replies: 6
    Last Post: 27th February 2008, 13:27
  5. About QLineEdit
    By vijay anandh in forum Newbie
    Replies: 2
    Last Post: 19th October 2006, 11:45

Tags for this Thread


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.