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. QPushButton won't top align in QGridLayout
Forum Updated to NodeBB v4.3 + New Features

QPushButton won't top align in QGridLayout

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 3.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.
  • I Offline
    I Offline
    ImDevinC
    wrote on last edited by
    #1

    I have a QVBoxLayout that contains a top-aligned QLineEdit and a top-aligned QWidget with the layout set as a QGridLayout. I then programmatically add top-aligned QPushButtons to the QGridLayout depending on user input. This all works, but the problem I'm having is that the QPushButton does not top-align in the QGridLayout. Here's some code snippets I'm hoping will help.

    Main Layout

    this->m_filterBox = new QLineEdit(tr(""));
    this->m_filterBox->setPlaceholderText(tr("Filter"));
    connect(m_filterBox, &QLineEdit::textChanged, this, &CMainWindow::filterChanged);
    QVBoxLayout *layout = new QVBoxLayout();
    layout->addWidget(m_filterBox);
    layout->setAlignment(this->m_filterBox, Qt::AlignTop);
    this->m_keyGrid = new QGridLayout();
    QWidget *gridWidget = new QWidget();
    gridWidget->setLayout(this->m_keyGrid);
    layout->addWidget(gridWidget);
    layout->setAlignment(gridWidget, Qt::AlignTop);
    QWidget *window = new QWidget(this);
    window->setLayout(layout);
    this->setCentralWidget(window);
    

    Adding buttons

    int row = 0;
    int column = 0;
    for(std::vector<QString>::iterator it = this->m_vecStrings.begin(); it != this->m_vecStrings.end(); ++it) {
        QString input = *it;
        QPushButton *button = new QPushButton(input);
        this->m_keyGrid->addWidget(button, row, column, 1, 1, Qt::AlignTop);
        column++;
        if (column > 2) {
            row++;
            column = 0;
        }
    }
    

    Any insight is appreciated. Here's a screenshot of what I'm getting for more information
    Screenshot

    1 Reply Last reply
    0
    • M Offline
      M Offline
      MaxDevI
      wrote on last edited by MaxDevI
      #2

      So you want the pushbuttons right beneath the lineEdit? See this

      "If the stretch factor is 0 and nothing else in the QBoxLayout has a stretch factor greater than zero, the space is distributed according to the QWidget:sizePolicy() of each widget that's involved.

      The alignment is specified by alignment. The default alignment is 0, which means that the widget fills the entire cell."
      http://doc.qt.io/qt-5/qboxlayout.html

      Let me know if that helps

      EDIT: Also, have a look at http://doc.qt.io/qt-5/qlayout.html#spacing-prop

      Maybe you can try setting the spacing of the rows to be small so that the buttons appear near top

      1 Reply Last reply
      2
      • I Offline
        I Offline
        ImDevinC
        wrote on last edited by
        #3

        Thanks @MaxDevI, that worked perfectly!

        1 Reply Last reply
        0

        • Login

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