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. Is there a maximum number of QLabels in a screen?

Is there a maximum number of QLabels in a screen?

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 3 Posters 1.2k Views
  • 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.
  • T Offline
    T Offline
    TomNow99
    wrote on last edited by TomNow99
    #1

    Hello,
    My code:

        int a=90;
        label.resize(a);
    
        for(int i=0;i<a;i++)
            label[i].resize(a);
    
        for(int i=0;i<a;i++)
        {
            for(int j=0;j<a;j++)
            {
                label[i][j]=new QLabel(this);
    
                label[i][j]->setGeometry(i*5,j*5,5,5);
                label[i][j]->setStyleSheet("QLabel {background:red; border: 1px solid black}");
            }
        }
    

    ( label is QVector with QLabels ).

    When I set a=80 everything is ok. When I change it to 81 my app is crashed.

    When I use QGridLayout and add to it QLabels result is the same - 80 ( ok ), 81 (not ok ).

    So maximum number of QLabels is between 6400 ( 80^2) and 6561 (81^2)?

    EDIT I think my pc can display many QLabels :)

    J.HilkJ JonBJ 2 Replies Last reply
    0
    • T TomNow99

      Hello,
      My code:

          int a=90;
          label.resize(a);
      
          for(int i=0;i<a;i++)
              label[i].resize(a);
      
          for(int i=0;i<a;i++)
          {
              for(int j=0;j<a;j++)
              {
                  label[i][j]=new QLabel(this);
      
                  label[i][j]->setGeometry(i*5,j*5,5,5);
                  label[i][j]->setStyleSheet("QLabel {background:red; border: 1px solid black}");
              }
          }
      

      ( label is QVector with QLabels ).

      When I set a=80 everything is ok. When I change it to 81 my app is crashed.

      When I use QGridLayout and add to it QLabels result is the same - 80 ( ok ), 81 (not ok ).

      So maximum number of QLabels is between 6400 ( 80^2) and 6561 (81^2)?

      EDIT I think my pc can display many QLabels :)

      J.HilkJ Offline
      J.HilkJ Offline
      J.Hilk
      Moderators
      wrote on last edited by
      #2

      @TomNow99

      Works perfectly fine for me.

      int main (int argc, char *argv[])
      {
      
          QApplication app(argc, argv);
      
          QVector<QVector<QLabel*> > label;
      
          int a=90;
          label.resize(a);
      
          for(int i=0;i<a;i++)
              label[i].resize(a);
      
          for(int i=0;i<a;i++)
          {
              for(int j=0;j<a;j++)
              {
                  label[i][j]=new QLabel(nullptr);
      
                  label[i][j]->setGeometry(i*5,j*5,5,5);
                  label[i][j]->setStyleSheet("QLabel {background:red; border: 1px solid black}");
              }
          }
          return 0;
      }
      

      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


      Q: What's that?
      A: It's blue light.
      Q: What does it do?
      A: It turns blue.

      1 Reply Last reply
      0
      • T Offline
        T Offline
        TomNow99
        wrote on last edited by
        #3

        @J-Hilk When I run your code it works, but doesn't see any QLabels. You don't set parent.

        J.HilkJ 1 Reply Last reply
        0
        • T TomNow99

          @J-Hilk When I run your code it works, but doesn't see any QLabels. You don't set parent.

          J.HilkJ Offline
          J.HilkJ Offline
          J.Hilk
          Moderators
          wrote on last edited by
          #4

          @TomNow99

          int main (int argc, char *argv[])
          {
          
              QApplication app(argc, argv);
          
              QVector<QVector<QLabel*> > label;
          
              int a=90;
              label.resize(a);
          
              for(int i=0;i<a;i++)
                  label[i].resize(a);
          
              QWidget w;
              w.resize(a * 5, a* 5);
          
              for(int i=0;i<a;i++)
              {
                  qDebug() << i << "of" << a;
                  for(int j=0;j<a;j++)
                  {
                      label[i][j]=new QLabel(&w);
          
                      label[i][j]->setGeometry(i*5,j*5,5,5);
                      label[i][j]->setStyleSheet(QStringLiteral("QLabel {background:red; border: 1px solid black}"));
                  }
              }
          
              w.show();
          
              qDebug() << "Now showing";
          
              return app.exec();
          }
          

          Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


          Q: What's that?
          A: It's blue light.
          Q: What does it do?
          A: It turns blue.

          1 Reply Last reply
          2
          • T Offline
            T Offline
            TomNow99
            wrote on last edited by
            #5

            @J-Hilk Still crashed. So this is my pc problem. Thank you for help :)

            J.HilkJ 1 Reply Last reply
            1
            • T TomNow99

              @J-Hilk Still crashed. So this is my pc problem. Thank you for help :)

              J.HilkJ Offline
              J.HilkJ Offline
              J.Hilk
              Moderators
              wrote on last edited by
              #6

              @TomNow99
              I ran it in release, because in debug it took quite literally forever to create that many QLabels

              only 70mb ram, is consumed so I doubt thats the problem:
              a6cb93ff-ff1b-4617-8dd4-1c31c801dc16-image.png

              that said, if your only objective is to create such a grid, you can do that easily with overwriting paint event on a QWidget class:

              class SomeClass : public QWidget
              {
                  Q_OBJECT
              
              public:
                  explicit SomeClass(int a, QWidget *parent = nullptr)
                      : QWidget(parent), m_a(a)
                  {
                      resize(5 * a, 5 * a);
                  }
                  
              
              protected:
                  int m_a;
                  virtual void paintEvent(QPaintEvent *event) override{
                      QPainter p(this);
              
                      p.fillRect(rect(), Qt::red);
                      p.setBrush(QBrush(Qt::black));
                      for(int i (0); i < m_a; i++) {
                          //Vertical
                          p.drawLine(5 * i, 0, 5 * i, height());
              
                          //Horizontal
                          p.drawLine(0, 5 * i, width(), 5 * i);
                      }
                  }
              };
              
              int main (int argc, char *argv[])
              {
              
                  QApplication app(argc, argv);
              
                  SomeClass s(90);
                  s.show();
              
                  return app.exec();
              }
              

              2978eb5c-6e3f-484a-8545-13009818abf3-image.png


              Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


              Q: What's that?
              A: It's blue light.
              Q: What does it do?
              A: It turns blue.

              1 Reply Last reply
              3
              • T TomNow99

                Hello,
                My code:

                    int a=90;
                    label.resize(a);
                
                    for(int i=0;i<a;i++)
                        label[i].resize(a);
                
                    for(int i=0;i<a;i++)
                    {
                        for(int j=0;j<a;j++)
                        {
                            label[i][j]=new QLabel(this);
                
                            label[i][j]->setGeometry(i*5,j*5,5,5);
                            label[i][j]->setStyleSheet("QLabel {background:red; border: 1px solid black}");
                        }
                    }
                

                ( label is QVector with QLabels ).

                When I set a=80 everything is ok. When I change it to 81 my app is crashed.

                When I use QGridLayout and add to it QLabels result is the same - 80 ( ok ), 81 (not ok ).

                So maximum number of QLabels is between 6400 ( 80^2) and 6561 (81^2)?

                EDIT I think my pc can display many QLabels :)

                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on last edited by JonB
                #7

                @TomNow99
                Maximum number of widgets should only be limited by memory. However, performance & resources will be hit. You simply should not create 6.5K labels, whether it works or not. Next you'll be wanting to do a connect() on each one, with 6.5K signal/slots :) @J-Hilk has shown a way of creating a grid just via painting, you should be looking for a solution like that or similar.

                This, by the way, is also the reason why one-man crusaders like @VRonin are on a mission to rid the world of QTableWidget::setCellWidget() --- can create too many individual widgets when used in a table, where a QStyledItemDelegate can do the job via drawing instead of creating actual widgets.

                1 Reply Last reply
                3

                • Login

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