Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How to populate Spinboxes on scroll area



  • This is my code where i am trying to populate spin boxes in the scroll area but the window shows without the scroll area and the spinboxes.

    void ShowCommands::showWindow()
    {
    	QWidget *wdg = new QWidget;
    	QVBoxLayout m_layout{ wdg };
    	QScrollArea m_area;
    	QWidget m_contents;
    	QVBoxLayout m_contentsLayout{ &m_contents };
    	QSpinBox m_spinBoxes[10];
    	m_layout.addWidget(&m_area);
    	m_area.setWidget(&m_contents);
    	for (auto & spinbox : m_spinBoxes)
    		m_contentsLayout.addWidget(&spinbox);
    	
    	m_contentsLayout.setSizeConstraint(QLayout::SetMinimumSize);
    	wdg->setWindowTitle("Commands");
    	wdg->show();
    }
    

  • Qt Champions 2019

    @summit said in How to populate Spinboxes on scroll area:

    QSpinBox m_spinBoxes[10];

    This is local arrray which is destroyed together with all spin boxes as soon as showWindow() finishes...
    Also QVBoxLayout m_layout{ wdg }; QScrollArea m_area; QWidget m_contents; are local stack variables...



  • This post is deleted!


  • @jsulm i have changed my code but still spinboxes are missing from the window.

    QWidget *wdg = new QWidget;
    	QVBoxLayout *m_layout = new QVBoxLayout;
    	QScrollArea *m_area = new QScrollArea;
    	QWidget *m_contents = new QWidget;
    	QVBoxLayout *m_contentsLayout = new QVBoxLayout;
    	m_contentsLayout->setParent(m_contents);
    	m_layout->addWidget(m_area);
    	m_area->setWidget(m_contents);
    	for (auto & spinbox : m_spinBoxes)  // m_spinBoxes is class varibles QSpinBox m_spinBoxes[10]
    		m_contentsLayout->addWidget(&spinbox);
    
    	m_contentsLayout->setSizeConstraint(QLayout::SetMinimumSize);
    	wdg->setLayout(m_layout);
    	wdg->setWindowTitle("Commands");
    	wdg->show();
    


  • @summit said in How to populate Spinboxes on scroll area:

    m_contentsLayout->setParent(m_contents);

    I'm not sure this is good enough. Where did you get the inspiration to do it this way? The usual is m_contents->setLayout(m_contentsLayout)?


  • Qt Champions 2019

    @summit With your current code you'll get a warning at runtime, check that.
    This works:

    QWidget *wdg = new QWidget;
    QVBoxLayout *m_layout = new QVBoxLayout;
    QScrollArea *m_area = new QScrollArea;
    QWidget *m_contents = new QWidget;
    QVBoxLayout *m_contentsLayout = new QVBoxLayout;
    m_contents->setLayout(m_contentsLayout);
    m_layout->addWidget(m_area);
    m_area->setWidget(m_contents);
    for (auto & spinbox : m_spinBoxes)  // m_spinBoxes is class varibles QSpinBox m_spinBoxes[10]
        m_contentsLayout->addWidget(&spinbox);
    
    m_contentsLayout->setSizeConstraint(QLayout::SetMinimumSize);
    wdg->setLayout(m_layout);
    wdg->setWindowTitle("Commands");
    wdg->show();
    

    But since wdg is not parented it will be shown in its own window...



  • @jsulm Thank you very much this works.


Log in to reply