Creating a cubeview object (qt3d) in mainWindow.cpp file
-
@#include <QApplication>
#include<mainwindow.h>
#include <iostream>int main(int argc, char *argv[])
{
QApplication app(argc, argv);
MainWindow mainWin;
mainWin.resize(800, 600);
mainWin.show();
return app.exec();
}
@
And my MainWindow .cpp file
@#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "cubeview.h"MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
CubeView view;
}MainWindow::~MainWindow()
{
delete ui;
}@But, I get a runtime error when i try to to create a cubeview class object in my mainwindow.cpp file
Cubeview is a graphics class which creates(one that comes with qt3d) cube on scree. This object works perfectly when created in the main.cpp file. What is the problem here?
-
I found an answer. We cannot create the object in a loop of a slot.
@
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
view=0;
//create the icons for all the UI push button and resize the buttons
ui->pushButton->setIcon(QIcon(":/pattern4.jpg"));
ui->pushButton->resize(buttonsize);
}void MainWindow::on_pushButton_clicked()
{
//if this is the first button to be clicked on the UI
if(view==NULL)
{
view=new CubeView(ui->centralwidget);
view->begin(4);
view->resize(800, 600);
view->show();
}
//for all later clicks
else if(view!=NULL)
{
std::cout<<"A window is already open"<<std::endl;
view->close();//close the already open window
view->begin(4);//begin graphics & haptics loop
view->resize(800, 600);
view->show();
}
}MainWindow::~MainWindow()
{
delete ui;
if(view!=NULL){
std::cout<<"view object deleted"<<std::endl;
delete view;}
;
}
@