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 in focus
QtWS25 Last Chance

QlineEdit in focus

Scheduled Pinned Locked Moved Solved General and Desktop
qlineditfocusbackground colo
8 Posts 4 Posters 8.0k 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.
  • G Offline
    G Offline
    gabor53
    wrote on last edited by
    #1

    Hi,
    I would like to change the background color of QLineEdit when it is in focus. Can somebody please show me which is the best way of doing it?

    Thank you.

    RatzzR 1 Reply Last reply
    0
    • jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      You can use setStyleSheet(const QString & styleSheet), for a style sheet description see: http://doc.qt.io/qt-5/stylesheet.html

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • G gabor53

        Hi,
        I would like to change the background color of QLineEdit when it is in focus. Can somebody please show me which is the best way of doing it?

        Thank you.

        RatzzR Offline
        RatzzR Offline
        Ratzz
        wrote on last edited by
        #3

        @gabor53
        You may also try

            QPalette palette;
            palette.setColor(QPalette::Base,Qt::black);
            palette.setColor(QPalette::Text,Qt::white);
            ui->lineEdit->setPalette(palette);

        --Alles ist gut.

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

          How can I know that lineEdit is in focus?
          Thnks.

          ValentinMicheletV 1 Reply Last reply
          0
          • G gabor53

            How can I know that lineEdit is in focus?
            Thnks.

            ValentinMicheletV Offline
            ValentinMicheletV Offline
            ValentinMichelet
            wrote on last edited by ValentinMichelet
            #5

            Hi,

            Either connect the QApplication signal focusChanged() to your slot:
            http://doc.qt.io/qt-5/qapplication.html#focusChanged

            Or reimplement the focusInEvent() method in a custom MyLineEdit, that inherits from QLineEdit, and promote your QLineEdit in QtDesigner using MyLineEdit.
            http://doc.qt.io/qt-5/qwidget.html#focusInEvent

            Or you can use style sheet as @jsulm suggested:
            setStyleSheet("QLineEdit:focus {...}");

            If you want only one QLineEdit to have a specific behavior, then give it a name with setObjectName() or through QtDesigner, and use it in the style sheet string:
            setStyleSheet("QLineEdit:focus#myLineEdit {...}");

            But I realize that I'm giving you different options without telling you what's the best one. I'd say style sheet, but only because it seems easier. I don't really know the mechanisms behind every solution.

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

              Thank you, it worked with LineEdit. Should this work with QComboBox too? I tried

              setStyleSheet("QComboBox:focus#comboBox{background-color: rgb(170, 255, 127)}");
              

              but nothing happened.
              Thank you for all your help.

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

                Each time I need to apply a style sheet, I read the documentation first.

                Here is the list of pseudo states:
                http://doc.qt.io/qt-5/stylesheet-reference.html#list-of-pseudo-states
                And it is said:

                :focus The item has input focus.

                I don't really know what "input focus" means for QCombBox, maybe it's when it's editable and its line edit has the focus. So then I'd go see how they style a QComboBox in examples:
                http://doc.qt.io/qt-5/stylesheet-examples.html#customizing-qcombobox

                I recommend you to read carefully these topics:
                http://doc.qt.io/qt-5/stylesheet.html
                http://doc.qt.io/qt-5/stylesheet-syntax.html
                http://doc.qt.io/qt-5/stylesheet-designer.html (optional)
                http://doc.qt.io/qt-5/stylesheet-customizing.html
                http://doc.qt.io/qt-5/stylesheet-reference.html
                http://doc.qt.io/qt-5/stylesheet-examples.html

                After reading them, you'll have a global sight of what are style sheets in Qt, how to use them properly, and what can be done and can't.

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

                  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