Home > Back-end >  Qt QPushbutton not added by using .ui
Qt QPushbutton not added by using .ui

Time:12-06

I'm trying to add new QPushbutton in my mainwindow.ui. I added Qpushbutton by dragging in .ui, and in the right side I can see it added properly. enter image description here

(I added btnDownload button, and btnQuit is added long time ago. it works fine)

I clicked go to slot on btnDownload and it created this code:


void MainWindow::on_btnDownload_clicked()
{
    QMessageBox::information(this,"notify","button is clicked "); //this part is what I added
}

however, when I build my project I don't see this button added in the place I located it. So to work around, I tried to use setGeometry function in mainwindow.cpp file to locate this button.

this is a code I tried to wrote:

ui->btnDownload->setGeometry(x,y,wide,height);

but I get a warning message saying:

no memeber named 'btnDownload' in Mainwindow::ui

I did add QPushbutton in .ui, and created the connected function, but it cannot find this button.. how should I solve this?

CodePudding user response:

I think I fixed it. I compared it with btnQuit button (which works fine) and the difference was that the new button was not clarified in header file. I'm going to fix it and I think it will works. If it doesn't, I'll add it.

CodePudding user response:

Look into the .ui file. It's XML. You should see something like this

  <widget class="QWidget" name="centralwidget">
   <widget class="QPushButton" name="pushButton">
    <property name="geometry">
     <rect>
      <x>130</x>
      <y>150</y>
      <width>75</width>
      <height>23</height>
     </rect>
    </property>
    <property name="text">
     <string>PushButton</string>
    </property>
   </widget>
  </widget>

On the build directory typically at <workspace>\build-<project>-Desktop_Qt_6_2_0_MSVC2019_64bit-Debug\<project>_autogen\include you should find a file ui_mainwindow.h with the contents

#include <QtWidgets/QMainWindow>
#include <QtWidgets/QMenuBar>
#include <QtWidgets/QPushButton>
#include <QtWidgets/QStatusBar>
#include <QtWidgets/QWidget>

QT_BEGIN_NAMESPACE

class Ui_MainWindow
{
public:
    QWidget *centralwidget;
    QPushButton *pushButton; //<<======= this
    QMenuBar *menubar;
    QStatusBar *statusbar;

    void setupUi(QMainWindow *MainWindow)
    {

Right below you should find the instantiation of your button

    void setupUi(QMainWindow *MainWindow)
    {
        if (MainWindow->objectName().isEmpty())
            MainWindow->setObjectName(QString::fromUtf8("MainWindow"));
        MainWindow->resize(800, 600);
        centralwidget = new QWidget(MainWindow);
        centralwidget->setObjectName(QString::fromUtf8("centralwidget"));
        pushButton = new QPushButton(centralwidget);  // <<<============= this
        pushButton->setObjectName(QString::fromUtf8("pushButton"));
        pushButton->setGeometry(QRect(130, 150, 75, 23));
        MainWindow->setCentralWidget(centralwidget);
  • Related