PDA

View Full Version : Scrolling 2 QGraphicsViews with 1 scrollbar



dentist
13th April 2010, 13:23
Hi, i have a scrolling issue. I read lots of docs, googled, etc - but couldn't figure it out. The idea is to scroll 2 QGraphicsViews with the scrollbar of one of them. This is what i did:



class QPianoRoll : public QWidget {

Q_OBJECT

public:
QPianoRoll(){

currentScrollPosition = 0;

QHBoxLayout *layout = new QHBoxLayout;

// both, QNoteEditor and QVerticalPiano are derived from QGraphicView

piano = new QVerticalPiano();
layout->addWidget(piano);

QNoteEditorCore * editor = new QNoteEditorCore();
wrapper = new QNoteEditor(editor);
QScrollBar * scrollbar = wrapper->verticalScrollBar();
connect(scrollbar, SIGNAL(valueChanged(int)),this,SLOT(scrollRoll(int )));

layout->addWidget(wrapper,2);
layout->setAlignment(Qt::AlignLeft);
setLayout(layout);

}

private :
QNoteEditor * wrapper;
QVerticalPiano * piano;
int currentScrollPosition;

public slots :
void scrollRoll(int value) {
int relative = currentScrollPosition - value;
piano->scroll(0,relative);
currentScrollPosition = value;
}

};


It's quite the same implementation as introduced here (http://qt.nokia.com/developer/faqs/faq.2007-06-08.0800842041), but unfortunately it doesn't work. The slot scrollRoll just moves the visible scene up and down and does not scroll any contents. What am I doing wrong ?

JohannesMunk
14th April 2010, 00:25
Hi!

Untested, but why don't you set the value of the pianos vertical scrollbar directly?


connect(wrapper->verticalScrollBar(), SIGNAL(valueChanged(int)),piano->verticalScrollBar(),SLOT(setValue(int)));

This requires them to have the same ranges though. But if you want to move synchronize their scrolling they should have!

HIH

Johannes

norobro
14th April 2010, 02:12
I agree with Joh about connecting the scroll bars directly, but you might also try the following in your slot:
piano->viewport()->scroll(0,relative);