Why QWidget behaves differently when it is inherited - python

When I use inherited QWidget, the margins and spacing have NO background color unlike using QWidget directly (the code is basically the same).
Pure QWidget:
class App(QWidget):
def __init__(self):
super().__init__()
self.start()
def start(self):
self.layout = QHBoxLayout()
self.layout.setContentsMargins(0, 0, 0, 0)
self.setLayout(self.layout)
self.setGeometry(10, 10, 500, 100)
grid_layout = QGridLayout()
grid_layout.setSpacing(10)
widget = QWidget()
widget.setLayout(grid_layout)
widget.setStyleSheet('background: green')
grid_layout.addWidget(QLabel("first"), 0, 0)
grid_layout.addWidget(QLabel("second"), 0, 1)
grid_layout.addWidget(QLabel("third"), 0, 2)
self.layout.addWidget(widget)
self.show()
if __name__ == "__main__":
app = QApplication(sys.argv)
ex = App()
#ex.start_card_holder()
sys.exit(app.exec_())
Inherited QWidget:
class App(QWidget):
....
class MainWidget(QWidget):
def __init__(self):
QWidget.__init__(self)
def start(self):
...
widget = App.MainWidget()
...
Can anyone tell what I did wrong?

The following concepts must be clear to understand the behavior:
By default the classes that inherit from QWidget do not implement the painting based on the Qt QStyleSheet as indicated by the following questions:
Qt Stylesheet for custom widget
CSS Stylesheet is not applied to custom QWidget
Set background colour for a custom QWidget
By setting the "background: green" you are indicating that the widget takes that color if it is enabled and its children.
In your case the "widget" if it is a QWidget then it will be painted but if it is a MainWidget no.
To check what I indicate, it is only necessary to enable the background color using one of the answers to the questions indicated above:
# ...
widget = App.MainWidget()
widget.setAttribute(Qt.WA_StyledBackground, True)
# ...

Related

Can I have a tab control in a widget contained in a layout?

I am trying to make a programmable sequencer where I can configure objects that I've programmed and create a list of them to run. That requires me to instantiate that object by defining it's properties and I'd like to make a tab control on the blue widget shown below that has an inputs and outputs tab.
Anytime I try to implement this, it replaces all of the other widgets:
Surely that has to be a way to use a standard tab control in a widget right?
Code below:
import sys
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QApplication, QGridLayout, QLabel, QMainWindow, QWidget, QTabWidget, QVBoxLayout
from PyQt5.QtGui import QColor, QPalette
# Placeholder widget for setting up layout
class Color(QWidget):
def __init__(self, color):
super().__init__()
self.setAutoFillBackground(True)
palette = self.palette()
palette.setColor(QPalette.Window, QColor(color))
self.setPalette(palette)
# Creating tab widgets
class MyTabWidget(QTabWidget):
def __init__(self):
super().__init__()
self.setTabPosition(QTabWidget.North)
for n, color in enumerate(['blue', 'purple']):
self.addTab(Color(color), color)
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("My App")
layout = QGridLayout()
# 1
layout.addWidget(Color("red"), 0, 0, 2, 1)
# 2
layout.addWidget(Color("yellow"), 2, 0, 3, 1)
# 3
layout.addWidget(Color("green"), 0, 1, 5, 4)
# 4
# layout.addWidget(Color("blue"), 0, 5, 5, 1)
tabs = MyTabWidget()
layout.addWidget(tabs, 0, 5, 5, 1)
widget = QWidget()
widget.setLayout(layout)
self.setCentralWidget(widget)
app = QApplication(sys.argv)
window = MainWindow()
window.show()
app.exec_()
The size policy of the QTabWidget is set to stretch and since the stretch factors of the QGridLayout items is 0 then it will cause the items to be compressed. The solution is to set the stretch factors to 1:
widget = QWidget()
widget.setLayout(layout)
self.setCentralWidget(widget)
for column in range(layout.columnCount()):
layout.setColumnStretch(column, 1)
for row in range(layout.rowCount()):
layout.setRowStretch(row, 1)

QPaintEvent event rect for scrollable widget

