Qt-interest Archive, September 2002
Multiple QListViews in a QScrollView
Message 1 in thread
I've only been using QT a few months. I'm currently using version 3.0.5
under windows 2000.
Anyway, I am trying to design an application whereby two or more QListViews
of varying sizes are all navigated by a single vertical scrollbar (each with
their own individual horizontal scrollbar, if possible).
I was wondering if this was possible - I have been experimenting by adding
two QlistViews to a single QScrollView with little success apart from hiding
the vertical bars of each QListView.
Thanks in advance
Mark Stephens
Message 2 in thread
On Mon, 2 Sep 2002 15:43:05 +0100
Mark Stephens <mark.stephens@microtest.co.uk> wrote:
> Anyway, I am trying to design an application whereby two or more
> QListViews of varying sizes are all navigated by a single vertical
> scrollbar (each with their own individual horizontal scrollbar, if
> possible).
>
> I was wondering if this was possible - I have been experimenting by
> adding two QlistViews to a single QScrollView with little success apart
> from hiding the vertical bars of each QListView.
I am not sure whether this is what you want, but here is a small class I
slapped together quickly that will sync two scrollviews together
vertically - each view still has free horizontal scrolling. It should be
easy enough to adapt it for an arbitrary number of views.
--+--
class QSyncer : public QObject {
Q_OBJECT
public:
QSyncer(QScrollView* s1, QScrollView* s2);
public slots:
void moving1(int x, int y);
void moving2(int x, int y);
protected:
QScrollView* s1_;
QScrollView* s2_;
};
--+--
QSyncer::QSyncer(QScrollView* s1, QScrollView* s2) : s1_(s1), s2_(s2) {
connect(s1, SIGNAL(contentsMoving(int, int)), this, SLOT(moving1(int,
int)));
connect(s2, SIGNAL(contentsMoving(int, int)), this, SLOT(moving2(int,
int)));
}
void QSyncer::moving1(int x, int y) {
bool blocked = s2_->signalsBlocked();
s2_->blockSignals(true);
s2_->setContentsPos(s2_->contentsX(), y);
s2_->blockSignals(blocked);
}
void QSyncer::moving2(int x, int y) {
bool blocked = s1_->signalsBlocked();
s1_->blockSignals(true);
s1_->setContentsPos(s1_->contentsX(), y);
s1_->blockSignals(blocked);
}
--+--
#include <qapplication.h>
#include <qhbox.h>
#include <qlistview.h>
#include "qsyncer.h"
int main(int argc, char** argv) {
QApplication app(argc, argv);
QHBox* box = new QHBox;
app.setMainWidget(box);
QListView* list1 = new QListView(box);
list1->addColumn("Foo");
QListView* list2 = new QListView(box);
list2->addColumn("Bar");
for (int i = 0; i < 20; i++) {
QString s;
s.sprintf("Item #%d for list 1", i);
new QListViewItem(list1, s);
s.sprintf("Item #%d for list 2", i);
new QListViewItem(list2, s);
}
new QSyncer(list1, list2);
box->show();
return app.exec();
}
--
[ signature omitted ]