Mary Chang
Mary Chang

Reputation: 955

QGraphicsView rendering its sub widget's sub widget weirdly

I'm working on a music/MIDI editor using C++ and Qt.
The develop was smooth until I met an issue that QGraphicsView is rendering weirdly with its child widget's child widget.
(wow, that's confusing)

This is the code I used to add my sub widgets to a QGraphicsView

pianoPlain = new QawPianoPlain();
keyBoard = new QawPianoKeyBoard();
scene->addWidget(pianoPlain);
scene->addWidget(keyBoard);


keyBoard->setGeometry(0,0,100,540);
keyBoard->setKeyNum(36);

pianoPlain->setGeometry(100,0,500,540);
pianoPlain->setStriptNum(36);

The code makes something like this.
pianoRoll -> keyBoard -> key
...............|-> pianoPlaom -> noteStript

Where pianoPlain and keyBoard are classes I inherit from GraphicsView and key and noteStript are both inherit from QWidget.

Here is a picture of what happens: https://drive.google.com/file/d/0BzYLJIsbhhuVZ0RMdzRqV01Cd2M/view?usp=sharing

And this is picture of a normal keyBoard which is placed directy in MainWindow(left) and a screwed up one placed in QGraphicsView(right)
(I have disable back/while key in this picture) https://drive.google.com/file/d/0BzYLJIsbhhuVSDFhSXRVM0dHWnM/view?usp=sharing

It seems that whenever pianoPlain and keyBoard are placed in a QGtraphicsView, it will be screwed.
Anyone know what's happening? How can I fix this?

Qt : 5.4.1
OS : KUubutu 15.04 AMD64
Compiler : clang 3.6.0/ GCC 4.9.2

Upvotes: 1

Views: 285

Answers (1)

kh25
kh25

Reputation: 1298

I would suggest pianoPlain and keyBoard should use a class that inherits QGraphicsItem - for example QGraphicsPixmapItem or QGraphicsProxyWidget.

You then would add these to the scene using your scene->addWidget() method.

The scene itself should then be part of a QGraphicsView.

For example:

QGraphicsView *view = new QGraphicsView();
QGraphicsScene* scene = new QGraphicsScene(view);

view->setScene(scene_);

// No need to parent these
pianoPlain = new QawPianoPlain();
keyBoard = new QawPianoKeyBoard();

// This will work if QawPianoPlain and QawPianoKeyBoard inherit QWidget
QGraphicsProxyWidget* pianoProxy = scene_->addWidget(pianoPlain);
QGraphicsProxyWidget* keyBoardProxy = scene_->addWidget(keyBoard );

Use the proxy widget for any transformations/animations.

Upvotes: 1

Related Questions