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. [solved] placing QPushButtons inside a QVbox on mainwindow

[solved] placing QPushButtons inside a QVbox on mainwindow

Scheduled Pinned Locked Moved General and Desktop
7 Posts 2 Posters 2.3k Views 1 Watching
  • 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.
  • D Offline
    D Offline
    deleted28
    wrote on last edited by
    #1

    I want to place some QPushButtons inside a QVboxLayout from within code
    Mainwindow was created as mainwindow.ui using QT-Creator Designer.
    Following code creating a centerd pushbutton with full windowsize width regardless
    any geometry settings.
    I think i need to set "centralwidget" layout (?) but can not figure out how to do this.

    @void MainWindow::on_pb_test_2_clicked()
    {
    QVBoxLayout *vb = new QVBoxLayout(MainWindow::centralWidget());
    vb->setObjectName(QString("vb_1"));
    vb->setGeometry(QRect(100, 100,25,100));

    QPushButton *pb_1 = new QPushButton;
    pb_1->setObjectName(QString("pb_1"));
    pb_1->setStyleSheet(QString("QPushButton {background-color: red;}"));
    pb_1->setGeometry(QRect(0, 0, 15, 15));
    pb_1->setText("o");
    vb->addWidget(pb_1);
    

    // ? ui->centralWidget
    // ? MainWindow->setCentralWidget(centralWidget());
    }@

    This code creates only a single pushbutton for now to keep things more clear.
    I found some 'solutions' on this forum but any of them fixes the problem for me.
    Thanks in advance.

    1 Reply Last reply
    0
    • JeroentjehomeJ Offline
      JeroentjehomeJ Offline
      Jeroentjehome
      wrote on last edited by
      #2

      You probably need to set the layout of the central widget:
      @
      ui->centralWidget()->setLayout(<your Vbox>);
      @

      Greetz, Jeroen

      1 Reply Last reply
      0
      • D Offline
        D Offline
        deleted28
        wrote on last edited by
        #3

        thanks for reply, but no change, still a full window width pushbutton, vertically centered on mainwindow.
        Built without any error.

        I added to position line 13 in above code

        @ ui->centralWidget->setLayout(vb);@

        When i do the design i want using the designer i see in
        ui_mainwindow.h this lines:

        @verticalLayoutWidget = new QWidget(centralWidget);
        verticalLayoutWidget->setObjectName(QString::fromUtf8("verticalLayoutWidget"));
        verticalLayoutWidget->setGeometry(QRect(60, 70, 21, 261));

        verticalLayout = new QVBoxLayout(verticalLayoutWidget);
        verticalLayout->setSpacing(6);
        verticalLayout->setContentsMargins(11, 11, 11, 11);
        verticalLayout->setObjectName(QString::fromUtf8("verticalLayout"));
        verticalLayout->setContentsMargins(0, 0, 0, 0);@

        in the "manual" code i do not use anything similar to:
        @verticalLayoutWidget = new QWidget(centralWidget);@

        maybe this is my mistake ?

        1 Reply Last reply
        0
        • JeroentjehomeJ Offline
          JeroentjehomeJ Offline
          Jeroentjehome
          wrote on last edited by
          #4

          Argh, I misread. So you have a pushbutton that hold the entire screen????
          That is what it suppose to do. Use the spacers to indicate to the layout that 'empty' space should be created if the pushbutton is beyond it's maximum size or geometry settings. Otherwise the Layout will force the pushbutton to take the entire screen.

          Greetz, Jeroen

          1 Reply Last reply
          0
          • D Offline
            D Offline
            deleted28
            wrote on last edited by
            #5

            so i need yo add "spacers" in the manual code ?
            puh

            I just want the pushbutton stays inside the given geometry
            of the vboxlayout. I fear i need to read a lot more to get this work.

            1 Reply Last reply
            0
            • D Offline
              D Offline
              deleted28
              wrote on last edited by
              #6

              Got it!
              tomorrow i 'll paste it here. Now i need a drink :)

              1 Reply Last reply
              0
              • D Offline
                D Offline
                deleted28
                wrote on last edited by
                #7

                Just QVBoxLayout is not sufficient, i need a QWidget too.
                (i guess the QVBoxLayout is an abstract object only
                and needs a widget object to 'materialise' )
                Reduced to the essentials the following code works:
                @void MainWindow::on_pb_test_clicked()
                {
                QWidget *vLW = new QWidget(MainWindow::centralWidget());
                vLW->setObjectName(QString::fromUtf8("vLW"));
                vLW->setGeometry(QRect(150, 100, 31, 250));

                QVBoxLayout *vL = new QVBoxLayout(vLW);
                vL->setObjectName(QString::fromUtf8("vL"));
                
                QPushButton *pushButton = new QPushButton(vLW);
                pushButton->setObjectName(QString::fromUtf8("pushButton"));
                pushButton->setMaximumSize(QSize(20, 20));
                pushButton->setStyleSheet(QString("QPushButton {background-color: red;}"));
                vL->addWidget(pushButton);
                
                vLW->show();
                

                }@

                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