每当在主窗口上点击一个按钮时,我就打开一个新窗口(qt3d应用程序中带有cubeview视图的窗口)。
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include "cubeview.h"
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
int getid();
public slots:
void pattern1();
private:
CubeView *view;
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
和mainwindow .cpp文件
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QPushButton>
#include <iostream>
#include "cubeview.h"
int pattern_id;
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QPushButton *button= new QPushButton("Pattern 1");
QObject::connect(button, SIGNAL(clicked()), this, SLOT(pattern1()));
button->show();
std::cout<<"reached mainwindow constructor"<<std::endl;
//view= new CubeView;
}
void MainWindow::pattern1()
{
pattern_id=1;
view->begin(1);
view->resize(800, 600);
view->show();
std::cout<<pattern_id<<std::endl;
}
int MainWindow::getid()
{
return pattern_id;
}
MainWindow::~MainWindow()
{
delete ui;
delete view;
}
我收到运行时错误。
我希望你得到我想做的事。每当我点击按钮时,都会显示cubeview视图窗口。我犯的错是什么? 我应该在哪里定义cubeView类对象,以便以后可以使用它。 我可以将其初始化为CubeView * view = new CubeView;在头文件中。 我试着在mainwindow.cpp的构造函数中编写它 但我得到一个运行时错误。
#ifndef CUBEVIEW_H
#define CUBEVIEW_H
#include "qglview.h"
#include "qgltexture2d.h"
QT_BEGIN_NAMESPACE
class QGLSceneNode;
QT_END_NAMESPACE
//! [1]
class CubeView : public QGLView
{
//! [1]
Q_OBJECT
public:
CubeView(QWidget *parent = 0);
~CubeView();
void begin(int pattern_id);
public slots:
void update_action();
protected:
void paintGL(QGLPainter *painter);
//! [2]
private:
QGLSceneNode *cube;
QGLSceneNode *cursor;
QGLTexture2D logo;
QGLTexture2D* texture;
QGLTexture2D handcursor;
};
//! [2]
#endif
//! [1] constructor, initialize the cube, cursor and camera
CubeView::CubeView(QWidget *parent)
{
//! [1] draw the paintboard
QVector3D *cube1_pos= new QVector3D(0.0,0.0,-1.5);
QGLBuilder builder1;
builder1 << QGL::Faceted;
builder1 << QGLCube(3.25);
cube = builder1.finalizedSceneNode();
cube->setPosition(*cube1_pos);
//draw the cursor
QGLBuilder cursor_builder;
cursor_builder <<QGL::Faceted;
cursor_builder <<QGLCube(0.15);
cursor=cursor_builder.finalizedSceneNode();
//camera setup
camera()->setFieldOfView(35);
camera()->setNearPlane(1);
camera()->setFarPlane(15);
//! [2] set texture for cube and cursor
//QImage image(QLatin1String(":/bluecircle.jpg"));
handcursor.setImage(QImage(QLatin1String(":/hand.jpg")));
std::cout<<"constructor called"<<std::endl;
}
答案 0 :(得分:0)
我想我终于找到了答案。它与cubeview类构造函数的定义无关。简单的原则是 - &GT;我们不需要在循环中创建一个类对象或程序代码崩溃。 - &GT;另外view.show如果直接放在一个连接槽内,将会遇到无限循环的错误。 这是答案。
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QPushButton>
#include <iostream>
#include "cubeview.h"
int pattern_id;
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
view=0;
std::cout<<"reached mainwindow constructor"<<std::endl;
QPushButton *button= new QPushButton("Pattern 1");
QObject::connect(button, SIGNAL(clicked()), this, SLOT(pattern1()));
button->show();
else if(view!=NULL)
{
std::cout<<"view is already initialized"<<std::endl;
}
}
void MainWindow::pattern1()
{
if(view==NULL)
{
view=new CubeView;
view->begin(1);
view->resize(800, 600);
view->show();
}
}
int MainWindow::getid()
{
return pattern_id;
}
MainWindow::~MainWindow()
{
delete ui;
delete view;
}
这样,如果值已经为NULL,则会创建新的对象窗口。