Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. When to use QWidget x QFrame as container?
Forum Updated to NodeBB v4.3 + New Features

When to use QWidget x QFrame as container?

Scheduled Pinned Locked Moved Solved General and Desktop
11 Posts 3 Posters 4.2k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    Roberrt
    wrote on last edited by Roberrt
    #1

    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 two QScrollArea 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 as QWidget or QFrame?

    I'm not interested in frame borders, shadow, etc, aiming just performance, trying to find the best container for each grid case.

    Chris KawaC 1 Reply Last reply
    0
    • R Roberrt

      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.

      Chris KawaC Online
      Chris KawaC Online
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by
      #10

      @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).

      1 Reply Last reply
      1
      • R Roberrt

        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 two QScrollArea 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 as QWidget or QFrame?

        I'm not interested in frame borders, shadow, etc, aiming just performance, trying to find the best container for each grid case.

        Chris KawaC Online
        Chris KawaC Online
        Chris Kawa
        Lifetime Qt Champion
        wrote on last edited by
        #2

        @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.

        R 1 Reply Last reply
        3
        • Chris KawaC Chris Kawa

          @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.

          R Offline
          R Offline
          Roberrt
          wrote on last edited by Roberrt
          #3

          @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?

          SGaistS 1 Reply Last reply
          0
          • R Roberrt

            @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?

            SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #4

            @Roberrt hi,

            This stylesheet will apply the same on a QWidget.

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            R 1 Reply Last reply
            1
            • SGaistS SGaist

              @Roberrt hi,

              This stylesheet will apply the same on a QWidget.

              R Offline
              R Offline
              Roberrt
              wrote on last edited by Roberrt
              #5

              @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 of QFrames that could be QWidgets
              as they have nothing being drawn in their background, does it impact in something?

              SGaistS 1 Reply Last reply
              0
              • R Roberrt

                @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 of QFrames that could be QWidgets
                as they have nothing being drawn in their background, does it impact in something?

                SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #6

                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.

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                R 1 Reply Last reply
                2
                • SGaistS SGaist

                  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.

                  R Offline
                  R Offline
                  Roberrt
                  wrote on last edited by
                  #7

                  @SGaist The red rectangle thing was just an example, when you create a QLabel it also creates a QWidget?

                  Chris KawaC 1 Reply Last reply
                  0
                  • R Roberrt

                    @SGaist The red rectangle thing was just an example, when you create a QLabel it also creates a QWidget?

                    Chris KawaC Online
                    Chris KawaC Online
                    Chris Kawa
                    Lifetime Qt Champion
                    wrote on last edited by Chris Kawa
                    #8

                    @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.

                    1 Reply Last reply
                    2
                    • R Offline
                      R Offline
                      Roberrt
                      wrote on last edited by
                      #9

                      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.

                      Chris KawaC 1 Reply Last reply
                      0
                      • R Roberrt

                        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.

                        Chris KawaC Online
                        Chris KawaC Online
                        Chris Kawa
                        Lifetime Qt Champion
                        wrote on last edited by
                        #10

                        @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).

                        1 Reply Last reply
                        1
                        • R Offline
                          R Offline
                          Roberrt
                          wrote on last edited by
                          #11

                          Thank you all for the explanation, i was using QFrame in all cases, looks like there are cases that QWidget is a better choice, like when you dont plan to draw anything on its background neither use anything from a specific class as SGaist mentioned!

                          1 Reply Last reply
                          0
                          • R Roberrt has marked this topic as solved on

                          • Login

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • Users
                          • Groups
                          • Search
                          • Get Qt Extensions
                          • Unsolved