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.7k 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 10 Dec 2018, 13:12 last edited by kweber 12 Oct 2018, 13:24
    #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);
    }
    
    
    J 1 Reply Last reply 10 Dec 2018, 13:15
    0
    • K kweber
      10 Dec 2018, 13:12

      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);
      }
      
      
      J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 10 Dec 2018, 13:15 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
      • S Offline
        S Offline
        SGaist
        Lifetime Qt Champion
        wrote on 10 Dec 2018, 13:19 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 10 Dec 2018, 13:35
        2
        • J Offline
          J Offline
          J.Hilk
          Moderators
          wrote on 10 Dec 2018, 13:19 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 10 Dec 2018, 13:23 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 J 2 Replies Last reply 10 Dec 2018, 13:27
            0
            • K kweber
              10 Dec 2018, 13:23

              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 Offline
              J Offline
              J.Hilk
              Moderators
              wrote on 10 Dec 2018, 13:27 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 10 Dec 2018, 13:49
              2
              • K kweber
                10 Dec 2018, 13:23

                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 Offline
                J Offline
                jsulm
                Lifetime Qt Champion
                wrote on 10 Dec 2018, 13:34 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
                • S SGaist
                  10 Dec 2018, 13:19

                  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 10 Dec 2018, 13:35 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 J.Hilk
                    10 Dec 2018, 13:27

                    @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 10 Dec 2018, 13:49 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 1 Reply Last reply 10 Dec 2018, 13:51
                    0
                    • K kweber
                      10 Dec 2018, 13:49

                      @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 Offline
                      J Offline
                      J.Hilk
                      Moderators
                      wrote on 10 Dec 2018, 13:51 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

                      1/10

                      10 Dec 2018, 13:12

                      • Login

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