- All (478)
- jom (0)
- Qt Linguist (7)
- Qt Eclipse Integration (9)
- Qt Designer (7)
- Qt Creator (4)
- Qt build system: qmake (31)
- Qt build system: configure (3)
- Qt Assistant (5)
- Printing (4)
- Porting from Qt 3 to Qt 4 (1)
- Plugins (7)
- Qt Visual Studio AddIn (2)
- Qt/MFC Migration (2)
- QtScript (3)
- MDI (2)
- XML (1)
- Widgets (22)
- WebKit (5)
- Tools and Containers (2)
- Threads (2)
- Text Handling (10)
- SQL (6)
- QtTest (1)
- QtService (1)
- Platform: Windows (49)
- Platform: Unix (16)
- Platform: Mac OS X (18)
- Image Formats (2)
- I/O (2)
- Graphicsview (8)
- Font handling (9)
- Event System (18)
- Drag and Drop (4)
- Dialogs (6)
- Desktop integration (3)
- ActiveQt (3)
- Itemviews (60)
- Layout (4)
- Qt Quick (10)
- Qt SDK (1)
- Licensing (2)
- Platform: Embedded Linux (38)
- Painting (32)
- OpenGL (4)
- Object Model (6)
- Network (5)
- Multimedia (3)
- Miscellanous (23)
- Main Window (19)
- Look and Feel (23)
- Development (0)
- Getting Involved (0)
- Routines (0)
How can I use one horizontal scrollbar to scroll several QGraphicsViews?
In order to use one scrollbar to scroll several views, you can put your views and a QScrollBar [doc.qt.nokia.com] inside a widget and connect the scrollbar’s value changed signal to the setValue() [doc.qt.nokia.com] slots of the hidden horizontal scrollbars. In addition the ranges need to be kept in sync, although you can easily change this if you want to make it a bit smarter. See the following example for a demonstration:
- #include <QtGui>
- {
- Q_OBJECT
- public:
- {
- layout->addWidget(view1);
- layout->addWidget(view2);
- vertical->addWidget(subWidget);
- vertical->addWidget(scrollbar);
- scrollbar->setRange(view1->horizontalScrollBar()->minimum(), view1->horizontalScrollBar()->maximum());
- connect(scrollbar, SIGNAL(valueChanged(int)), view1->horizontalScrollBar(), SLOT(setValue(int)));
- connect(scrollbar, SIGNAL(valueChanged(int)), view2->horizontalScrollBar(), SLOT(setValue(int)));
- connect(view1->horizontalScrollBar(), SIGNAL(rangeChanged(int, int)), this, SLOT(updateRange(int, int)));
- }
- public slots:
- void updateRange(int min, int max)
- {
- scrollbar->setRange(min, max);
- }
- private:
- };
- #include "main.moc"
- int main(int argc, char** argv)
- {
- Widget widget;
- widget.show();
- return app.exec();
- }

4 comments
November 22, 2010
Lab Rat
That works if the views are scrolled only using the scroll bar. Using the cursor keys
up/down/pgup/pgdown or <ALT>+mousewheel causes the views to become out of sync.
The QGraphicsViews need their scroll bars cross connected with valueChange/setValue functions in addition to connecting to the master scroll bar. The connect code shown above should be:
connect(scrollbar, SIGNAL), view1->horizontalScrollBar(), SLOT));
//connect(scrollbar, SIGNAL), view2->horizontalScrollBar(), SLOT));
connect(view1->horizontalScrollBar(), SIGNAL), this, SLOT));
connect(view1->horizontalScrollBar(),SIGNAL), view2->horizontalScrollBar(),SLOT));
connect(view2->horizontalScrollBar(),SIGNAL), view1->horizontalScrollBar(),SLOT));
connect(view1->horizontalScrollBar(), SIGNAL), scrollbar, SLOT));
The views are smart enough to prevent infinite signal recursion on setValue
November 22, 2010
Lab Rat
Let’s try again. The formatter made the code unreadable.
The code should read:
connect ( scrollbar, SIGNAL ( valueChanged ( int ) ) , view1->horizontalScrollBar ( ), SLOT ( setValue ( int ) ) ) ;
connect ( view1->horizontalScrollBar ( ) , SIGNAL ( rangeChanged ( int, int ) ) , this, SLOT ( updateRange ( int, int ) ) ) ;
connect ( view1->horizontalScrollBar ( ) , SIGNAL ( valueChanged ( int ) ) , view2->horizontalScrollBar ( ) , SLOT ( setValue ( int ) ) ) ;
connect ( view2->horizontalScrollBar ( ) , SIGNAL ( valueChanged ( int ) ) , view1->horizontalScrollBar ( ) , SLOT ( setValue ( int ) ) ) ;
connect ( view1->horizontalScrollBar ( ) , SIGNAL ( valueChanged ( int ) ) , scrollbar, SLOT ( setValue ( int ) ) ) ;
December 4, 2010
Lab Rat
Formatting won’t work in here Jesse. We get what you’re saying though :)
I don’t know if it’s good to rely on infinite recursion detection. Doesn’t it cause a slow down?
December 13, 2010
Lab Rat
I have not seen any slowdown. Scrolling response is instantaneous and there is no duplicate drawing occurring in either GraphicsView.