我实现了一个用我的图形项填充场景的类。我想从我的类到主窗口返回指向场景的指针。但出于某种原因,当我尝试这样做时,图形视图并没有显示任何内容。如果填充功能在主窗口中正常工作,则填充功能正常工作。
如何将该函数保留在类中并将场景指针返回到主窗口?
这是我的班级标题:
#ifndef KEYBOARD_H
#define KEYBOARD_H
#include <QGraphicsScene>
#include <QVector>
#include <pianokey.h>
class keyboard
{
public:
keyboard(QObject *parent);
void highLightKeys(QVector<int> highVec);
void resetHighLight();
QGraphicsScene* getScene() {return scene;}
/*~keyboard()
{
while(!vec.empty())
{
delete vec.back();
vec.pop_back();
}
}*/
private:
QGraphicsScene* scene;
QVector<QSharedPointer<pianoKey> > vec;
};
#endif // KEYBOARD_H
这是cpp:
#include "keyboard.h"
#include "pianokey.h"
#include <QVector>
#include <QSharedPointer>
keyboard::keyboard(QObject * parent)
{
scene=new QGraphicsScene(parent);
vec.resize(14);
int black[]{2,4,7,9,11,14,16,19,21,23};
for(int i=0; i<14;i++)
{
QSharedPointer<pianoKey> temp(new pianoKey(i*WHITE_WIDTH,0));
scene->addItem(temp.data());
vec[i]=temp;
}
//PLEASE DON'T JUDGE ME ON THIS I SWEAR I'LL THINK OF SOMETHING
QSharedPointer<pianoKey> black1(new pianoKey(true,25,0));
scene->addItem(black1.data());
vec.insert(2,black1);
QSharedPointer<pianoKey> black2(new pianoKey(true,55,0));
scene->addItem(black2.data());
vec.insert(4,black2);
QSharedPointer<pianoKey> black3(new pianoKey(true,115,0));
scene->addItem(black3.data());
vec.insert(7,black3);
QSharedPointer<pianoKey> black4(new pianoKey(true,145,0));
scene->addItem(black4.data());
vec.insert(9,black4);
QSharedPointer<pianoKey> black5(new pianoKey(true,175,0));
scene->addItem(black5.data());
vec.insert(11,black5);
QSharedPointer<pianoKey> black6(new pianoKey(true,235,0));
scene->addItem(black6.data());
vec.insert(14,black6);
QSharedPointer<pianoKey> black7(new pianoKey(true,265,0));
scene->addItem(black7.data());
vec.insert(16,black7);
QSharedPointer<pianoKey> black8(new pianoKey(true,325,0));
scene->addItem(black8.data());
vec.insert(19,black8);
QSharedPointer<pianoKey> black9(new pianoKey(true,355,0));
scene->addItem(black9.data());
vec.insert(21,black9);
QSharedPointer<pianoKey> black10(new pianoKey(true,385,0));
scene->addItem(black10.data());
vec.insert(23,black10);
}
void keyboard::highLightKeys(QVector<int> highVec)
{
for(int i=0; i<highVec.size();i++)
{
int tempInt = highVec[i];
vec[tempInt]->highlight();
}
}
void keyboard::resetHighLight()
{
for(int i=0;i<vec.size();i++)
vec[i]->resetHighlight();
}
更新:发布我的主窗口代码:
部首:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QGraphicsScene>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
Ui::MainWindow *ui;
QGraphicsScene *scene;
void testfunc();
};
#endif // MAINWINDOW_H
CPP:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "pianokey.h"
#include "keyboard.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
keyboard piano(this);
scene = piano.getScene();
ui->graphicsView->setScene(scene);
}
MainWindow::~MainWindow()
{
delete ui;
}
答案 0 :(得分:1)
问题是在MainWindow的构造函数中,您在堆栈上创建keyboard
。在这个构造函数的末尾,调用keyboard
的析构函数,因为你使用的是智能指针,它会破坏所有new pianoKey(..);
,因此将它们从场景中删除。 (这是因为Qt很聪明,当你在delete
上调用QGraphicsSceneItem
时,会在释放内存之前将其从场景中删除)
只需改用:
QVector<pianoKey*> vec;
无论如何都会在删除场景时巧妙地删除所有项目,因此在这种特定情况下不建议使用智能指针。