Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Add widget to layout programmatically
Forum Updated to NodeBB v4.3 + New Features

Add widget to layout programmatically

Scheduled Pinned Locked Moved Unsolved General and Desktop
layoutqwidget
10 Posts 4 Posters 8.6k Views 2 Watching
  • 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.
  • K Offline
    K Offline
    kweber
    wrote on last edited by kweber
    #1

    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);
    }
    
    
    jsulmJ 1 Reply Last reply
    0
    • K kweber

      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);
      }
      
      
      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @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.

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      3
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        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.

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        K 1 Reply Last reply
        2
        • J.HilkJ Online
          J.HilkJ Online
          J.Hilk
          Moderators
          wrote on last edited by
          #4

          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.


          Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


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

          1 Reply Last reply
          4
          • K Offline
            K Offline
            kweber
            wrote on last edited by
            #5

            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)

            J.HilkJ jsulmJ 2 Replies Last reply
            0
            • K kweber

              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)

              J.HilkJ Online
              J.HilkJ Online
              J.Hilk
              Moderators
              wrote on last edited by
              #6

              @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);
              }
              

              Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


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

              K 1 Reply Last reply
              2
              • K kweber

                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)

                jsulmJ Offline
                jsulmJ Offline
                jsulm
                Lifetime Qt Champion
                wrote on last edited by
                #7

                @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...

                https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                0
                • SGaistS SGaist

                  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.

                  K Offline
                  K Offline
                  kweber
                  wrote on last edited by
                  #8

                  @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

                  1 Reply Last reply
                  0
                  • J.HilkJ J.Hilk

                    @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);
                    }
                    
                    K Offline
                    K Offline
                    kweber
                    wrote on last edited by
                    #9

                    @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.

                    J.HilkJ 1 Reply Last reply
                    0
                    • K kweber

                      @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.

                      J.HilkJ Online
                      J.HilkJ Online
                      J.Hilk
                      Moderators
                      wrote on last edited by
                      #10

                      @kweber said in Add widget to layout programmatically:

                      setLayout(ui->verticalLayout);

                      my bad for posting untested code

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

                      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


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

                      1 Reply Last reply
                      2

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved