2

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.

1
  • Can you paste your .ui file? Commented Jan 3, 2015 at 21:01

2 Answers 2

2

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!

Sign up to request clarification or add additional context in comments.

6 Comments

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.
@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.
So there is no possibility to add additionall button directly to the centralWidget? I must have a layout ?
@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.
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.
|
0

You can add QPushbutton to central widget by passing its object name.

QPushButton *PB=new QPushButton(ui->centralwidgetobjectname);
PB.show();

Otherwise create layout inside Central widget then using addwidget function to add Qpushbutton.

QGridLayout *GL=new QGridLayout(this);
QPushButton *PB=new QPushButton();
GL->addWidget(PB);

Set Geometry For Grid Layout.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.