Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QLineEdit
Qt 6.11 is out! See what's new in the release blog

QLineEdit

Scheduled Pinned Locked Moved Solved General and Desktop
qlineeditfocus issue
9 Posts 5 Posters 5.0k Views 3 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    gabor53
    wrote on last edited by
    #1

    Hi,
    I'm trying to read and validate the entry in QLineEdit when it loses focus either by pressing TAB, ENTER or the user clicks on an other widget.
    I have the following code so far (which is not working):

        while((ui->lineEdit->hasFocus ()== (true) ))
        {
        	QString string;
            if(string.length ()<2)
              {
                  ui->label_2->setText ("The name is too short!");
                  ui->label_2->setStyleSheet ("color: red");
    
                  QPixmap pix
          ("C:/Programming/Projects/FolkFriends/icons/angry.png");
                  ui->label_3->setScaledContents (true);
                  ui->label_3->setPixmap (pix);
              }
              else
              {
                  ui->label_2->setText ("");
                  QPixmap pix2("C:/Programming/Projects/FolkFriends/icons/Check-icon.png");
                  ui->label_3->setScaledContents (true);
                  ui->label_3->setPixmap (pix2);
              }
          }
    	  QString Name;
          Name = ui->lineEdit->text ();
          qDebug() << "Name: " << Name;
    

    Please help me to figure out how to do it correctly.
    Thank you.

    CharbyC 1 Reply Last reply
    0
    • M Offline
      M Offline
      michelson
      wrote on last edited by
      #2
      This post is deleted!
      1 Reply Last reply
      0
      • mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on last edited by mrjj
        #3

        Hi
        A while loop is not the way :)

        as @Charby suggest might be the fastest!

        Alternatively You can use
        http://doc.qt.io/qt-5.5/qwidget.html#focusOutEvent
        Using a subclass

        class LineEdit : public QLineEdit
        {
            virtual void focusOutEvent( QFocusEvent* )
            {
             /// focus is lost. do stuff
             }
        };
        
        1 Reply Last reply
        0
        • G gabor53

          Hi,
          I'm trying to read and validate the entry in QLineEdit when it loses focus either by pressing TAB, ENTER or the user clicks on an other widget.
          I have the following code so far (which is not working):

              while((ui->lineEdit->hasFocus ()== (true) ))
              {
              	QString string;
                  if(string.length ()<2)
                    {
                        ui->label_2->setText ("The name is too short!");
                        ui->label_2->setStyleSheet ("color: red");
          
                        QPixmap pix
                ("C:/Programming/Projects/FolkFriends/icons/angry.png");
                        ui->label_3->setScaledContents (true);
                        ui->label_3->setPixmap (pix);
                    }
                    else
                    {
                        ui->label_2->setText ("");
                        QPixmap pix2("C:/Programming/Projects/FolkFriends/icons/Check-icon.png");
                        ui->label_3->setScaledContents (true);
                        ui->label_3->setPixmap (pix2);
                    }
                }
          	  QString Name;
                Name = ui->lineEdit->text ();
                qDebug() << "Name: " << Name;
          

          Please help me to figure out how to do it correctly.
          Thank you.

          CharbyC Offline
          CharbyC Offline
          Charby
          wrote on last edited by
          #4

          @gabor53 you should connect the editingFinished signal of your lineedit to your custom slot.

          1 Reply Last reply
          2
          • G Offline
            G Offline
            gabor53
            wrote on last edited by
            #5

            I came up with the following (not working) code:

            	setFocusPolicy (Qt::StrongFocus);
            
                if((ui->lineEdit->QFocusEvent::lostFocus())==true )
            
            	{
                		QString string;
                       string = ui->lineEdit->text ();
                                  if(string.length ()<2)
                                    {
                                        ui->label_4->setText ("The name is too short!");
                                        ui->label_4->setStyleSheet ("color: red");
            
                                        QPixmap pix
                                ("C:/Programming/Projects/FolkFriends/icons/angry.png");
                                        ui->label_3->setScaledContents (true);
                                        ui->label_3->setPixmap (pix);
                                    }
                                    else
                                    {
                                        ui->label_4->setText ("");
                                        QPixmap pix2("C:/Programming/Projects/FolkFriends/icons/Check-icon.png");
                                        ui->label_3->setScaledContents (true);
                                        ui->label_3->setPixmap (pix2);
                                    }
            
                                QString Name;
                                Name = ui->lineEdit->text ();
                                qDebug() << "Name: " << Name;
            
            	}
            

            When I run it I get the following error message:
            C:\Programming\Projects\Folkfriends\additem.cpp:-1: In constructor 'Additem::Additem(QWidget*)':
            C:\Programming\Projects\Folkfriends\additem.cpp:51: error: 'QFocusEvent' is not a base of 'QLineEdit'
            if((ui->lineEdit->QFocusEvent::lostFocus())==true )
            ^
            What am I doing wrong?
            Thank you for all your help.

            1 Reply Last reply
            0
            • ValentinMicheletV Offline
              ValentinMicheletV Offline
              ValentinMichelet
              wrote on last edited by
              #6

              This is a C++ error, not an issue with Qt, and the compiler is pretty explicit.
              There is no lostFocus() method in QLineEdit class. And adding QFocusEvent is not the solution. Instead, consider implementing what @Charby suggested: connect your ui->lineEdit editingFinished() signal to your custom slot:

              connect(ui->lineEdit, SIGNAL(editingFinished()), this, SLOT(readAndValidate()));
              

              Add this connect in your the constructor of the widget that contains the ui.

              1 Reply Last reply
              0
              • G Offline
                G Offline
                gabor53
                wrote on last edited by
                #7

                Do I need to create a separate class and object to receive this signal?

                1 Reply Last reply
                0
                • ValentinMicheletV Offline
                  ValentinMicheletV Offline
                  ValentinMichelet
                  wrote on last edited by ValentinMichelet
                  #8

                  connect(ui->lineEdit, SIGNAL(editingFinished()), this, SLOT(readAndValidate()));

                  The 'this' means that readAndValidate() is a slot that belongs to 'this'.

                  1 Reply Last reply
                  0
                  • G Offline
                    G Offline
                    gabor53
                    wrote on last edited by
                    #9

                    Thank you it worked.

                    1 Reply Last reply
                    0

                    • Login

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved