0

Any one idea how to get reference of QPushButton defined in QT .ui file.

We can create button QPushButton btnOK = new QPushButton("OK", this); this I know. But how can I link it with button reference in .ui file.

Any suggestion!

CoDe
  • 11,056
  • 14
  • 90
  • 197
  • `Ui::push_button_name` ? – t3ft3l--i Aug 04 '15 at 12:20
  • or `QPushButton* button = pWin->findChild("Button name");` where `pWin` is your class defined at `.ui`. – t3ft3l--i Aug 04 '15 at 12:22
  • 1
    Depending on `Tools->Options...->C++/Qt Class Generation/Embedding of the UI Class` mode it can be either `ui->pushButton`, `ui.pushButton` or just `pushButton`, where 'pushButton' is button's name in ui-file. – Amartel Aug 04 '15 at 12:25

1 Answers1

0

Say you have a MyWidget.ui file that describes a widget. In this widget, you have a push button named btnOk;

In your code, you typically have the following:

#include "ui_MyWidget.h"

class MyWidget : public QWidget
{
Q_OBJECT
public:
    MyWidget() : QWidget() {
        m_ui.setupUi(this);
        // now this is what you want:
        m_ui.btnOk->setText("Hello World!");
    }
private:
    Ui::MyWidget m_ui;
}

To make this work, you have to be sure to list your MyWidget.ui file in your qmake or cmake based project, so that the ui_MyWidget.h file is automatically created (make sure you have the include paths set correctly).

Further, if you do not want to find the QPushButton based on the variable name, you can also use QObject::findChild(). The string you need then is probably "btnOk", i.e. uic sets this name automatically to the objectName.

dhaumann
  • 1,590
  • 13
  • 25