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 setStyleSheet() not working correctly when parent is MainWindow
Forum Updated to NodeBB v4.3 + New Features

QLineEdit setStyleSheet() not working correctly when parent is MainWindow

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 3 Posters 851 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.
  • K Offline
    K Offline
    Kevin Hoang
    wrote on last edited by
    #1

    Hi everyone,

    I have a QLineEdit, and I want it to work globally, meaning it should not be part of centralWidget() because that would interfere with event handling in MainWindow. However, when I set MainWindow as its parent, some setStyleSheet() properties related to size attributes (e.g., width, padding) do not work correctly.

    Example (not working as expected):

    QLineEdit *lineEdit = new QLineEdit(this); // 'this' is MainWindow
    lineEdit->setStyleSheet("padding: 1px 2px; width: 200px;");
    

    The padding does not shrink correctly.
    The width is not applied properly.

    Example (works, but is not an option for me):

    QLineEdit *lineEdit = new QLineEdit(this->centralWidget());
    lineEdit->setStyleSheet("padding: 1px 2px; width: 200px;");
    

    When the parent is centralWidget(), the style works fine, but this is not a solution for me because: The focus is now inside centralWidget(), which prevents MainWindow from receiving key events related to QLineEdit.

    Question:
    How can I make setStyleSheet() work correctly for QLineEdit without putting it inside centralWidget()?
    Is there any Qt-specific behavior that prevents style properties from being applied properly when QLineEdit is a direct child of MainWindow?

    I would really appreciate any insights! Thanks.

    Pl45m4P 1 Reply Last reply
    0
    • K Kevin Hoang

      Hi everyone,

      I have a QLineEdit, and I want it to work globally, meaning it should not be part of centralWidget() because that would interfere with event handling in MainWindow. However, when I set MainWindow as its parent, some setStyleSheet() properties related to size attributes (e.g., width, padding) do not work correctly.

      Example (not working as expected):

      QLineEdit *lineEdit = new QLineEdit(this); // 'this' is MainWindow
      lineEdit->setStyleSheet("padding: 1px 2px; width: 200px;");
      

      The padding does not shrink correctly.
      The width is not applied properly.

      Example (works, but is not an option for me):

      QLineEdit *lineEdit = new QLineEdit(this->centralWidget());
      lineEdit->setStyleSheet("padding: 1px 2px; width: 200px;");
      

      When the parent is centralWidget(), the style works fine, but this is not a solution for me because: The focus is now inside centralWidget(), which prevents MainWindow from receiving key events related to QLineEdit.

      Question:
      How can I make setStyleSheet() work correctly for QLineEdit without putting it inside centralWidget()?
      Is there any Qt-specific behavior that prevents style properties from being applied properly when QLineEdit is a direct child of MainWindow?

      I would really appreciate any insights! Thanks.

      Pl45m4P Offline
      Pl45m4P Offline
      Pl45m4
      wrote on last edited by Pl45m4
      #2

      @Kevin-Hoang said in QLineEdit setStyleSheet() not working correctly when parent is MainWindow:

      it should not be part of centralWidget() because that would interfere with event handling in MainWindow.

      Looks like a design issue.
      Why making the QLineEdit a child of `QMainWindow anyway? You could make it a toplevel widget and still communicate with the main window.

      Is there any Qt-specific behavior that prevents style properties from being applied properly when QLineEdit is a direct child of MainWindow?

      QSS is inherited/propagated to child QWidgets. Depending on what your centralWidget() is, it does make a difference whether using a probably custom widget or a QMainWindow as direct parent.


      If debugging is the process of removing software bugs, then programming must be the process of putting them in.

      ~E. W. Dijkstra

      1 Reply Last reply
      0
      • K Offline
        K Offline
        Kevin Hoang
        wrote on last edited by
        #3

        I understand that making QLineEdit a child of QMainWindow might not be the best design choice, but for my specific use case, it is the most practical and easiest to manage. My goal is for MainWindow to have full control over QLineEdit (e.g., showing/hiding it via keypress). If I make it a top-level widget, it behaves like a new window, which prevents MainWindow from controlling it as expected—just like when it is placed inside centralWidget.

        In the end, I found a trick that allows QLineEdit to be displayed correctly while still letting MainWindow control it seamlessly.

        JonBJ Pl45m4P 2 Replies Last reply
        0
        • K Kevin Hoang

          I understand that making QLineEdit a child of QMainWindow might not be the best design choice, but for my specific use case, it is the most practical and easiest to manage. My goal is for MainWindow to have full control over QLineEdit (e.g., showing/hiding it via keypress). If I make it a top-level widget, it behaves like a new window, which prevents MainWindow from controlling it as expected—just like when it is placed inside centralWidget.

          In the end, I found a trick that allows QLineEdit to be displayed correctly while still letting MainWindow control it seamlessly.

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

          @Kevin-Hoang
          I have no idea why you want to make your QLineEdit a direct child of a QMainWindow, which is what you say you need to do. It should be a child of QMainWindow::centralWidget(). And probably that should be a QWidget which some QLayout on it, and you add your QLineEdit onto that layout. new QLineEdit(this->centralWidget()) is not right, you should place some kind of QLayout onto whatever your centralWidget() is before adding the QLineEdit onto that, not onto contralWidget() directly.

          I do not recognise "My goal is for MainWindow to have full control over QLineEdit (e.g., showing/hiding it via keypress)" implying that your MainWindow has "less control" over the QLineEdit if it has the QMainWindow as its direct parent versus placing it on the centralWidget() (with a suitable QLayout there, as stated).

          1 Reply Last reply
          1
          • K Kevin Hoang

            I understand that making QLineEdit a child of QMainWindow might not be the best design choice, but for my specific use case, it is the most practical and easiest to manage. My goal is for MainWindow to have full control over QLineEdit (e.g., showing/hiding it via keypress). If I make it a top-level widget, it behaves like a new window, which prevents MainWindow from controlling it as expected—just like when it is placed inside centralWidget.

            In the end, I found a trick that allows QLineEdit to be displayed correctly while still letting MainWindow control it seamlessly.

            Pl45m4P Offline
            Pl45m4P Offline
            Pl45m4
            wrote on last edited by Pl45m4
            #5

            @Kevin-Hoang said in QLineEdit setStyleSheet() not working correctly when parent is MainWindow:

            My goal is for MainWindow to have full control over QLineEdit (e.g., showing/hiding it via keypress).

            You can do this easily, but you don't need any weird design for it.
            What you do / want to do with forcing the QLineEdit into QMainWindow's coordinate system makes no sense.
            Create a separate QWidget for/with the said QLineEdit like @JonB mentions above or re-design your idea.

            Btw: Depending on what you are doing, the MainWindow has full control over the QLineEdit

            In the end, I found a trick that allows QLineEdit to be displayed correctly while still letting MainWindow control it seamlessly.

            Maybe you want to share your "trick" with us (and later readers) ?!


            If debugging is the process of removing software bugs, then programming must be the process of putting them in.

            ~E. W. Dijkstra

            1 Reply Last reply
            0
            • K Offline
              K Offline
              Kevin Hoang
              wrote on last edited by
              #6

              Thank you all for your input! I’d like to share my trick: I initially declare the QLineEdit with centralWidget as the parent. Once it is displayed, I change its parent to MainWindow, and everything works as expected.

              I guess another approach could be to create a custom top-level widget and place QLineEdit inside it, but that feels like an overcomplicated solution just for a single QLineEdit. Managing styles, event handling, and other behaviors would add unnecessary complexity. Don't you think so? 😄

              Pl45m4P 1 Reply Last reply
              0
              • K Kevin Hoang

                Thank you all for your input! I’d like to share my trick: I initially declare the QLineEdit with centralWidget as the parent. Once it is displayed, I change its parent to MainWindow, and everything works as expected.

                I guess another approach could be to create a custom top-level widget and place QLineEdit inside it, but that feels like an overcomplicated solution just for a single QLineEdit. Managing styles, event handling, and other behaviors would add unnecessary complexity. Don't you think so? 😄

                Pl45m4P Offline
                Pl45m4P Offline
                Pl45m4
                wrote on last edited by
                #7

                @Kevin-Hoang said in QLineEdit setStyleSheet() not working correctly when parent is MainWindow:

                I initially declare the QLineEdit with centralWidget as the parent. Once it is displayed, I change its parent to MainWindow, and everything works as expected.

                That's just weird.
                The widget-way is the better approach.


                If debugging is the process of removing software bugs, then programming must be the process of putting them in.

                ~E. W. Dijkstra

                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