I have a QPaintEvent override for a custom widget that has a fixed size set. This fixed size can change per instance but in this simple example, ive set it. however the PaintEvent doesn't take it into account so when the users scrolls to the right the rectangle shouldn't paint rounded corners since the widget extends past the visible viewport. How do i fix this?
Full widget painted correctly...
When i resize dialog and scroll right, you'll see rounded corners appear on the left side... when it should NOT.
They should look like this...
Code
import os
import sys
from PySide2 import QtGui, QtWidgets, QtCore, QtSvg
class Card(QtWidgets.QWidget):
def __init__(self, parent=None):
super(Card, self).__init__(parent=parent)
self.label = QtWidgets.QLabel('Help This Paint Event Is Broken')
self.label.setFixedHeight(40)
self.label.setFixedWidth(300)
self.mainLayout = QtWidgets.QVBoxLayout(self)
self.mainLayout.addWidget(self.label)
# overrides
def paintEvent(self, event):
painter = QtGui.QPainter()
painter.begin(self)
painter.setOpacity(1.0)
painter.setRenderHints(QtGui.QPainter.Antialiasing)
painter.setPen(QtGui.QColor(0, 0, 0, 128))
painter.setPen(QtCore.Qt.NoPen)
painter.setBrush(QtGui.QColor('#F44336'))
painter.drawRoundedRect(event.rect(), 12, 12)
painter.setRenderHint(QtGui.QPainter.Antialiasing)
painter.end()
class ListViewExample(QtWidgets.QWidget):
def __init__(self, parent=None):
super(ListViewExample, self).__init__(parent)
self.resize(200,200)
self.listView = QtWidgets.QListWidget()
self.listView.setSpacing(10)
self.listView.setVerticalScrollMode(QtWidgets.QAbstractItemView.ScrollPerPixel)
self.listView.verticalScrollBar().setSingleStep(10)
# layout
self.mainLayout = QtWidgets.QVBoxLayout()
self.mainLayout.setContentsMargins(0,0,0,0)
self.mainLayout.addWidget(self.listView)
self.setLayout(self.mainLayout)
for x in range(50):
wgt = Card()
self.appendItem(wgt)
def appendItem(self, widget):
lwi = QtWidgets.QListWidgetItem()
lwi.setSizeHint(widget.sizeHint())
self.listView.addItem(lwi)
self.listView.setItemWidget(lwi, widget)
################################################################################
# Widgets
################################################################################
def unitTest_CardDelegate():
app = QtWidgets.QApplication(sys.argv)
window = ListViewExample()
window.show()
app.exec_()
if __name__ == '__main__':
pass
unitTest_CardDelegate()
QPaintEvent::rect() returns the visible rectangle, not the rectangle of the widget itself, so you observe this behavior. The solution is:
painter.drawRoundedRect(self.rect(), 12, 12)

Custom widget background color in pyside2

