PDA

View Full Version : How can we inherit ui?



vinayaka
30th May 2011, 12:51
how to inherit ui of the base class in derived class, now i can access memeber fuctions and member variables of base class:). what can i do to inherit the ui of base class?

stampede
30th May 2011, 13:18
If ui form is public or protected member of base class, then you can access it directly in derived class (as any other base class member).
In derived class constructor, you just call base class constructor in ctor initializer list:


Derived::Derived(QWidget * parent) : Base(parent){ ... }

If you initialize ui in Base class constructor, it will be initialized here as well.
If this does not helps you, provide more description for your problem.

vinayaka
30th May 2011, 13:42
i don't get it. it shows errors. can you plz describe more.

FelixB
30th May 2011, 13:49
i don't get it. it shows errors. can you plz describe more.

describe more? no. *you* have to describe more. it is not clear what you have and what exactly you want to achieve. maybe you could post a small piece of code...

vinayaka
30th May 2011, 14:15
//baseclass.h
#ifndef BASECLASS_H
#define BASECLASS_H

#include <QMainWindow>

namespace Ui {
class baseclass;
}

class baseclass : public QMainWindow
{
Q_OBJECT

public:
explicit baseclass(QWidget *parent = 0);
~baseclass();
// baseclass(int i = 10) : member(i){}
// int getMember() {return member;}
int i;
Ui::baseclass *ui;

public slots:
void on_pushButton_clicked();
void test();



private:


private slots:



private slots:
void on_lineEdit_cursorPositionChanged(int , int );
};

#endif // BASECLASS_H


//derived class
#ifndef BASECLASS_H
#define BASECLASS_H

#include <QMainWindow>

namespace Ui {
class baseclass;
}

class baseclass : public QMainWindow
{
Q_OBJECT

public:
explicit baseclass(QWidget *parent = 0);
~baseclass();
// baseclass(int i = 10) : member(i){}
// int getMember() {return member;}
int i;
Ui::baseclass *ui;

public slots:
void on_pushButton_clicked();
void test();



private:


private slots:



private slots:
void on_lineEdit_cursorPositionChanged(int , int );
};

#endif // BASECLASS_H


//baseclass.cpp
#ifndef BASECLASS_H
#define BASECLASS_H

#include <QMainWindow>

namespace Ui {
class baseclass;
}

class baseclass : public QMainWindow
{
Q_OBJECT

public:
explicit baseclass(QWidget *parent = 0);
~baseclass();
// baseclass(int i = 10) : member(i){}
// int getMember() {return member;}
int i;
Ui::baseclass *ui;

public slots:
void on_pushButton_clicked();
void test();



private:


private slots:



private slots:
void on_lineEdit_cursorPositionChanged(int , int );
};

#endif // BASECLASS_H

//mai.cpp
#ifndef BASECLASS_H
#define BASECLASS_H

#include <QMainWindow>

namespace Ui {
class baseclass;
}

class baseclass : public QMainWindow
{
Q_OBJECT

public:
explicit baseclass(QWidget *parent = 0);
~baseclass();
// baseclass(int i = 10) : member(i){}
// int getMember() {return member;}
int i;
Ui::baseclass *ui;

public slots:
void on_pushButton_clicked();
void test();



private:


private slots:



private slots:
void on_lineEdit_cursorPositionChanged(int , int );
};

#endif // BASECLASS_H


//derivedclass.cpp
#include "derivedclass.h"
#include "ui_derivedclass.h"
#include "baseclass.h"

derivedclass::derivedclass(QWidget *parent) :
baseclass(parent),
ui(new Ui::derivedclass)
{
ui->setupUi(this);
test();

QString v=QString::number(i);

ui->lineEdit->setText(v);


}

derivedclass::~derivedclass()
{

delete ui;
}

stampede
30th May 2011, 15:28
The same source is pasted few times.
If you want the base and derived class to use the same ui form, you don't have to initialize it in both classes constructors, just in base class:


baseclass::baseclass(QWidget * parent) : ui(new ui::baseclass)
{
...
}

...

derivedclass::derivedclass(QWidget *parent) :
baseclass(parent) // ui(new Ui::baseclass) and ui->setupUi() will be called here, no need to call it again
{
test();

QString v=QString::number(i);

ui->lineEdit->setText(v);
}

Anyway, it's not clear to me what do you want to achieve.