Avoiding ui->setupUi(this);



  • Hi I have the following files

    A form mainwindow.ui that contains a button named okButton

    main.cpp
    @#include "mainwindow.h"
    #include "ui_mainwindow.h"

    MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow){
    ui->setupUi(this);
    connect(ui->okButton,SIGNAL(clicked()),this,SLOT(accept()));

    }

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

    main.h
    @#include <QtGui/QApplication>
    #include "mainwindow.h"

    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);
    MainWindow w;
    w.show();

    return a.exec&#40;&#41;;
    

    }@

    Every time I want to acces and object from the form, I have to do it, as in the mainWindow constructor via ui->okButton. What changes do I need to do in order to access the okButton without having to use the ui pointer?

    Thanks in advance
    santiagorf



  • You can "inherit from the UI struct":http://qt-project.org/doc/qt-4.8/designer-using-a-ui-file.html#the-multiple-inheritance-approach, so it's members become part of your class



  • ... or you can construct your user interface using code you write for yourself.



  • ...or you can add a QPushButton* member variable in your class, and assign the ui->okButton to that variable directly after the call to setupUi.

    ...or you can learn to appreciate that your UI elements are all nicely grouped together in a ui member instead of cluttering up your internal API.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.