Add widget to layout programmatically



  • I have a layout added to form in desginer, then, in run time I try to add an object, which extends from QWidget, to central widget. The program crashes in the point where I add the widget, with no possibility for further debug.

    0_1544447180072_layout.jpg

    Code in main window:

    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
    
        m_canvas = new RenderArea(this);
        QWidget *q = static_cast<QWidget*>(m_canvas);
        this->centralWidget()->layout()->addWidget(q);
    
        m_canvas->setLevel(1);
    }
    
    

    The crash is in line this->centralWidget()->layout()->addWidget(q); with message:

    The inferior stopped because it received a signal from the operating system.
    Signal name : SIGSEGV
    Signal meaning : Segmentation fault

    Where RenderArea is this:

    class RenderArea : public QWidget
    {
        Q_OBJECT
    
    public:
        explicit RenderArea(QWidget *parent = nullptr);
    
        void setLevel(qreal value);
    
    protected:
        void paintEvent(QPaintEvent *event) override;
    
    private:
        qreal m_level = 0;
        QPixmap m_pixmap;
    };
    

    and

    RenderArea::RenderArea(QWidget *parent)
        : QWidget(parent)
    {
        setBackgroundRole(QPalette::Base);
        setAutoFillBackground(true);
    
        setMinimumHeight(200);
        setMinimumWidth(30);
    }
    
    

  • Qt Champions 2019

    @kweber Please check q before using it!
    It's a pointer and then static_cast<QWidget*> fails it will be null.
    If RenderArea is a QWidget there is no need for this cast at all.


  • Lifetime Qt Champion

    Hi,

    Are you sure centralCentral() does return a QWidget ?

    Calling setCentralWidget(m_canvas); will likely be enough.

    By the way, there's no need for that static_cast at all.


  • Moderators

    additionally to what @jsulm said,

    you're trying to add a Qwidget the the centralWidget layout, but your central widget has no layout.
    in the designer itself you created a new layout, that is part of the centralWidget but not the one you access via this->centralWidget()->layout() that one does not exist.



  • I have the same problem if I skip the cast and change for this:

    this->centralWidget()->layout()->addWidget(m_canvas);
    

    (actually I introduced the cast just to try, I forgot to take it out)


  • Moderators

    @kweber

    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
    
        m_canvas = new RenderArea(this);
    
       setLayout(ui->verticalLayout);
       ui->verticalLayout->addWidget(m_canvas);
    }
    

  • Qt Champions 2019

    @kweber said in Add widget to layout programmatically:

    I have the same problem if I skip the cast and change for this:

    You should read answers more carefully...



  • @SGaist that does work, but what I want is to specify an area of the designer for the widget to be added. In the image I posted the layout is almost as big as the window, but what I want is it to be a vertical line at the right of the window, that's why I wanted to use a box layout and want to put the widget inside it.

    (I'm sew user, I can only post every 10 minutes :-) )

    @J-Hilk I'm new to Qt, I don't know how to add a layout to centralWidget or how to achieve this.

    Thank you



  • @J.Hilk said in Add widget to layout programmatically:

    @kweber

    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
    
        m_canvas = new RenderArea(this);
    
       setLayout(ui->verticalLayout);
       ui->verticalLayout->addWidget(m_canvas);
    }
    

    Thank you for this, after trying, the code does not add my widget and I get message:

    QWidget::setLayout: Attempting to set QLayout "verticalLayout" on MainWindow "MainWindow", which already has a layout

    What I want to achieve is to set this widget in the right end of the Window. Sorry, I'm just starting with Qt.


  • Moderators

    @kweber said in Add widget to layout programmatically:

    setLayout(ui->verticalLayout);

    my bad for posting untested code

     ui->centralWidget->setLayout(ui->verticalLayout);
    

Log in to reply