QLineEdit setStyleSheet() not working correctly when parent is MainWindow
-
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.
-
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.
@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 theQLineEdita 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 yourcentralWidget()is, it does make a difference whether using a probably custom widget or aQMainWindowas direct parent. -
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.
-
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.
@Kevin-Hoang
I have no idea why you want to make yourQLineEdita direct child of aQMainWindow, which is what you say you need to do. It should be a child ofQMainWindow::centralWidget(). And probably that should be aQWidgetwhich someQLayouton it, and you add yourQLineEditonto that layout.new QLineEdit(this->centralWidget())is not right, you should place some kind ofQLayoutonto whatever yourcentralWidget()is before adding theQLineEditonto that, not ontocontralWidget()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
MainWindowhas "less control" over theQLineEditif it has theQMainWindowas its direct parent versus placing it on thecentralWidget()(with a suitableQLayoutthere, as stated). -
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.
@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 theQLineEditintoQMainWindow's coordinate system makes no sense.
Create a separateQWidgetfor/with the saidQLineEditlike @JonB mentions above or re-design your idea.Btw: Depending on what you are doing, the
MainWindowhas full control over theQLineEditIn 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) ?!
-
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? 😄
-
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? 😄
@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.