Qt Designer中如何重置继承自centralwidget的控件背景为默认
Hey there! I’ve dealt with this exact Qt styling quirk before—super annoying when all your buttons and labels end up with the parent widget’s background image. Let’s walk through the best solutions to get your controls back to their default look:
1. Use a Specific Selector for the Central Widget
The root issue here is that your original stylesheet applies to all QWidget-derived widgets (since centralwidget is a QWidget), and child controls inherit this rule. To fix this, target only the central widget itself using its unique object name in the stylesheet:
QWidget#centralwidget { border-image: url(:/Resources/MyImage.jpg); }
Adding #centralwidget tells Qt to apply this style exclusively to the widget with the object name "centralwidget", so none of its child controls will inherit the background image.
2. Reset Individual Controls to Default Styles
If you’ve already applied the parent stylesheet and need to fix specific widgets, you can override their styles to revert to defaults:
- Right-click the control (e.g., a
QPushButtonorQLabel) → Change Style Sheet - For a fully default look, leave the stylesheet blank and click OK.
- If you want the control to have a transparent background (but not the parent’s image), use:
background-color: transparent; - To explicitly use the system’s default background color, leverage Qt’s palette:
QPushButton { background-color: palette(base); }
3. Enable autoFillBackground for Child Widgets
Many Qt controls don’t automatically fill their background by default, which lets the parent’s background show through. Enabling this property forces the control to use its own default background:
- Select the child control in Qt Designer.
- Open the Property Editor (usually on the right side).
- Scroll down to the
autoFillBackgroundproperty and check the box.
This will make the control draw its own default background, covering up the parent’s image.
4. Use a Background Container Widget (Cleaner Layout Approach)
For a more scalable solution, especially if you have lots of controls:
- Add a new
QWidgetto yourcentralwidget(call itbackgroundWidget). - Set
backgroundWidgetto fill the entirecentralwidget(use a layout or adjust its geometry). - Apply your background image stylesheet only to
backgroundWidget:QWidget#backgroundWidget { border-image: url(:/Resources/MyImage.jpg); } - Move all your existing controls (buttons, labels, etc.) into
centralwidget(notbackgroundWidget), then setbackgroundWidgetto the lowest Z-order (right-click → Send to Back).
This way, the background is a separate layer, and your controls don’t inherit any styles from it.
内容的提问来源于stack exchange,提问作者Abderrahman Faouzi

