PDA

View Full Version : Moving scroll bar inside QScroll Area



gmahan
8th October 2014, 20:43
Hi,

I need to use a custom scroll bar for the QScrollArea widget which I am setting using the setVerticalScrollBar() member function. However, my custom scroll bar is showing up complete, it's being cut. The custom scroll bar has a width of 48px.

MyScrollBar::MyScrollBar(QWidget *parent) :
QWidget(parent),
ui(new Ui::MyScrollBar)
{
scrollWidget = new ScrollWidget(this);
scrollBar = new ScrollBar(Qt::Vertical, this);

ui->setupUi(this);
scrollBar->setMaximumWidth(46);
scrollBar->setMinimumWidth(46);
ui->scrollArea->setVerticalScrollBar(scrollBar);

//ui->scrollArea->widget()->show();
}

See image below..

10658

10659

wysota
8th October 2014, 22:51
I think that if the bar is wider than the standard bar, you will need to adjust the scroll area viewport's contents margins accordingly and maybe also position the new bar yourself inside a reimplemented resizeEvent.

gmahan
8th October 2014, 22:55
Thanks for the response. Could you elaborate on this : scroll area viewport's contents margins

Is the viewport a subcontrol of the scrollArea widget. I am unable to get any information on the subcontrols that belong to QScrollArea.

wysota
8th October 2014, 22:58
Look at QAbstractScrollArea api.

gmahan
9th October 2014, 15:57
I tried setting the margins using the setViewportMargins(), but it didn't work.

ScrollArea::ScrollArea(QWidget *parent, QScrollBar *scrollbar) :
QScrollArea(parent)
{
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOf f);
setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
setVerticalScrollBar(scrollbar);
setViewportMargins(0, 0, 50, 0);
}

I am not familiar with the resize event so I have not tried it yet. I just wanted to make sure if I was on the correct path.

10664

gmahan
9th October 2014, 19:34
Tried to add the setViewportMargins in the resize event function, but that didn't work either. Any other hints? I've looked everywhere but haven't found anything. Looks like I might have to create a custom scroll bar and link the signals to the scrollarea widget.

wysota
9th October 2014, 22:47
How does "doesn't work" manifest itself?

gmahan
10th October 2014, 14:20
The scroll bar is still clipped off. I am unable to move it's position within QScrollArea Widget.

wysota
10th October 2014, 14:59
The scroll bar is still clipped off. I am unable to move it's position within QScrollArea Widget.

Can we see the code you have written to achieve this?

gmahan
10th October 2014, 19:13
#include "scrollarea.h"

ScrollArea::ScrollArea(QWidget *parent, QScrollBar *scrollbar) :
QScrollArea(parent)
{
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOf f);
setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
setVerticalScrollBar(scrollbar);
setViewportMargins(50, 50, 50, 50);
}

ScrollArea::~ScrollArea()
{

}

void ScrollArea::resizeEvent(QResizeEvent *event)
{
setViewportMargins(0, 0, 50, 0);

QScrollArea::resizeEvent(event);
}

#include "scrollbar.h"

ScrollBar::ScrollBar(Qt::Orientation orientation, QWidget *parent):
QScrollBar(orientation, parent)
{
setMaximumWidth(48);
setMinimumWidth(48);

/* Center Body */
this->setStyleSheet(QString("QScrollBar:vertical {"
"background: #333333;"
"margin: 46px 0 46px 0;"
"}"
/*Slider*/
"QScrollBar::handle:vertical {"
"border: none;"
"background: #4b4b4b;"
"}"

/* Down button */
"QScrollBar::add-line:vertical {"
"background: none;"
"height: 48px;"
"subcontrol-position: bottom;"
"subcontrol-origin: margin;"
"}"
/*Up-button*/
"QScrollBar::sub-line:vertical {"
"background: none;"
"height: 48px;"
"subcontrol-position: top;"
"subcontrol-origin: margin;"
"}"

/* add-page, sub-page subcontrol*/
"QScrollBar::add-page:vertical, QScrollBar::sub-page:vertical {"
"background: none;"
"}"

/*up arrow normal state*/
"QScrollBar::up-arrow:vertical {"
"image: url(:/Images/scroll-uparrow);"
"width: 48px;"
"height: 46px;"
"}"

/* down arrow normal state*/
"QScrollBar::down-arrow:vertical {"
"image: url(:/Images/scroll-downarrow);"
"width: 48px;"
"height: 46px;"
"}"

/* up arrow pressed*/
"QScrollBar::up-arrow:vertical:pressed {"
"image: url(:/Images/scroll-uparrow-hit);"
"width: 48px;"
"height: 46px;"
"}"

/* down arrow pressed*/
"QScrollBar::down-arrow:vertical:pressed {"
"image: url(:/Images/scroll-downarrow-hit);"
"width: 48px;"
"height: 46px;"
"}"));


}

ScrollBar::~ScrollBar()
{

}

#include "scrollbar.h"
#include "myscrollbar.h"
#include "scrollwidget.h"
#include "scrollarea.h"

#include <QApplication>
#include <QVBoxLayout>
#include <QSize>

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QWidget *mainWindow = new QWidget;
ScrollBar *scrollBar = new ScrollBar(Qt::Vertical, mainWindow);
ScrollArea *scrollArea = new ScrollArea(mainWindow, scrollBar);

QVBoxLayout *layout = new QVBoxLayout;

mainWindow->setWindowTitle("Scroll");
layout->addWidget(scrollArea);
mainWindow->setLayout(layout);
mainWindow->show();

return a.exec();
}





Sorry, for the annoying text format. What is the tag to insert code.

wysota
11th October 2014, 09:38
Ok, but in the resize event you were supposed to reposition the scrollbar to where you want it to be. Calling the base class implementation will position the bar exactly the same way as originally.

gmahan
14th October 2014, 13:52
Hmm, I guess that is the part I am tripping. What function call do you use to reposition the bar?

wysota
14th October 2014, 14:25
Setter of QWidget::geometry property.