Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Solved Scroll area is smaller than Dialog

    General and Desktop
    scrollarea dialog
    2
    4
    1374
    Loading More Posts
    • 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.
    • G
      gabor53 last edited by

      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.

      1 Reply Last reply Reply Quote 0
      • SGaist
        SGaist Lifetime Qt Champion last edited by

        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.

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

        1 Reply Last reply Reply Quote 1
        • G
          gabor53 last edited by

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

          1 Reply Last reply Reply Quote 0
          • G
            gabor53 last edited by

            I got it. Thank you for your help.

            1 Reply Last reply Reply Quote 0
            • First post
              Last post