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. How to handle when an QLineEdit widget is selected and no longer selected
Forum Updated to NodeBB v4.3 + New Features

How to handle when an QLineEdit widget is selected and no longer selected

Scheduled Pinned Locked Moved Unsolved General and Desktop
18 Posts 5 Posters 2.4k Views 1 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.
  • jsulmJ jsulm

    @Karine said in How to handle when an QLineEdit widget is selected and no longer selected:

    when its selected and no longer selected

    Sounds like Schrodinger widget :-)
    Sorry, could not resist :-)

    JonBJ Online
    JonBJ Online
    JonB
    wrote on last edited by
    #4

    @jsulm
    The code is right. Just it needs to be run on a quantum computer....

    1 Reply Last reply
    0
    • K Offline
      K Offline
      Karine
      wrote on last edited by
      #5

      How fun you'll are, I'm referring to when a line edit is current selected (has focus, the caret is blinking on it) and when no longer.

      JonBJ 1 Reply Last reply
      0
      • K Karine

        How fun you'll are, I'm referring to when a line edit is current selected (has focus, the caret is blinking on it) and when no longer.

        JonBJ Online
        JonBJ Online
        JonB
        wrote on last edited by JonB
        #6

        @Karine
        But what is your question? You read a QLineEdit's text via lineEdit->text(). It does not matter whether the QLineEdit is selected or has focus or is not.

        BTW, you cannot have two methods with the same name (clicked()) as signals, slots or anything else in a class (ignoring overloads, which is not the case here).

        K 1 Reply Last reply
        0
        • JonBJ JonB

          @Karine
          But what is your question? You read a QLineEdit's text via lineEdit->text(). It does not matter whether the QLineEdit is selected or has focus or is not.

          BTW, you cannot have two methods with the same name (clicked()) as signals, slots or anything else in a class (ignoring overloads, which is not the case here).

          K Offline
          K Offline
          Karine
          wrote on last edited by Karine
          #7

          @JonB

          But what is your question?

          I'm asking how to detect when a line edit is currently focused (caret on it) and when it is no longer.

          It does not matter whether the QLineEdit is selected or has focus or is not.

          I'm applying a blur effect into the line edit widget when its focused, similar to this gif:

          2022-12-02_13-20-09.gif

          I don't think it's possible to apply blur using Stylesheet?

          // When the edit is focused  (caret on it):
          QGraphicsDropShadowEffect * dse = new QGraphicsDropShadowEffect();
          dse->setBlurRadius(60);
          dse->setOffset(0);
          dse->setColor(0x007eff);
          ui.Line_Edit->setGraphicsEffect(dse);
          
          // When it's not longer focused
          dse->deleteLate();
          
          Christian EhrlicherC M 2 Replies Last reply
          0
          • K Karine

            @JonB

            But what is your question?

            I'm asking how to detect when a line edit is currently focused (caret on it) and when it is no longer.

            It does not matter whether the QLineEdit is selected or has focus or is not.

            I'm applying a blur effect into the line edit widget when its focused, similar to this gif:

            2022-12-02_13-20-09.gif

            I don't think it's possible to apply blur using Stylesheet?

            // When the edit is focused  (caret on it):
            QGraphicsDropShadowEffect * dse = new QGraphicsDropShadowEffect();
            dse->setBlurRadius(60);
            dse->setOffset(0);
            dse->setColor(0x007eff);
            ui.Line_Edit->setGraphicsEffect(dse);
            
            // When it's not longer focused
            dse->deleteLate();
            
            Christian EhrlicherC Offline
            Christian EhrlicherC Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on last edited by
            #8

            @Karine said in How to handle when an QLineEdit widget is selected and no longer selected:

            I'm asking how to detect when a line edit is currently focused (caret on it) and when it is no longer.

            And you simply ignored my answer which also includes links:

            There is no such attribute 'selected'. Only if a widget has focus or not.

            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
            Visit the Qt Academy at https://academy.qt.io/catalog

            K 1 Reply Last reply
            0
            • K Karine

              @JonB

              But what is your question?

              I'm asking how to detect when a line edit is currently focused (caret on it) and when it is no longer.

              It does not matter whether the QLineEdit is selected or has focus or is not.

              I'm applying a blur effect into the line edit widget when its focused, similar to this gif:

              2022-12-02_13-20-09.gif

              I don't think it's possible to apply blur using Stylesheet?

              // When the edit is focused  (caret on it):
              QGraphicsDropShadowEffect * dse = new QGraphicsDropShadowEffect();
              dse->setBlurRadius(60);
              dse->setOffset(0);
              dse->setColor(0x007eff);
              ui.Line_Edit->setGraphicsEffect(dse);
              
              // When it's not longer focused
              dse->deleteLate();
              
              M Offline
              M Offline
              mpergand
              wrote on last edited by mpergand
              #9

              @Karine
              Yon can know if you have the focus with:

              if(QApplication::focusWidget()==this)
                {
                // has focus
                }
              

              There's focusWidget in QWidget as well:

              Returns the last child of this widget that setFocus had been called on.

              Not sure what's really mean. Have a test if it's return your own widget.
              Or try: parentWidget().focusWidget()

              1 Reply Last reply
              0
              • Christian EhrlicherC Christian Ehrlicher

                @Karine said in How to handle when an QLineEdit widget is selected and no longer selected:

                I'm asking how to detect when a line edit is currently focused (caret on it) and when it is no longer.

                And you simply ignored my answer which also includes links:

                There is no such attribute 'selected'. Only if a widget has focus or not.

                K Offline
                K Offline
                Karine
                wrote on last edited by
                #10

                @Christian-Ehrlicher sorry i didn't see your answer because the jokes caught of my attention.

                I tried this:

                class LineEdit : public QLineEdit
                {
                    Q_OBJECT
                
                public:
                
                    LineEdit(QWidget* parent = 0) : QLineEdit(parent)
                    {
                
                    }
                
                    QGraphicsDropShadowEffect* dse;
                
                    void focusInEvent(QFocusEvent* event)
                    {
                        dse = new QGraphicsDropShadowEffect();
                        dse->setBlurRadius(60);
                        dse->setOffset(0);
                        dse->setColor(0x007eff);
                        this->setGraphicsEffect(dse);
                
                        qDebug() << "focus";
                        QLineEdit::focusInEvent(event); 
                    }
                
                    void focusOutEvent(QFocusEvent* event)
                    {
                        qDebug() << "lost focus";
                        dse->deleteLater();
                        this->update();        
                        this->repaint();    
                        QLineEdit::focusOutEvent(event); 
                    }
                
                };
                

                But after the widget lost focus it did not remove the blur effect, any idea why?
                When i minimize/restore my GUI, then the blur effect is removed.

                M 1 Reply Last reply
                1
                • K Karine

                  @Christian-Ehrlicher sorry i didn't see your answer because the jokes caught of my attention.

                  I tried this:

                  class LineEdit : public QLineEdit
                  {
                      Q_OBJECT
                  
                  public:
                  
                      LineEdit(QWidget* parent = 0) : QLineEdit(parent)
                      {
                  
                      }
                  
                      QGraphicsDropShadowEffect* dse;
                  
                      void focusInEvent(QFocusEvent* event)
                      {
                          dse = new QGraphicsDropShadowEffect();
                          dse->setBlurRadius(60);
                          dse->setOffset(0);
                          dse->setColor(0x007eff);
                          this->setGraphicsEffect(dse);
                  
                          qDebug() << "focus";
                          QLineEdit::focusInEvent(event); 
                      }
                  
                      void focusOutEvent(QFocusEvent* event)
                      {
                          qDebug() << "lost focus";
                          dse->deleteLater();
                          this->update();        
                          this->repaint();    
                          QLineEdit::focusOutEvent(event); 
                      }
                  
                  };
                  

                  But after the widget lost focus it did not remove the blur effect, any idea why?
                  When i minimize/restore my GUI, then the blur effect is removed.

                  M Offline
                  M Offline
                  mpergand
                  wrote on last edited by
                  #11

                  @Karine
                  record the state in a member variable and do the drawing in paintEvent()

                  K 1 Reply Last reply
                  0
                  • M mpergand

                    @Karine
                    record the state in a member variable and do the drawing in paintEvent()

                    K Offline
                    K Offline
                    Karine
                    wrote on last edited by
                    #12

                    @mpergand could you give an example? im not sure how to do this inside of the paint event

                    M 1 Reply Last reply
                    0
                    • K Karine

                      @mpergand could you give an example? im not sure how to do this inside of the paint event

                      M Offline
                      M Offline
                      mpergand
                      wrote on last edited by mpergand
                      #13

                      @Karine

                      class LineEdit : public QLineEdit
                      {
                          Q_OBJECT
                      
                      public:
                      
                          LineEdit(QWidget* parent = 0) : QLineEdit(parent)
                          {
                              blurEffect = new QGraphicsBlurEffect(this);
                              setGraphicsEffect(blurEffect);
                              blurEffect->setEnabled(false);
                              setText("Hello");
                          }
                      
                      private:
                          void focusInEvent(QFocusEvent* event)
                              {
                              blurEffect->setEnabled(false);
                              }
                      
                          void focusOutEvent(QFocusEvent* event)
                              {
                              blurEffect->setEnabled(true);
                              }
                      
                         /* void paintEvent(QPaintEvent* ev)
                          {
                              QLineEdit::paintEvent(ev);
                          }*/
                      
                          QGraphicsBlurEffect * blurEffect;
                      };
                      
                      1 Reply Last reply
                      1
                      • Christian EhrlicherC Offline
                        Christian EhrlicherC Offline
                        Christian Ehrlicher
                        Lifetime Qt Champion
                        wrote on last edited by Christian Ehrlicher
                        #14

                        Since the blur effect is outside of the widget's boundaries, the parent needs a redraw

                        parentWidget()->update();

                        this->update() and repaint() are not needed. I would also go with the solution from @mpergand to not re-create the effect every time but simply enable/disable it.

                        /edit: with @mpergand's solution there is also no extra redraw of the parent needed. I'll take a look on it why this is needed when the effect is destroyed. Looks like a small bug to me.

                        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                        Visit the Qt Academy at https://academy.qt.io/catalog

                        1 Reply Last reply
                        0
                        • K Offline
                          K Offline
                          Karine
                          wrote on last edited by Karine
                          #15

                          Using the code post by @mpergand when the effect is applied to the widget the caret disappears, does this also happen on your side?

                          class LineEdit : public QLineEdit
                          {
                              Q_OBJECT
                          
                          public:
                          
                              QGraphicsDropShadowEffect* dse;
                              LineEdit(QWidget* parent = 0) : QLineEdit(parent)
                              {
                                  dse = new QGraphicsDropShadowEffect(this);
                                  setGraphicsEffect(dse);
                                  dse->setBlurRadius(60);
                                  dse->setOffset(0);
                                  dse->setColor(0x007eff);
                                  dse->setEnabled(false);
                              }
                          
                              void focusInEvent(QFocusEvent* event)
                              {
                                  dse->setEnabled(true);
                                  QLineEdit::focusInEvent(event); 
                              }
                          
                              void focusOutEvent(QFocusEvent* event)
                              {
                                  dse->setEnabled(false);
                                  QLineEdit::focusOutEvent(event); 
                              }
                          
                          };
                          
                          1 Reply Last reply
                          0
                          • Christian EhrlicherC Offline
                            Christian EhrlicherC Offline
                            Christian Ehrlicher
                            Lifetime Qt Champion
                            wrote on last edited by
                            #16

                            Created a bug report for the problem when an enabled graphics effect gets deleted: https://bugreports.qt.io/browse/QTBUG-109165

                            On linux with Qt5.15 I can see the cursor even when the effect is applied.

                            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                            Visit the Qt Academy at https://academy.qt.io/catalog

                            K 1 Reply Last reply
                            1
                            • Christian EhrlicherC Christian Ehrlicher

                              Created a bug report for the problem when an enabled graphics effect gets deleted: https://bugreports.qt.io/browse/QTBUG-109165

                              On linux with Qt5.15 I can see the cursor even when the effect is applied.

                              K Offline
                              K Offline
                              Karine
                              wrote on last edited by
                              #17

                              @Christian-Ehrlicher i think the caret is disappearing because of the stylesheet, could you try with this:

                              QLineEdit
                              {	background-color: rgba(0, 0, 0, 190);
                              	border-radius: 12px;
                                      border: 1px solid rgb(22, 22, 22);
                              	padding-left: 10px;
                                      font-size: 12px;
                              	font-weight: 900;
                              	color: rgb(154, 154, 154);
                              }
                              
                              QLineEdit:hover 
                              {
                                     color: white; 
                              }
                              
                              1 Reply Last reply
                              0
                              • K Offline
                                K Offline
                                Karine
                                wrote on last edited by
                                #18

                                @Karine said in How to handle when an QLineEdit widget is selected and no longer selected:

                                background-color: rgba(0, 0, 0, 190);

                                Removing the alpha from the background, the caret is now visible, could it also be a bug?

                                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