C++ (Qt)scrollArea::scrollArea(QWidget *parent, Qt::WFlags flags) : QMainWindow( parent, flags ){ ui.setupUi(this); TMyScrollArea *pScrollArea = new TMyScrollArea( this ); TMyWidget *pWidget = new TMyWidget(this); pScrollArea->attach( pWidget ); //pScrollArea->setViewport( pWidget ); setCentralWidget( pScrollArea );}
C++ (Qt)class TMyWidget : public QWidget{ Q_OBJECTpublic: TMyWidget(QWidget *parent = 0):QWidget(parent){}; ~TMyWidget(){}; QSize sizeHint() const { return size(); };protected: virtual void paintEvent( QPaintEvent *event_ ) { QWidget::paintEvent( event_ ); QPainter painter; painter.begin(this); painter.drawText( 10, 10, tr("some text...") ); };private:};
C++ (Qt)class TMyScrollArea : public QAbstractScrollArea{ Q_OBJECT public: TMyScrollArea(QWidget *parent = 0) : QAbstractScrollArea( parent ){}; ~TMyScrollArea(){}; void attach( QWidget *widget_ ) { m_pWidget = widget_; } protected: virtual bool viewportEvent( QEvent *event_ ) { QSize areaSize = viewport()->size(); QSize widgetSize = m_pWidget->size(); verticalScrollBar()->setPageStep(widgetSize.height()); horizontalScrollBar()->setPageStep(widgetSize.width()); verticalScrollBar()->setRange(0, widgetSize.height() - areaSize.height()); horizontalScrollBar()->setRange(0, widgetSize.width() - areaSize.width()); updateWidgetPosition(); viewport()->update(); return QAbstractScrollArea::viewportEvent( event_ ); }; void updateWidgetPosition(void) { int hvalue = horizontalScrollBar()->value(); int vvalue = verticalScrollBar()->value(); QPoint topLeft = viewport()->rect().topLeft(); m_pWidget->move(topLeft.x() - hvalue, topLeft.y() - vvalue); }private: QWidget *m_pWidget;};