Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6033013
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T05:25:17+00:00 2026-05-23T05:25:17+00:00

How to Create QGridLayout at run time and generate rows dynamically and add buttons

  • 0

How to Create QGridLayout at run time and generate rows dynamically and add buttons to it dynamically ?

On button i want to place a Image & label at run time.

I am creating four buttons on button click and try to place a icon on it , but it is not appearing on it and i am trying to make my grid as scrollable(vertically) but it is not getting scrollable.

 QScrollArea *scrollArea = new QScrollArea(this);         

       QPushButton *b1 = new QPushButton(strsearch);
       QPushButton *b2 = new QPushButton(strsearch);
       QPushButton *b3 = new QPushButton(strsearch);
       QPushButton *b4 = new QPushButton(strsearch);


       b1->setGeometry(QRect(0, 0, 162, 26));
       b2->setGeometry(QRect(0, 0, 162, 26));
       b3->setGeometry(QRect(0, 0, 162, 26));
       b4->setGeometry(QRect(0, 0, 162, 26));
       b1->setIcon(QIcon("user.gif"));
       b1->setIconSize(QSize(160, 26));
       b2->setIcon(QIcon("user.gif"));
       b2->setIconSize(QSize(160, 26));
       b3->setIcon(QIcon("user.gif"));
       b3->setIconSize(QSize(160, 26));
       b4->setIcon(QIcon("user.gif"));
       b4->setIconSize(QSize(160, 26));

       QVBoxLayout *vl = new QVBoxLayout;

       layout = new QGridLayout;  // this is your grid layout
       vl->addWidget(b1);
       vl->addWidget(b2);
       vl->addWidget(b3);
       vl->addWidget(b4);

       vl->addLayout(layout);
      scrollArea->setWidget(vl->widget());
       vl->addWidget(scrollArea);

       setLayout(vl);

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T05:25:18+00:00Added an answer on May 23, 2026 at 5:25 am

    There’s nothing very special about adding things to a layout at runtime.

    I’d suggest you create the grid layout and store it as a member of the widget into which you want it displayed, and connect your button’s clicked() signal to a custom slot if that widget.

    Something like this:

    class Win: public QWidget
    {
        Q_OBJECT
    
        public:
            Win(QWidget *parent=0): QWidget(parent)
        {
            QPushButton *b1 = new QPushButton("click");
            QVBoxLayout *vl = new QVBoxLayout;
            layout = new QGridLayout;  // this is your grid layout
            vl->addWidget(b1);
            vl->addLayout(layout);     // added to the widget's main layout
            setLayout(vl);
            connect(b1, SIGNAL(clicked()), this, SLOT(clicked())); // connect button to slot
        }
    
        public slots:
            void clicked()
            {
                int position = layout->rowCount(); // this works out as the first row index with nothing on it
                QPushButton *b = new QPushButton;
                b->setIcon(<<the icon you want>>);
                b->setText(<<the text you want>>);
                layout->addWidget(b, position, 0);
                layout->addWidget(new QLabel(<<whatever you want here>>), position, 1);
                        ...
            }
    
        private:
            QGridLayout *layout;
    };
    

    Nothing prevents you from adding a new QGridLayout, and adding new widgets to it, at runtime. It’s probably going to be a bit tough managing a variable number of layouts on your UI.


    Your code does not make much sens. You’re adding your buttons to the QVBoxLayout, then adding an empty grid layout to it, then setting your scroll area’s widget to something that doesn’t exist (vl->widget() will return NULL, since a QVBoxLayout is not a widget, see here), then adding that scroll area into the layout you were trying to include into the scroll area itself?

    You need to nest the layouts and widgets properly.

    • Add the buttons to the grid layout if that’s what you want.
    • Then create a container widget for that layout (a QFrame for instance), and set that widget’s layout to your grid layout.
    • Then add that QFrame into a QScrollArea.
    • Then put add scroll area to your container widget’s layout.

    Please go through the the Qt samples and layout documentation, including the Image Viewer sample. You need to study more about layouts.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create a QGridLayout in PyQt4, and I can't figure out for
I have a GridLayout 2 rows by 5 columns, and I want to make
Is it possible to have the Save note button under the Create note button
I'm having an issue in my application. I wish to dynamically create several tabbed
I am using the following code for create a button. It is working fine.
Hello I have a Problem with some Buttons I would like to create a
create table person ( name varchar(15), attr1 varchar(15), attr2 varchar(1), attr3 char(1), attr4 int
Create a flat text file in c++ around 50 - 100 MB with the
Create a file called Valid[File].txt and stick some text in it. Start powershell and
Create Proc CrearNuevoAnuncio @Titulo varchar(250), @Precio int, @Descripcion varchar(250), @IDCategoria int, @IDImagen int, @Login

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.