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. I can't find
QtWS25 Last Chance

I can't find

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 3 Posters 347 Views
  • 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.
  • D Offline
    D Offline
    Duy Khang
    wrote on last edited by
    #1

    How can I change stylesheet of a line edit when I click into itself or how can i get mouse event . Thanks!

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by mrjj
      #2

      Hi

      You have to create a custom class that inherits QLineedit and
      then you override
      https://doc.qt.io/qt-5/qwidget.html#mousePressEvent

      Depending on what you want its might also be possible to some degree with stylesheet but
      that more for hover events. (holding mouse over the lineedit)

      class MyLineEdit : public QLineEdit
      {
          Q_OBJECT
      public:
          explicit MyLineEdit(QWidget *parent = nullptr)
              : QLineEdit(parent) {}
      protected:
          void mousePressEvent(QMouseEvent *event) override
          {
              QLineEdit::mousePressEvent(event); // call the base to allow normal operation too
              setStyleSheet(xxx);
          }
      
      };
      
      

      You could also use an eventfiler to catch events.
      https://doc.qt.io/qt-5/eventsandfilters.html

      Then you dont need the subclass but its get a bit messy if you have many LineEdits etc and its not as self-contained as the subclass.

      D 1 Reply Last reply
      1
      • mrjjM mrjj

        Hi

        You have to create a custom class that inherits QLineedit and
        then you override
        https://doc.qt.io/qt-5/qwidget.html#mousePressEvent

        Depending on what you want its might also be possible to some degree with stylesheet but
        that more for hover events. (holding mouse over the lineedit)

        class MyLineEdit : public QLineEdit
        {
            Q_OBJECT
        public:
            explicit MyLineEdit(QWidget *parent = nullptr)
                : QLineEdit(parent) {}
        protected:
            void mousePressEvent(QMouseEvent *event) override
            {
                QLineEdit::mousePressEvent(event); // call the base to allow normal operation too
                setStyleSheet(xxx);
            }
        
        };
        
        

        You could also use an eventfiler to catch events.
        https://doc.qt.io/qt-5/eventsandfilters.html

        Then you dont need the subclass but its get a bit messy if you have many LineEdits etc and its not as self-contained as the subclass.

        D Offline
        D Offline
        Duy Khang
        wrote on last edited by
        #3

        @mrjj for an example, I have many lineEdit object: lineEditname, lineEditage, lineEditaddress, lineEditPhoneNumber and I want when click one of them, it will change styleSheet of its. How can I do it?

        JonBJ 1 Reply Last reply
        0
        • D Duy Khang

          @mrjj for an example, I have many lineEdit object: lineEditname, lineEditage, lineEditaddress, lineEditPhoneNumber and I want when click one of them, it will change styleSheet of its. How can I do it?

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

          @Duy-Khang
          So if you want to do that make all your line edits be @mrjj's MyLineEdit, then the code he shows there will apply to all of them

          D 1 Reply Last reply
          1
          • JonBJ JonB

            @Duy-Khang
            So if you want to do that make all your line edits be @mrjj's MyLineEdit, then the code he shows there will apply to all of them

            D Offline
            D Offline
            Duy Khang
            wrote on last edited by
            #5

            @JonB thank you

            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