Avoiding ui->setupUi(this);
-
wrote on 19 Sept 2012, 20:10 last edited by
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();
}@
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 -
wrote on 19 Sept 2012, 20:14 last edited by
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
-
wrote on 20 Sept 2012, 03:30 last edited by
... or you can construct your user interface using code you write for yourself.
-
wrote on 20 Sept 2012, 07:53 last edited by
...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.
1/4