-3

每当单击主窗口上的按钮时,我都会打开一个新窗口(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

和主窗口 .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 *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;


}
4

1 回答 1

0

我想我终于找到了答案。它与cubeview类构造函数的定义无关。简单的原则是 -> 我们不需要在循环中创建类对象或程序代码崩溃。-> 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,则创建新的对象窗口。

于 2014-12-19T11:34:50.620 回答