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. Lines in Gridlayout are incorrectly positioned
QtWS25 Last Chance

Lines in Gridlayout are incorrectly positioned

Scheduled Pinned Locked Moved Unsolved General and Desktop
gridlayout
2 Posts 2 Posters 1.2k Views
  • 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 Offline
    G Offline
    gabor53
    wrote on 15 Feb 2016, 05:46 last edited by
    #1

    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.

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 15 Feb 2016, 23:51 last edited by
      #2

      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

      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
      0

      2/2

      15 Feb 2016, 23:51

      • Login

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