different size buttons on qlayout



  • i created two buttons and a vertical layout and i want both buttons to be positioned in the center of their row. but their widths are different and that makes my work seem hard, and only the second (bottom-most) button is in the center. what's the problem with the first one?

    and by the way, why don't the commented lines make a difference in the code?

    #include <qapplication.h>
    #include <qpushbutton.h>
    #include <qlayout.h>
    
    int main(int argc, char **argv) {
    
        QApplication app(argc, argv);
    
        QWidget window;
        window.setFixedSize(200, 100);
    
        QPushButton *buttonInfo = new QPushButton("Info", &window);
        //buttonInfo->setGeometry(10, 10, 80, 30);
        buttonInfo->setMaximumWidth(80);
    
        QPushButton *buttonQuit = new QPushButton("Quit", &window);
        //buttonQuit->setGeometry(10, 10, 110, 30);
        buttonQuit->setMinimumWidth(110);
    
        QObject::connect(buttonInfo, SIGNAL(clicked()), qApp, SLOT(aboutQt()));
        QObject::connect(buttonQuit, SIGNAL(clicked()), qApp, SLOT(quit()));
    
        QVBoxLayout *layout = new QVBoxLayout;
        layout->addWidget(buttonInfo);
        layout->addWidget(buttonQuit);
        layout->setAlignment(Qt::AlignCenter);
    
        window.setLayout(layout);
        window.show();
    
        return app.exec();
    }
    


  • First you've should created a horizontal layout and added the buttonInfo widget with Qt::AlignHCenter argument, then created a vertical layout and added the horizontal layout to it and the quitButton widget. Check this code, worked as you wanted to:

    #include <qapplication.h>
    #include <qpushbutton.h>
    #include <qlayout.h>
    
    int main(int argc, char **argv) {
    
        QApplication app(argc, argv);
    
        QWidget window;
        window.setFixedSize(200, 100);
    
        QPushButton *buttonInfo = new QPushButton("Info", &window);
        //buttonInfo->setGeometry(10, 10, 80, 30);
        buttonInfo->setMaximumWidth(80);
    
        QPushButton *buttonQuit = new QPushButton("Quit", &window);
        //buttonQuit->setGeometry(10, 10, 110, 30);
        buttonQuit->setMinimumWidth(110);
    
        QObject::connect(buttonInfo, SIGNAL(clicked()), qApp, SLOT(aboutQt()));
        QObject::connect(buttonQuit, SIGNAL(clicked()), qApp, SLOT(quit()));
    
        QHBoxLayout *hlayout = new QHBoxLayout;
        QVBoxLayout *vlayout = new QVBoxLayout;
    
        hlayout->addWidget(buttonInfo, 0, Qt::AlignHCenter);
    
        vlayout->addLayout(hlayout);
        vlayout->addWidget(buttonQuit);
        vlayout->setAlignment(Qt::AlignCenter);
    
        window.setLayout(vlayout);
        window.show();
    
        return app.exec();
    }
    

    The commented lines doesn't work because you assigned those widgets to a layout, then the layout customizes their size for its needs.


Log in to reply
 

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