Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. PyQt6: How to anchor a QLineEdit to the bottom right corner of a QMainWindow's central widget?

PyQt6: How to anchor a QLineEdit to the bottom right corner of a QMainWindow's central widget?

Scheduled Pinned Locked Moved Solved Qt for Python
qt for python
4 Posts 4 Posters 63 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.
  • W Offline
    W Offline
    wayfarer
    wrote last edited by wayfarer
    #1

    The app I'm working on has a QMainWindow layout, with a QTextBrowser as the central widget and a QToolBar and QStatusBar providing a toolbar and footer, respectively. I'm developing it entirely from source code, without any of those fancy GUI assembler apps.

    My goal is to anchor a QLineEdit to the bottom right corner of the QMainWindow's central widget, such that if the main window is moved or resized, the QLineEdit stays in the same place, and it's always shown on top.

    This QLineEdit is meant to be a temporary interface for user input, to be shown with show() when needed and hidden with hide() when it's not. Placing it above the footer is an intentional decision meant to prevent it from obscuring information on it.

    This screenshot shows what I'm trying to accomplish:

    Screenshot_20260209_151941.png

    JonBJ 1 Reply Last reply
    0
    • Axel SpoerlA Online
      Axel SpoerlA Online
      Axel Spoerl
      Moderators
      wrote last edited by SGaist
      #2

      Hi,
      I am not a pythonist, but the principle should be identical to C++.

      // Assuming that mainWindow points to the main window.
      QWidget *central = new QWidget;
      auto *edit = new QLineEdit;
      edit->setPlaceholderText("I want to show on the bottom right"); 
      auto *boxLayout = new QVBoxLayout;
      auto *bottomRow = new QHBoxLayout;
      boxLayout->addStretch(); // pushes widgets/sublayouts to the bottom
      bottomRow->addStretch(); // pushes widgets (line edit)  to the right
      bottomRow->addWidget(edit);
      boxLayout->addLayout(bottomRow);
      central->setLayout(boxLayout);
      mainWindow->setCentralWidget(central);
      

      Software Engineer
      The Qt Company, Oslo

      1 Reply Last reply
      2
      • W wayfarer

        The app I'm working on has a QMainWindow layout, with a QTextBrowser as the central widget and a QToolBar and QStatusBar providing a toolbar and footer, respectively. I'm developing it entirely from source code, without any of those fancy GUI assembler apps.

        My goal is to anchor a QLineEdit to the bottom right corner of the QMainWindow's central widget, such that if the main window is moved or resized, the QLineEdit stays in the same place, and it's always shown on top.

        This QLineEdit is meant to be a temporary interface for user input, to be shown with show() when needed and hidden with hide() when it's not. Placing it above the footer is an intentional decision meant to prevent it from obscuring information on it.

        This screenshot shows what I'm trying to accomplish:

        Screenshot_20260209_151941.png

        JonBJ Online
        JonBJ Online
        JonB
        wrote last edited by JonB
        #3

        @wayfarer said in PyQt6: How to anchor a QLineEdit to the bottom right corner of a QMainWindow's central widget?:

        My goal is to anchor a QLineEdit to the bottom right corner of the QMainWindow's central widget, such that if the main window is moved or resized, the QLineEdit stays in the same place, and it's always shown on top.

        This QLineEdit is meant to be a temporary interface for user input, to be shown with show() when needed and hidden with hide() when it's not.

        @Axel-Spoerl's suggestion means that the line edit is actually a part of the central widget's layout. E.g. it actually takes up "room" at least when shown (when hiding it you can choose whether it still takes up room or not). Other widgets get moved etc.

        The way you phrase your question you may prefer to have it as a "floating" widget on top of everything and at certain position, without it actually being part of any layout or causing other widgets to move etc. If so do not create a layout for it or put it on one; instead, just create it as a child of the central widget or whatever and absolute position it (relative to its parent) via QWidget.move(x: int, y: int). Calculate the position when it is about to be shown, so that takes care of the parent having been resized. (If you allow central widget resize while the line edit is displayed and want it to move then you need to override parent's resizeEvent().) You might also want to make it it a "popup", I don't know.

        1 Reply Last reply
        2
        • GrecKoG Offline
          GrecKoG Offline
          GrecKo
          Qt Champions 2018
          wrote last edited by
          #4

          in QML it would have been:

          ScrollView {
              TextArea {
                  TextField {
                      anchors {
                          right: parent.right
                          bottom: parent.bottom
                          margins: 16
                      }
                  }
              }
          }
          

          As said by JonB in widgets you could override the parent resizeEvent or install an eventFilter handling QEvent::Resize on it and call your QTextField move function.

          1 Reply Last reply
          1
          • W wayfarer has marked this topic as solved

          • Login

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