Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct


    Qt World Summit: Early-Bird Tickets

    Unsolved Is there a maximum number of QLabels in a screen?

    General and Desktop
    3
    7
    203
    Loading More Posts
    • 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
      TomNow99 last edited by 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.Hilk JonB 2 Replies Last reply Reply Quote 0
      • J.Hilk
        J.Hilk Moderators @TomNow99 last edited by

        @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

        Qt Needs YOUR vote: https://bugreports.qt.io/browse/QTQAINFRA-4121


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

        1 Reply Last reply Reply Quote 0
        • T
          TomNow99 last edited by

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

          J.Hilk 1 Reply Last reply Reply Quote 0
          • J.Hilk
            J.Hilk Moderators @TomNow99 last edited by

            @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

            Qt Needs YOUR vote: https://bugreports.qt.io/browse/QTQAINFRA-4121


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

            1 Reply Last reply Reply Quote 2
            • T
              TomNow99 last edited by

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

              J.Hilk 1 Reply Last reply Reply Quote 1
              • J.Hilk
                J.Hilk Moderators @TomNow99 last edited by

                @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

                Qt Needs YOUR vote: https://bugreports.qt.io/browse/QTQAINFRA-4121


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

                1 Reply Last reply Reply Quote 3
                • JonB
                  JonB @TomNow99 last edited by JonB

                  @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 Reply Quote 3
                  • First post
                    Last post