Search code examples
c++qtqwidgetqtguiqscrollarea

Cannot get QScrollArea to work


I have a QDeclarativeView that I want to put in a QScrollArea the problem is that the scrollarea does not work. It doesnt matter how big I set the declarative view. I do not get the scrollbar it is like it cant tell that the view need a scrollbar. If i dont set setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn); the scrollbar does not appear.

Parent are inserted in a borderlayout as the centralwidget – I use this layout http://qt-project.org/doc/qt-4.8/layouts-borderlayout.html

myWidgets *editWidget = new myWidgets(pathToExe,viewerMgr, this);
editWidget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);

Here is myWidget:

QScrollArea* scroll = new QScrollArea();
    view = new QDeclarativeView(this);
    view->setSource(QUrl::fromLocalFile(path));
    view->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);

    view->setResizeMode(QDeclarativeView::SizeViewToRootObject);
    scroll->setWidget(view);
    scroll->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
    scroll->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
    scroll->setWidgetResizable(true);

I have tried different size on the content but it's like the qscrollview doesnt detect when my widget is bigger then the visible view of qscrollarea.


Solution

  • I have solved this problem, I created a help class that holds the qscrollarea and the fix is to update qscrollarea viewport. here is the code for the helpwidget

    testHelpWidget::testHelpWidget(QString path,viewerManager *cMgr, QWidget *parent)
    : QWidget(parent)
    {
    QVBoxLayout *layout = new QVBoxLayout;
    scroll = new QScrollArea(this);
    
    testWidget = new testWidgets(path,cMgr,this);
    testWidget->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
    QObject::connect(testWidget,SIGNAL(zoomChanged()),this,SLOT(repaintZoom()));
    scroll->setWidget(qmlWidget);
    scroll->setWidgetResizable(true);
    scroll->setAlignment(Qt::AlignLeft);
    layout->addWidget(scroll);
    setLayout(layout);
    }
    
    void testHelpWidget::repaintZoom(){
    scroll->viewport()->updateGeometry();
    scroll->viewport()->update();
    scroll->update();
    }
    

    Code for zoom function in testWidget

    void testWidgets::zoom(double scale){
    double tmp = scale/1;
    double reset = 1/previousScale;
    if(scale == previousScale){
        return;
    }
    
    view->scale(reset,reset);
    view->resize(view->sizeHint()*reset);
    
    view->scale(tmp,tmp);
    previousScale = scale;
    
    view->resize(view->sizeHint()*tmp);
    view->updateGeometry();
    view->update();
    emit zoomChanged();
    }