Reputation: 89
I am developing a game using C++ in Qt creator.
I can call the fuctions of the child class in main without an issue, but when I try to call a function in child class from my parent class, the program unexpectedly closes.
I have declared all the public attributes in the .h file of the parent class. T tried removing those attributes and placing them inside the .h file of the child class, then it works fine. But W want to keep them in the parent class and access those from the child class.
Please help get this to work, I tried several times but I could not figure it out.
.h file of parent class:
class Game:public QGraphicsView,public home{
Q_OBJECT
public:
Game(QWidget* parent=0);
virtual ~Game(){}
void createBlockCol(double x);
void creatBlockGrid();
virtual void setbackground();
QPushButton *btn2;
QPushButton *btn3;
QPushButton *btn4;
QGraphicsScene* scene;
QGraphicsScene *scene2;
QGraphicsView* view;
QPushButton *btn;
QPushButton *btn1;
QGraphicsProxyWidget *pr;
QGraphicsProxyWidget *pr1;
QGraphicsProxyWidget *pr2;
QGraphicsProxyWidget *pr3;
QGraphicsProxyWidget *pr4;
QGraphicsPixmapItem* item1;
QGraphicsPixmapItem* item;
private slots:
void start();
This is .cpp class of parent
Game::Game(QWidget *parent): QGraphicsView(parent){
scene = new QGraphicsScene(0,0,466,600);
setScene(scene);
}
void Game::setbackground(){
scene2 = new QGraphicsScene(0,0,400,600);
QGraphicsView* view = new QGraphicsView(scene2);
item = new QGraphicsPixmapItem(QPixmap(":/Ies/uu.jpg"));
scene2->addItem(item);
view->show();
game_function *gm; //call a function in child class
gm->set_buttons();
}
child class .h file
class game_function:public Game
{
Q_OBJECT
public:
void set_background();
void mainwindow();
void set_buttons();
private slots:
void button_help();
};
.cpp file
void game_function::set_buttons(){
btn = new QPushButton;
QObject::connect(btn,SIGNAL(clicked()),this,SLOT(startgame()));
btn->setStyleSheet("background-color: white");
pr = this->scene2->addWidget(button);
btn->setAutoFillBackground(true);
btn->setIcon(QIcon(":/Ies/main.png"));
btn->setIconSize(QSize(131,41));
pr->setPos(130,430);
btn1 = new QPushButton;
QObject::connect(btn1,SIGNAL(clicked()),this,SLOT(stopgame()));
btn1->setStyleSheet("background-color: white");
pro = this->scene2->addWidget(button1);
btn1->setAutoFillBackground(true);
btn1->setIcon(QIcon(":/Images/exit.png"));
btn1->setIconSize(QSize(131,41));
pr->setPos(130,500);
Upvotes: 0
Views: 457
Reputation: 5566
is there ay way i can call this function
You might not be too far from trying a polymorphic invocation ...
perhaps if you
A) add "Game::set_buttons()", and make it virtual, perhaps pure virtual, somewhere in class header:
class Game:public QGraphicsView,public home{
// ... perhaps after
virtual ~Game(){}
virtual void setbuttons() = 0;
// ...
} // end of class Game
then, in method "Game::setbackground()" you can use it ... replace this
// undefined behavior - gm not initialized
//game_function *gm; //call a function in child class
//gm->set_buttons();
with
this->set_buttons();
Upvotes: 2