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 8024275
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T22:49:13+00:00 2026-06-04T22:49:13+00:00

I am trying to create layouts in my MainWindow class dynamically. I have four

  • 0

I am trying to create layouts in my MainWindow class dynamically. I have four frames which are laid with a grid layout object. Each frame contains a custom ClockWidget. I want the ClockWidget objects to resize accordingly when I resize the main window, so I need to add them to a layout. However, I need to do this at runtime, since the object itself is created at runtime. I tried to accomplish this programmatically, but the commented-out code below attempting to create a new layout causes the program to crash. What is the procedure for doing this correctly?

Header file:

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

#include "ClockView.h"

namespace Ui{
    class MainWindow;
}

class QLayout;

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:

    explicit MainWindow(QWidget *parent = 0);

    ~MainWindow();

    void populateViewGrid();

private:

    Ui::MainWindow *ui;

    ClockView *clockView_1;
    ClockView *clockView_2;
    ClockView *clockView_3;
    ClockView *clockView_4;

    QLayout *layout_1;
    QLayout *layout_2;
    QLayout *layout_3;
    QLayout *layout_4;
};

#endif // MAINWINDOW_H

implementation file:

#include <QVBoxLayout>

#include "MainWindow.h"
#include "ui_MainWindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    populateViewGrid();
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::populateViewGrid()
{
    clockView_1 = new ClockView(ui->frame_1);
    clockView_2 = new ClockView(ui->frame_2);
    clockView_3 = new ClockView(ui->frame_3);
    clockView_4 = new ClockView(ui->frame_4);

    /*
    layout_1 = new QVBoxLayout;
    layout_2 = new QVBoxLayout;
    layout_3 = new QVBoxLayout;
    layout_4 = new QVBoxLayout;

    layout1->addWidget(clockView_1);
    layout2->addWidget(clockView_2);
    layout3->addWidget(clockView_3);
    layout4->addWidget(clockView_4);

    ui->frame_1->setLayout(layout_1);
    ui->frame_2->setLayout(layout_2);
    ui->frame_3->setLayout(layout_3);
    ui->frame_3->setLayout(layout_4);
    */
}
  • 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-06-04T22:49:14+00:00Added an answer on June 4, 2026 at 10:49 pm

    Your procedure is correct. There are some typos, for example, you’re setting the layout twice for frame3. That may be your problem. Crashes aren’t always reproducible. I don’t think you have any other problems than that. Below is a self contained example. It also keeps all the instances by value, avoiding the premature pessimization of an extra dereference via a pointer.

    // https://github.com/KubaO/stackoverflown/tree/master/questions/dynamic-widget-10790454
    #include <cmath>
    #include <QtGui>
    #if QT_VERSION >= QT_VERSION_CHECK(5,0,0)
    #include <QtWidgets>
    #endif
    #include <array>
    
    // Interface
    
    class ClockView : public QLabel
    {
    public:
        explicit ClockView(QWidget* parent = nullptr) : QLabel(parent)
        {
            static int ctr = 0;
            setText(QString::number(ctr++));
        }
    };
    
    class MainWindow : public QMainWindow
    {
    public:
        explicit MainWindow(QWidget *parent = nullptr);
        void populateViewGrid();
    
    private:
        static constexpr int N = 10;
    
        QWidget central{this};
        QGridLayout centralLayout{&central};
        std::array<QFrame, N> frames;
    
        std::array<ClockView, N> clockViews;
        std::array<QVBoxLayout, N> layouts;
    };
    
    // Implementation
    
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent)
    {
        setCentralWidget(&central);
    
        const int n = ceil(sqrt(N));
        for (int i = 0; i < N; ++ i) {
            frames[i].setFrameShape(QFrame::StyledPanel);
            centralLayout.addWidget(&frames[i], i/n, i%n, 1, 1);
        }
    
        populateViewGrid();
    }
    
    void MainWindow::populateViewGrid()
    {
        for (int i = 0; i < N; ++ i) {
            layouts[i].addWidget(&clockViews[i]);
            frames[i].setLayout(&layouts[i]);
        }
    }
    
    int main(int argc, char** argv)
    {
        QApplication app{argc, argv};
        MainWindow w;
        w.show();
        return app.exec();
    }
    

    And the qmake project file.

    greaterThan(QT_MAJOR_VERSION, 4) {
        QT = widgets 
        CONFIG += c++11
    } else {
        QT = gui 
        unix:QMAKE_CXXFLAGS += -std=c++11
        macx {
            QMAKE_CXXFLAGS += -stdlib=libc++
            QMAKE_MACOSX_DEPLOYMENT_TARGET = 10.7
        }
    }
    TARGET = dynamic-widget-10790454
    TEMPLATE = app
    SOURCES += main.cpp
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to create an interactive ListView without extending the ListActivity class.The layout
In a new app that I'm trying to create I have 3 separate layouts
I am trying create a layout. There are several inner layouts that should like
Im trying to create a layout in flex using mxml, the layout contains a
I'm trying to create a layout similar to this: alt text http://img20.imageshack.us/img20/3533/stackn.png Here's the
I'm trying to create a layout with display: table and alike, but it seems
i'm trying to create following layout: http://jsfiddle.net/BTuMH/1/ but without setting the width, like: http://jsfiddle.net/yuGyg/
I'm trying to create a Tkinter layout that has labels and entry fields vertically
I am trying to just create a basic layout, but i am having trouble
I'm trying to create a flexible box layout that fills the entire viewport, and

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.