Scroll area is smaller than Dialog



  • Hi,
    I have the following code that works perfectly in MainWindow, but in Dialog the scroll area is much smaller, than the Dialg, and the widgets don't fit in the scroll area.

    #include "dialog.h"
    #include "ui_dialog.h"
    #include <QScrollArea>
    #include <QGridLayout>
    #include <QLabel>
    #include <QVBoxLayout>
    #include <QLineEdit>
    #include <QSpacerItem>
    
    Dialog::Dialog(QWidget *parent) :
        QDialog(parent),
        ui(new Ui::Dialog)
    {
        ui->setupUi(this);
        QWidget *mainWidget = new QWidget(this);
    
        QLabel* title = new QLabel ;
          title->setText("Add a Friend To the Database");
          title->setAlignment(Qt::AlignCenter);
          QFont f( "Arial", 18, QFont::Bold);
          title->setFont( f);
    
        QScrollArea *scroll = new QScrollArea(mainWidget);
        scroll->setWidgetResizable (true);
        scroll->setVerticalScrollBarPolicy (Qt::ScrollBarAlwaysOn);
        scroll->setHorizontalScrollBarPolicy (Qt::ScrollBarAlwaysOn);
    
        QLabel *spacer1 = new QLabel;
        spacer1->setText ("           ");
        QLabel *spacer2 = new QLabel;
        spacer2->setText ("                    ");
    
    
        QLabel *Name_Label = new QLabel;
        Name_Label->setText ("Name: ");
    
        QLabel *Address_Label = new QLabel;
        Address_Label->setText ("Address: ");
    
        QLineEdit *Name_Edit = new QLineEdit;
    
        Name_Edit->setMaxLength (10);
    
        QLineEdit *Address_Edit = new QLineEdit;
    
        Address_Edit->setMaxLength (10);
    
        QGridLayout *grid = new QGridLayout;
    
        QWidget *viewport = new QWidget(this);
        scroll->setWidget (viewport);
        scroll->setWidgetResizable (true);
    
        QHBoxLayout *Name_Layout = new QHBoxLayout;
        Name_Layout->addWidget (Name_Label);
        Name_Layout->addWidget (Name_Edit);
    
        grid->addWidget (Name_Label,0,0);
        grid->addWidget (Name_Edit,0,1);
        grid->addWidget (spacer2,0,1,Qt::AlignRight);
        grid->addWidget (spacer1,0,2);
        grid->addWidget (Address_Label,1,0);
        grid->addWidget (Address_Edit,1,1);
        grid->addWidget (spacer2,1,1,Qt::AlignRight);
        grid->addWidget (spacer1,1,2);
    
        scroll->setLayout (grid);
    
        QVBoxLayout *mainLayout = new QVBoxLayout(mainWidget);
        mainLayout->addWidget (title);
        mainLayout->addWidget (scroll);
    
    
        mainWidget->setLayout (mainLayout);
    
    
    }
    
    Dialog::~Dialog()
    {
        delete ui;
    }
    
    

    Dialog
    How can I change the code to make the scroll area stretch with the dialog (and cover the whole dialog)?
    Thank you.


  • Lifetime Qt Champion

    Hi,

    Since you seem to build your widget in the constructor, why do you a designer based base class ?

    What currently happens is that mainWidget is a child of Dialog and it's not managed by any layout so you would have to do the positioning and sizing yourself unless you remove the designer bits (since you don't seem to need them at all in this case) then you can simply remove mainWidget and set mainLayout on your dialog and you should be good to go.



  • Thanks. How can I remove the "designer bits"?



  • I got it. Thank you for your help.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.