How to use Qt inheritance not only for the ui , but for the whole Project
Iam using an Integer Value i in Base class and created another class Inherited from the base class . In the constructor of base class , i is assigned with value =2510,
that value can be displayed in the derived class .
But the problem is that Integer i cannot be assigned value in the pushbutton click function of the baseclass, instead the old value is showing ........
the header files and source files are given below
Code:
//baseclass.h
#ifndef BASECLASS_H
#define BASECLASS_H
#include <QMainWindow>
namespace Ui {
class baseclass;
}
{
Q_OBJECT
public:
explicit baseclass
(QWidget *parent
= 0);
~baseclass();
protected:
int i;
protected slots:
void on_pushButton_clicked();
private:
Ui::baseclass *ui;
private slots:
private slots:
void on_lineEdit_cursorPositionChanged(int , int );
};
#endif // BASECLASS_H
Code:
//derived.h
#ifndef DERIVEDCLASS_H
#define DERIVEDCLASS_H
#include <baseclass.h>
#include <QMainWindow>
namespace Ui {
class derivedclass;
}
class derivedclass : public baseclass
{
Q_OBJECT
public:
explicit derivedclass
(QWidget *parent
= 0);
~derivedclass();
private:
Ui::derivedclass *ui;
};
#endif // DERIVEDCLASS_H
baseclass.cpp
Code:
#include "baseclass.h"
#include "ui_baseclass.h"
#include "inherited.h"
#include "derivedclass.h"
baseclass
::baseclass(QWidget *parent
) : ui(new Ui::baseclass)
{
ui->setupUi(this);
i=2510;
}
baseclass::~baseclass()
{
delete ui;
}
void baseclass::on_pushButton_clicked()
{
QString Value
=ui
->lineEdit
->text
();
i=Value.toInt();
derivedclass *in=new derivedclass;
in->show();
this->hide();
}
void baseclass::on_lineEdit_cursorPositionChanged(int , int )
{
QString Value
=ui
->lineEdit
->text
();
ui->label_2->setText(Value);
//i= Value.toInt();
}
DERIVED.CPP
Code:
#include "derivedclass.h"
#include "ui_derivedclass.h"
#include "baseclass.h"
derivedclass
::derivedclass(QWidget *parent
) : baseclass(parent),
ui(new Ui::derivedclass)
{
ui->setupUi(this);
ui->lineEdit->setText(v);
}
derivedclass::~derivedclass()
{
delete ui;
}
i JUST wANTED TO HAVE VALUE IN BASE CLASS ACCESSIBLE AND ALTERABLE TO DERIVED CLASS
Re: How to use Qt inheritance not only for the ui , but for the whole Project
The first thing that I would check is that your on_pushButton_clicked() click handle fires. I gues people often spend hours in finding problems in code that never gets executed.
Re: How to use Qt inheritance not only for the ui , but for the whole Project
To OP:
In your slot, you create a new instance of your derived class, that new instance of course has your hard coded 'i' initialization in it, which is what you see.
It looks to me, you are confused as to what inheritance really does.
My suggestion to you:
Leave Qt for now, and practice and learn C++ until you are fully proficient with it.
Only then come back to Qt, otherwise you will just get frustrated.
Re: How to use Qt inheritance not only for the ui , but for the whole Project
Your problem has nothing to do with Qt. Actually you don't have a problem at all. "i" can be modified from within methods defined in both classes. Your real problem is that you forgot to learn object oriented programming.