PyQt6: How to anchor a QLineEdit to the bottom right corner of a QMainWindow's central widget?
-
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:

-
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); -
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:

@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. -
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
resizeEventor install an eventFilter handlingQEvent::Resizeon it and call your QTextFieldmovefunction. -
W wayfarer has marked this topic as solved