Qt add buttons to existing widget

11,511

The central widget default layout is called gridLayout and is a QGridLayout. You can add the new button to it like this:

ui->gridLayout->addWidget(new QPushButton("Button Text"), rowNumber, colNumber);

Here is an example:

mainwindow.h:

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

class QPushButton;

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

private:
    Ui::MainWindow *ui;
    QPushButton *newButton;
};

#endif // MAINWINDOW_H

mainwindow.cpp:

#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <QPushButton>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow),
    newButton(new QPushButton("Button 3"))
{
    ui->setupUi(this);

    const int rowNumber = 1;
    const int colNumber = 0;

    ui->gridLayout->addWidget(newButton, rowNumber, colNumber);
}

MainWindow::~MainWindow()
{
    delete ui;
}

mainwindow.ui:

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>MainWindow</class>
 <widget class="QMainWindow" name="MainWindow">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>400</width>
    <height>300</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>MainWindow</string>
  </property>
  <widget class="QWidget" name="centralWidget">
   <layout class="QGridLayout" name="gridLayout">
    <item row="0" column="0">
     <widget class="QPushButton" name="pushButton1">
      <property name="text">
       <string>Button 1</string>
      </property>
     </widget>
    </item>
    <item row="0" column="1">
     <widget class="QPushButton" name="pushButton2">
      <property name="text">
       <string>Button 2</string>
      </property>
     </widget>
    </item>
   </layout>
  </widget>
  <widget class="QMenuBar" name="menuBar">
   <property name="geometry">
    <rect>
     <x>0</x>
     <y>0</y>
     <width>400</width>
     <height>21</height>
    </rect>
   </property>
  </widget>
  <widget class="QToolBar" name="mainToolBar">
   <attribute name="toolBarArea">
    <enum>TopToolBarArea</enum>
   </attribute>
   <attribute name="toolBarBreak">
    <bool>false</bool>
   </attribute>
  </widget>
  <widget class="QStatusBar" name="statusBar"/>
 </widget>
 <layoutdefault spacing="6" margin="11"/>
 <resources/>
 <connections/>
</ui>

Good luck!

Share:
11,511
user3700021
Author by

user3700021

Updated on June 04, 2022

Comments

  • user3700021
    user3700021 almost 2 years

    I have added two pushButtons using qtDesigner, those buttons are chilldren of centralWidget, now I would like to add one more pushButton, but programmatically in mainwindow.cpp.

    When I use setCentralWidget method it removes or hides prevous 2 pushButtons which were added by qtDesigner. My question is how to add this additional button programmatically so those 2 pushButtons remains?

    I am beginner in Qt.

  • user3700021
    user3700021 over 9 years
    Hmm can you belive it, I dont have this QGridLayout in default. Should I add this somehow. I dont want to copy your mainwindow.ui code each time I open new project.
  • Iuliu
    Iuliu over 9 years
    @user3700021 It depends on what type of layout you selected for the centralWidget. It could be a horizontalLayout if you used a horizontal layout(QHBoxLayout) or a verticalLayout if you used a vertical layout(QVBoxLayout). In my example I used a grid layout, that's why the object is called gridLayout. You can try the generated *_ui.h file for more details on how the class is generated from the *.ui file.
  • user3700021
    user3700021 over 9 years
    So there is no possibility to add additionall button directly to the centralWidget? I must have a layout ?
  • Iuliu
    Iuliu over 9 years
    @user3700021 Of course there is possible if you don't have a layout but if you want your widgets to be positioned nicely I recommend you add a layout. Don't you have a layout? You must have a layout if you don't want your buttons to be positioned absolute on the form. If you want your buttons to be positioned absolute, which you don't, but if you want, you just have to create a new QPushButton pointer and pass the central widget as its parent. Then call setGeometry on it passing the values you want for x, y coordinates and widthe and height.
  • user3700021
    user3700021 over 9 years
    Thank you I just was spending to much time in c# and I did not have to use a thing like layouts now, I see its inseparable part in qt.
  • Iuliu
    Iuliu over 9 years
    @user3700021 It's not inseparable. It's just nice to have. As I said, just pass the centralWidget as parent to your button and the button will appear on the centralWidget. Alternatively you can call setParent(centralWidget) on the button. Then you can change its position with setGeometry or move. .NET also has layouts if you don't want your resizable windows to look ugly :)