Lines in Gridlayout are incorrectly positioned



  • Hi,
    I have the following code which supposed to create subsequent lines in a Dialog:

    	QWidget *mainWidget = new QWidget(this);
    
     QScrollArea *scroll = new QScrollArea(this);
            scroll->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
            scroll->setVerticalScrollBarPolicy (Qt::ScrollBarAlwaysOn);
            scroll->setFixedSize (885,710);
    
    
       //viewport
    
            QWidget* viewport  = new QWidget(this);
            scroll->setWidget (viewport);
            scroll->setWidgetResizable (true);
    
           QLineEdit *lineedit1 = new QLineEdit;
           QLineEdit *lineedit2 = new QLineEdit;
           QLineEdit *lineedit3 = new QLineEdit;
           QLineEdit *lineedit4 = new QLineEdit;
           QLineEdit *lineedit5 = new QLineEdit;
           QLineEdit *lineedit6 = new QLineEdit;
    
           lineedit1->setText ("LineEdit1");
           lineedit2->setText ("LineEdit2");
           lineedit3->setText ("LineEdit3");
           lineedit4->setText ("LineEdit4");
           lineedit5->setText ("LineEdit5");
           lineedit6->setText ("LineEdit6");
    
           lineedit1->setMaximumWidth (100);
           lineedit2->setMaximumWidth (100);
           lineedit3->setMaximumWidth (100);
           lineedit4->setMaximumWidth (100);
           lineedit5->setMaximumWidth (100);
           lineedit6->setMaximumWidth (100);
    
      // Gridlayout
    
           QGridLayout *grid = new QGridLayout;
    
           grid->addWidget (lineedit1,0,0,Qt::AlignTop);
           grid->addWidget (lineedit2,0,1,Qt::AlignTop);
           grid->addWidget (lineedit3,0,2,Qt::AlignTop);
           grid->addWidget (lineedit4,0,3,Qt::AlignTop);
           grid->addWidget (lineedit5,1,0,Qt::AlignTop);
           grid->addWidget (lineedit6,1,1,Qt::AlignTop);
    
          scroll->setLayout (grid);
    
            int colcount = grid->columnCount ();
            qDebug() << "Column count: " << colcount;
    
            int rowcount = grid->rowCount ();
            qDebug() << "Row count: " << rowcount;
    
            int columnwidth = grid->columnMinimumWidth (0);
            qDebug() << "1st column width: " << columnwidth;
    
      // VBox Layout
    
            QVBoxLayout* mainLayout = new QVBoxLayout;
            mainLayout->addWidget(title);
    
    
            mainLayout->addWidget(scroll);
            mainWidget->setLayout(mainLayout);
            mainWidget->show ();
    }
    

    The code generates the following dialog:

    Dialog
    I'm wondering why is the second line on the bottom of the page? How can I get the widgets in the line closer to each other?
    Thank you for your help.


  • Lifetime Qt Champion

    Hi,

    There's one thing that's wrong, you should not put a layout on a QScrollArea but a widget. So the first thing to do is put your grid layout on a widget and then set that widget on the QScrollArea.

    IIRC the grid layout (as QVBoxLayout) will use by default the available space to layout the widget evenly


Log in to reply
 

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