gpt4 book ai didi

c++ - 在一个类中填充一个QGraphicsScene并返回场景指针

转载 作者:行者123 更新时间:2023-11-30 05:43:04 25 4
gpt4 key购买 nike

我正在实现一个用我的图形项目填充场景的类。我想从我的类(class)返回一个指向场景的指针到主窗口。但是由于某种原因,当我尝试这样做时,图形 View 没有显示任何内容。如果填充功能在主窗口中,则填充功能正常工作。

如何将函数保留在类中并将场景指针返回到主窗口?

这是我的类(class)标题:

#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;
}

最佳答案

问题是在 MainWindow 的构造函数中,您在堆栈上创建了一个 keyboard。在这个构造函数的最后,调用了keyboard的析构函数,因为你使用的是智能指针,它会析构所有的new pianoKey(..);,因此删除他们从现场。 (这是因为 Qt 很聪明,当您在 QGraphicsSceneItem 上调用 delete 时,它会在释放内存之前将其从场景中删除)

只需使用:

QVector<pianoKey*> vec;

无论如何删除场景时,所有项目都会被智能删除,因此在这种特定情况下不建议使用智能指针。

关于c++ - 在一个类中填充一个QGraphicsScene并返回场景指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30334714/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com