1

I'm implementing a class filling up a scene with my graphics item. I want to return a pointer to the scene from my class to main window. But for some reason when I try to do it, graphics view doesn't show anything. And the filling up function works normally if it's in the main window.

How can I keep the function in the class and return the scene pointer to main window?

Here's my class header :

#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

Here's the 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();
}

UPDATE : Posting my main window code too:

Header:

#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;
}
IAmInPLS
  • 4,051
  • 4
  • 24
  • 57
Ivan
  • 16,536
  • 6
  • 23
  • 36
  • This code looks fine, can you add the code where you call getScene(), and bind the scene to the view? – Boris Dalstein May 19 '15 at 19:47
  • @Boris I've added the mainwindow code in the question. It's all super simple actually, but doesn't work for some reason. – Ivan May 19 '15 at 19:53

1 Answers1

1

The issue is that in the constructor of MainWindow, you create a keyboard on the stack. At the end of this constructor, the destructor of keyboard is called, and because you are using smart pointers, it destructs all the new pianoKey(..);, therefore remove them from the scene. (This is because Qt is smart, when you call delete on a QGraphicsSceneItem, it removes it from the scene before deallocating the memory)

just use instead:

QVector<pianoKey*> vec;

All the items will be smartly deleted when the scene is deleted anyway, so using smart pointers is not advisable in this specific case.

Boris Dalstein
  • 7,015
  • 4
  • 30
  • 59
  • Boris, I love you. I knew it was something idiotic like this. Thank you very much. – Ivan May 19 '15 at 20:03
  • @woolywhale You are welcome :-) As a free advice, in this situation the cleanest way to organize the code is to have your class Keyboard inheriting QGraphicsScene. In the constructor of Keyboard, call things like `addItem(new PianoKey(...));`, then in the constructor of MainWindow, simply do `graphicsView->setScene(new Keyboard());` – Boris Dalstein May 19 '15 at 20:06