Restrict horizontal scrolling in QScrollArea



  • I have a widget that contains a QScrollArea. I have set a custom made widget ItemList that is basically a list of items in a QVBoxLayout
    in mainwidget.cpp
    @
    MainWidget::MainWidget(QWidget *parent) :
    QWidget(parent)
    {
    engine = new Engine(QString("/home/user/MyDocs/"));
    engine->loadIndex();
    results = new QList<IndexItem>();
    w= new SearchWidget();
    w2 = new ItemList();
    area=new QScrollArea();
    area->setWidget(w2);
    layout = new QVBoxLayout(this);
    layout->addWidget(w);
    layout->addWidget(area);
    connect(w,SIGNAL(searchPressed()),this,SLOT(performSearch()));
    connect(w,SIGNAL(filter()),this,SLOT(displayFilterDialog()));
    }
    @

    in itemlist.cpp
    @
    ItemList::ItemList(QWidget *parent) :
    QWidget(parent)
    {

    lay = new QVBoxLayout(this);
    
    //performSearch(QString());
    

    }
    ItemList::ItemList(QList<ItemButton *> l, QWidget *parent):
    QWidget(parent)
    {
    lay=new QVBoxLayout(this);
    addButtons(l);
    }

    void ItemList::addButtons(QList<ItemButton *> l) {
    ItemButton *i;
    foreach(i,l) {
    lay->addWidget(i);
    }
    }
    @

    On deploying on an N900 running Maemo5-fremantle everything is fine except that the scroll area scrolls in the horizontal direction also when i flick it.. I do not want it to flick in the horizontal direction.. Can anyone tell me how this can be done.. I dont see any attribute of QScrollArea that disables horizontal scrolling...
    I have also defined sizeHint() for each of the ItemButton as follows:
    @
    QSize ItemButton::sizeHint() const {
    return(QSize(780,100));
    }
    @
    which I believe is well within the horizontal limits of the screen
    PLEASE HELP!



  • The property you are looking for is in "QAbstractScrollArea":http://developer.qt.nokia.com/doc/qt-4.8/qabstractscrollarea.html#horizontalScrollBarPolicy-prop, the parent class of QScrollArea. Always look at the inherited members as well.

    So, the property is called horizontalScrollBarPolicy and can be set by the function:
    @
    area=new QScrollArea();
    area->setHorizontalScrollBarPolicy ( Qt::ScrollBarAlwaysOff );
    @



  • [quote author="favoritas37" date="1326040653"]The property you are looking for is in "QAbstractScrollArea":http://developer.qt.nokia.com/doc/qt-4.8/qabstractscrollarea.html#horizontalScrollBarPolicy-prop, the parent class of QScrollArea. Always look at the inherited members as well.

    So, the property is called horizontalScrollBarPolicy and can be set by the function:
    @
    area=new QScrollArea();
    area->setHorizontalScrollBarPolicy ( Qt::ScrollBarAlwaysOff );
    @
    [/quote]

    I know about horizontalScrollBarPolicy and all it does is make the scroll bar disappear
    What I need is that horizontal scrolling * should not take place * even if I flick the screen horizontally....



  • In the constructor of both MainWidget and ItemList, as it seems from your code, you haven't assigned the QVBoxLayout to the parent widget. After creating and initializing a layout you have to add it to the widget you want it to exist through setLayout(layout) function. So you code would be like this:

    MainWidget.cpp
    @
    MainWidget::MainWidget(QWidget *parent) :
    QWidget(parent)
    {
    engine = new Engine(QString("/home/user/MyDocs/"));
    engine->loadIndex();
    results = new QList<IndexItem>();
    w= new SearchWidget();
    w2 = new ItemList();
    area=new QScrollArea();
    area->setWidget(w2);
    layout = new QVBoxLayout(this);
    layout->addWidget(w);
    layout->addWidget(area);
    setLayout(layout);
    connect(w,SIGNAL(searchPressed()),this,SLOT(performSearch()));
    connect(w,SIGNAL(filter()),this,SLOT(displayFilterDialog()));
    }
    @

    and itemlist.cpp
    @
    ItemList::ItemList(QWidget *parent) :
    QWidget(parent)
    {

    lay = new QVBoxLayout(this);
    setLayout(lay);
    //performSearch(QString());
    

    }
    @



  • That doesnt work either
    Either we pass the widget to the layout's constructor OR we use setLayout() to parent the layout.
    It says so in the documentation of setLayout()
    If it were not so I would not have been getting the list of ItemButtons arranged vertically when I call ItemList::addButtons() which I sure am
    and...
    I am still able to flick horizontally... :(



  • The same also happens when I use a @QListWidget@ and add items to it...
    When I flick horizontally it moves and then springs back to its initial position...
    ;(



  • area=new QScrollArea();
    area->setHorizontalScrollBarPolicy ( Qt::ScrollBarAlwaysOff );
    

    This worked for me - Thanks a lot



  • [quote author="Bhupal" date="1339560007"] area=new QScrollArea();
    area->setHorizontalScrollBarPolicy ( Qt::ScrollBarAlwaysOff );

    This worked for me - Thanks a lot[/quote]

    Tried that already... It only hides the scrollbar, doesnt prevent the scrolling ..


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.