Qt Forum

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

    Unsolved Arranging the grid layout in function

    General and Desktop
    2
    2
    309
    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.
    • Pradeep Kumar
      Pradeep Kumar last edited by

      Hi,

      I have a grid layout of 2x2
      aligned to QVBoxLayout.

      This code is in constructor.

      int col = 0;
      int row = 0;
      int m_layout  = 4
      int lay_col= 1;
      
      for(int i = 0 ; i < m_layout ; i++ )
      {
         m_List[i].m_label = new QLabel;
         m_List[i].m_label ->setText("Label");
      
         m_List[i].widget = new QWidget;
         m_List[i].widget->setStyleSheet("background-color:blue");
       
         m_grid->addWidget(m_List[i].m_label,row,col);
         m_grid->addWidget(m_List[i].widget,row,col);
      
      
         col++;
      
      
         if(col == lay_col)
         {
             row++;
             col = 0;
         }
      }
      m_vbox->addLayout(m_grid);
      setLayout(m_vbox)
      

      From the function when i need to change the gridlayout to the corresponding values provided from the function.
      for example 2x3 or so .

      How can i do it, guidance is required.

      Thanks,

      Pradeep Kumar
      Qt,QML Developer

      kshegunov 1 Reply Last reply Reply Quote 0
      • kshegunov
        kshegunov Moderators @Pradeep Kumar last edited by kshegunov

        Something along those lines:

        qint32 columns = 5; // Or whatever comes as an argument
        QList<QWidget *> itemsToInsert;
        QGridLayout * layout;
        
        for (qint32 i = 0, size = itemsToInsert.size(); i < size; i++)
            layout->addWidget(itemsToInsert[i], i / columns, i % columns);
        

        Read and abide by the Qt Code of Conduct

        1 Reply Last reply Reply Quote 3
        • First post
          Last post