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. Centering a QToolButton in a QVBoxLayout?
Forum Updated to NodeBB v4.3 + New Features

Centering a QToolButton in a QVBoxLayout?

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 4.4k 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
    drwho
    wrote on last edited by
    #1

    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();
    }
    
    1 Reply Last reply
    0
    • M Offline
      M Offline
      mostefa
      wrote on last edited by mostefa
      #2

      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 !

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

        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.

        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