Centering a QToolButton in a QVBoxLayout?



  • It appears that QVBoxLayout's setAlignment(Qt::AlignHCenter) only sudo centres the widgets (or only the widest one). The smaller QToolButton is left aligned with the left edge of widest widget.
    0_1512583094832_wrong.png
    Is this a bug in QVBoxLayout or am I doing somehing wrong? QGridLayout (below) works fine. Code that made above image....

    test.pro

    QT+= core gui widgets
    TARGET = test
    TEMPLATE = app
    SOURCES+= main.cpp
    

    main.cpp

    #include <QWidget>
    #include <QVBoxLayout>
    #include <QToolButton>
    #include <QApplication>
    #include <QStyle>
    
    int main(int argc, char *argv[])
    {
    	QApplication a(argc, argv);
    
    	QWidget *window = new QWidget();
    	QVBoxLayout *layout = new QVBoxLayout();
    	QToolButton *button;
    
    	layout->setAlignment(Qt::AlignHCenter);
    
    	layout->addStretch();
    
    	button = new QToolButton();
    	button->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
    	button->setIcon(window->style()->standardIcon(QStyle::SP_BrowserReload));
    	button->setIconSize(QSize(25,25));
    	button->setText("Short");
    	//button->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Maximum);
    	layout->addWidget(button);
    
    	layout->addStretch();
    
    	button = new QToolButton();
    	button->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
    	button->setIcon(window->style()->standardIcon(QStyle::SP_BrowserReload));
    	button->setIconSize(QSize(25,25));
    	button->setText("Long Long Long Long Long");
    	//button->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Maximum);
    	layout->addWidget(button);
    
    	layout->addStretch();
    
    	window->setLayout(layout);
    	window->show();
    
    	return a.exec();
    }
    

    QGridLayout works fine....
    0_1512582172066_right.png
    Code that made this below....

    #include <QWidget>
    #include <QGridLayout>
    #include <QToolButton>
    #include <QApplication>
    #include <QStyle>
    
    int main(int argc, char *argv[])
    {
    	QApplication a(argc, argv);
    
    	QWidget *window = new QWidget();
    	QGridLayout *layout = new QGridLayout();
    	QToolButton *button;
    
    	//layout->setAlignment(Qt::AlignHCenter);  // does nothing
    
    	layout->setRowStretch(0, 1);
    	layout->setRowStretch(2, 1);
    	layout->setRowStretch(4, 1);
    	layout->setColumnStretch(0, 1);
    	layout->setColumnStretch(2, 1);
    
    	button = new QToolButton();
    	button->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
    	button->setIcon(window->style()->standardIcon(QStyle::SP_BrowserReload));
    	button->setIconSize(QSize(25,25));
    	button->setText("Short");
    	layout->addWidget(button, 1, 1, Qt::AlignHCenter);
    
    	button = new QToolButton();
    	button->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
    	button->setIcon(window->style()->standardIcon(QStyle::SP_BrowserReload));
    	button->setIconSize(QSize(25,25));
    	button->setText("Long Long Long Long Long");
    	layout->addWidget(button, 3, 1, Qt::AlignHCenter);
    
    	window->setLayout(layout);
    	window->show();
    
    	return a.exec();
    }
    


  • Hi @drwho

    The problem is with this line :

    layout->setAlignment(Qt::AlignHCenter);

    You are calling this function before the addWidget function, and i think that with vertialLayout to use AlignHCenter , you need to tell which item you need to center,

    -just put setAlignement after the addWidget, and use setAlignement(button, Qt::AlignHCenter) instead of setAlgnement(Qt::AlignHCenter);

    You code should look to something like this:

    QWidget *window = new QWidget();
        QVBoxLayout *layout = new QVBoxLayout();
        QToolButton *button;
    
        layout->addStretch();
    
        button = new QToolButton();
        button->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
        button->setIcon(window->style()->standardIcon(QStyle::SP_BrowserReload));
        button->setIconSize(QSize(25,25));
        button->setText("Short");
        //button->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Maximum);
        layout->addWidget(button);
        layout->setAlignment(button, Qt::AlignHCenter);// Call setAlignement(button, Qt::AlignHCenter) after addWidget
        layout->addStretch();
    
        button = new QToolButton();
        button->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
        button->setIcon(window->style()->standardIcon(QStyle::SP_BrowserReload));
        button->setIconSize(QSize(25,25));
        button->setText("Long Long Long Long Long");
        //button->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Maximum);
        layout->addWidget(button);
        layout->setAlignment(button, Qt::AlignHCenter);// Call setAlignement(button, Qt::AlignHCenter) after addWidget
        layout->addStretch();
    
        window->setLayout(layout);
        window->show();
    

    I hope this can help you !



  • Yes, setAlignment(button, Qt::AlignHCenter) does the trick. Thanks! It is too bad the alignment parameter in QVBoxLayout's addWidget() doesn't do this like QGridLayout's addWidget() does.


Log in to reply
 

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