When to use QWidget x QFrame as container?
-
Given two QGridLayout, on grid_1, I'm adding multiples widgets containing nothing more than a
QLabel
on grid_2, I'm also adding multiple widgets, but in this case they contain twoQScrollArea
and some other widgets, like buttons, label.As I'm creating a large amount of widgets on these grids, I would like to ask which container i should use
on grid_1 and grid_2?
I should be creating the containers asQWidget
orQFrame
?I'm not interested in frame borders, shadow, etc, aiming just performance, trying to find the best container for each grid case.
-
I see, I'm searching about and looks like QWidget can become a top level window a QFrame doesnt, I thought depending of the widget type, like a QLabel, it could have any kind of modification that could make it "lower overhead" when compared to a simple QWidget.
@Roberrt said:
looks like QWidget can become a top level window a QFrame doesnt
I don't know where you got that idea from, but that's not true. Like I said - all widgets are derived from QWidget, so any widget can be top level.
I thought depending of the widget type, like a QLabel, it could have any kind of modification that could make it "lower overhead"
QWidget is the lowest in the inheritance hierarchy and it doesn't have any additional functionality. There's nothing to cut down from it. All other widgets add functionality to it, so all other widgets have some overhead over it (marginal in some cases).
-
Given two QGridLayout, on grid_1, I'm adding multiples widgets containing nothing more than a
QLabel
on grid_2, I'm also adding multiple widgets, but in this case they contain twoQScrollArea
and some other widgets, like buttons, label.As I'm creating a large amount of widgets on these grids, I would like to ask which container i should use
on grid_1 and grid_2?
I should be creating the containers asQWidget
orQFrame
?I'm not interested in frame borders, shadow, etc, aiming just performance, trying to find the best container for each grid case.
@Roberrt QFrame is derived from QWidget and has a visual representation. It can be borders and/or different background color, depending on the style you use. If all you want is a container without any sort of visual representation a plain QWidget is the way to go.
-
@Roberrt QFrame is derived from QWidget and has a visual representation. It can be borders and/or different background color, depending on the style you use. If all you want is a container without any sort of visual representation a plain QWidget is the way to go.
@Chris-Kawa So if i don't need any kind of background/border and just a container is better to choose QWidget?
This is also valid for a
QLabel
?I mean, give a QWidget which i'm using exclusively just to draw a "red rectangle", something like:
void Widget::paintEvent(QPaintEvent* event) { QPainter painter(this); painter.setRenderHint(QPainter::Antialiasing, true); QPainterPath path; path.addRect(rect()); painter.fillPath(path, QColor(Qt::red)); }
Will it use less resource than a
QLabel
?QLabel *label = new QLabel() label->setStyleSheet("background-color: red;");
Or in this case QLabel is a better option as its not a container and i'm not going to add anything inside it?
-
@Chris-Kawa So if i don't need any kind of background/border and just a container is better to choose QWidget?
This is also valid for a
QLabel
?I mean, give a QWidget which i'm using exclusively just to draw a "red rectangle", something like:
void Widget::paintEvent(QPaintEvent* event) { QPainter painter(this); painter.setRenderHint(QPainter::Antialiasing, true); QPainterPath path; path.addRect(rect()); painter.fillPath(path, QColor(Qt::red)); }
Will it use less resource than a
QLabel
?QLabel *label = new QLabel() label->setStyleSheet("background-color: red;");
Or in this case QLabel is a better option as its not a container and i'm not going to add anything inside it?
-
@SGaist hi, my doubt is the better widget to choose in the given cases.
For example, when you are resizing a GUI that contain a lot ofQFrames
that could beQWidgets
as they have nothing being drawn in their background, does it impact in something? -
@SGaist hi, my doubt is the better widget to choose in the given cases.
For example, when you are resizing a GUI that contain a lot ofQFrames
that could beQWidgets
as they have nothing being drawn in their background, does it impact in something?Unless you have specific needs for a class feature, just use QWidget.
If you are worried about performance of showing lots of widgets that are in the end painted rectangles, you should maybe consider using something like the Graphics View Framework.
-
Unless you have specific needs for a class feature, just use QWidget.
If you are worried about performance of showing lots of widgets that are in the end painted rectangles, you should maybe consider using something like the Graphics View Framework.
-
@SGaist The red rectangle thing was just an example, when you create a QLabel it also creates a QWidget?
@Roberrt Every widget is a class derived from QWidget, QLabel too. This means every widget does something extra on top of the base QWidget.
For the lowest overhead QWidget is the lowest in the inheritance hierarchy, so it will do the least of all widgets. QLabel does text layout and adjusts its size constraints based on contained text. If you don't need that functionality i.e. don't plan on showing any text in it then don't use it.
As for stylesheet - as SGaist said, you can set it on a plain QWidget too. You don't need a QLabel to be able to use stylesheets. Wheter a stylesheet will be faster or a custom widget with overriden paintEvent depends on what you plan to paint. There's no way for us to tell you. Measure.
As for containers - there's not really anything like that in widgets. It's more just how you use it. You can add a child widget to QWidget, QFrame, QLabel, QPushButton or any other widget. They are all containers in that sense. It's just that plain QWidget doesn't do any extra drawing in its area so it's a good base class for grouping, but there's no limitations on which widget type can have children. They all can.
-
I see, I'm searching about and looks like QWidget can become a top level window a QFrame doesnt, I thought depending of the widget type, like a QLabel, it could have any kind of modification that could make it "lower overhead" when compared to a simple QWidget.
-
I see, I'm searching about and looks like QWidget can become a top level window a QFrame doesnt, I thought depending of the widget type, like a QLabel, it could have any kind of modification that could make it "lower overhead" when compared to a simple QWidget.
@Roberrt said:
looks like QWidget can become a top level window a QFrame doesnt
I don't know where you got that idea from, but that's not true. Like I said - all widgets are derived from QWidget, so any widget can be top level.
I thought depending of the widget type, like a QLabel, it could have any kind of modification that could make it "lower overhead"
QWidget is the lowest in the inheritance hierarchy and it doesn't have any additional functionality. There's nothing to cut down from it. All other widgets add functionality to it, so all other widgets have some overhead over it (marginal in some cases).
-
-