Trying to work out how to set the background color in a QWidget. Here is my code:
class ParentTester(QWidget):
def __init__(self, parent=None):
super().__init__(parent)
left = ColorTester(self)
right = QFrame(self)
right.setFrameStyle(QFrame.Panel | QFrame.Sunken)
layout = QHBoxLayout()
layout.addWidget(left)
layout.addWidget(right)
self.setLayout(layout)
class ColorTester(QWidget):
def __init__(self, parent=None):
super().__init__(parent)
palette = self.palette()
palette.setColor(QPalette.Window, QColor(128, 0, 0))
self.setPalette(palette)
def main():
import sys
from PySide2.QtWidgets import QApplication
app = QApplication([])
works = True
if works:
win = ColorTester()
else:
win = ParentTester()
win.show()
win.resize(640, 480)
sys.exit(app.exec_())
if __name__ == '__main__':
main()
This works if I create the class as a topmost window. However if I make it a child of another control, the background color goes back to the default. Some of the other color roles do take effect, but not the background color. Not only that, but the colors get passed through to child controls.
How can I change the background color of a control but not its child controls?
By default, the child widgets take the color of the window, so you observe that effect, if you want the custom background color to be used then you must enable the autoFillBackground property:
class ColorTester(QWidget):
def __init__(self, parent=None):
super().__init__(parent)
palette = self.palette()
palette.setColor(QPalette.Window, QColor(128, 0, 0))
self.setPalette(palette)
self.setAutoFillBackground(True)
I finally settled on overriding paintEvent for my widget. Setting the color in the palette always seems to pass the color down to child controls, which is not what I wanted. Here is an example of what worked for me. This is a QFrame that takes the default background color and darkens and green-shifts it slightly.
class GreenFrame(QFrame):
def __init__(self, parent=None):
super().__init__(parent)
r, g, b, a = self.palette().color(QPalette.Window).toTuple()
self._bgcolor = QColor(r * 7 // 8, g, b * 7 // 8)
def paintEvent(self, event):
painter = QPainter(self)
painter.fillRect(event.rect(), self._bgcolor)
super().paintEvent(event)

Prevent clipping the absolute positioned child widget by the parent

I'm trying to create a layout where is existing a row, and this row should contain an absolute positioned button which should be placed outside of this row.
Here is the simple schema
I did it by just pushing a child button into the parent button (I'm not sure that it's a correct solution) and moved it to some absolute coordinates.
It works but, unfortunately, the child button is clipping by the parent. So it's like overflow: hidden in CSS. But in case of QT I couldn't found how to disable this behavior.
Here is the demo of my current QUI
Is there exists any way to solve it? Or should I just use some widget combination with the empty spacer etc.?
btn = QPushButton("button")
test = QPushButton("X")
test.setParent(btn)
test.move(200, 5)
self.layout.addWidget(btn)
Full code of the UI class (minimal reproducible example)
class MainWindow(QWidget):
def __init__(self):
super(MainWindow, self).__init__()
self.layout = QVBoxLayout()
btn = QPushButton("button")
test = QPushButton("X")
test.setParent(btn)
test.move(200, 5)
self.layout.addWidget(btn)
self.setLayout(self.layout)
self.layout.setContentsMargins(0,0,0,74)
self.layout.setSpacing(0)
# self.layout.addStretch(-1)
self.setMinimumSize(640,400)
self.setWindowFlags(Qt.FramelessWindowHint)
Sorry, but the advice of #Heike is absolutely correct and you should not look for wrong solutions.
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
class MainWindow(QWidget):
def __init__(self):
super(MainWindow, self).__init__()
self.setMinimumSize(640,400)
self.setWindowFlags(Qt.FramelessWindowHint)
btn = QPushButton("button")
test = QPushButton("X")
test.setParent(btn)
# test.move(200, 5)
# self.layout = QVBoxLayout()
self.layout = QGridLayout()
self.layout.addWidget(btn, 0, 0, 1, 10)
self.layout.addWidget(test, 0, 11, 1, 1)
self.layout.setContentsMargins(0,0,0,74)
self.layout.setSpacing(0)
self.setLayout(self.layout)
if __name__ == '__main__':
import sys
application = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(application.exec_())

Cannot create QScrollArea with QWidget and QVBoxLayout to QWidget with QVBoxLayout

I have this code:
class Window(QWidget):
def __init__(self):
super().__init__()
def init_gui(self):
self.layout = QVBoxLayout()
self.setLayout(self.layout)
self.new1()
self.new2()
self.showMaximized()
def create_scroll_area(self):
scroll_area = QScrollArea()
widget = QWidget()
scroll_area.setWidget(widget)
layout = QVBoxLayout()
widget.setLayout(layout)
button = QPushButton("Ahoj")
layout.addWidget(button)
self.layout.addLayout(layout)
def new1(self):
self.create_scroll_area()
def new2(self):
self.create_scroll_area()
I get this error message:
QLayout::addChildLayout: layout "" already has a parent
What's wrong?
Who is layout's parent? Widget? I also tried self.widget instead of widget and it still does not work.
Please try this code.
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
import sys
class Window(QWidget):
def __init__(self, parent=None):
super().__init__(parent=None)
self.init_gui()
def init_gui(self):
self.create_scroll_area()
self.showMaximized()
def create_scroll_area(self):
scroll_area = QScrollArea()
widget = QWidget()
layout = QVBoxLayout()
button = QPushButton("Ahoj")
layout.addWidget(button)
widget.setLayout(layout)
scroll_area.setWidget(widget)
self.setLayout(layout)
def main():
app = QApplication([])
window = Window()
window.show()
sys.exit(app.exec_())
if __name__ == "__main__":
main()
Let's revise from A to Z.
1 to write self.init_gui() in __init__ constructor.
If you don't do it, you can't execute init_gui method at the first time.
2.setLayout() or setWidget() should be written at the last place at least.
In python, we prepare the things we want to show, and set them on the mainwidget, and show them at the last time.
3.please pay attention to self.layout name.
Widget has originally setLayout() method. and layout() method.
If you make self.layout = ***, you crush the original method of QWidget.
4. it may as well delete new1 and new2 method.
please call them directly.
5. please look create_scroll_area method.
You make three widget.QScrollArea,QWidget,QPushButton.
and make a layout object.and you set the layout into QWidget.
But you set the QWidget before the widget set the layout.
It is not good order for coding.
You make QPushButton but the button doesn't belong to any widget.
Because you set the button on the self.layout certainly, but if you want to show it, you must setLayout(self.layout) at the last position.

Categories

Resources