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. different size buttons on qlayout

different size buttons on qlayout

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

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

      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.

      1 Reply Last reply
      3

      • Login

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