PDA

View Full Version : QScrollBar subclass constructor ignores orientation Qt4.4



DuncanG
24th July 2008, 18:24
I'm new to Qt so its probably me, but..

I've created a trivial subclass of QScrollbar -


class ScrollBar : public QScrollBar
{
Q_OBJECT
public:
ScrollBar ( Qt::Orientation orientation, QWidget * parent = 0 );
protected:
void paintEvent(QPaintEvent* event);
};

ScrollBar::ScrollBar ( Qt::Orientation orientation, QWidget * parent )
{
QScrollBar::QScrollBar (orientation, parent);
}

void ScrollBar:: paintEvent(QPaintEvent * event)
{
int x, y, w, h;
this->rect().getRect(&x,&y,&w,&h);
QScrollBar:: paintEvent(event);
QPainter p(this);
p.setPen(QPen(Qt::red,1));
p.drawLine(x,y,x+w,y+h);
}

when created by -

scrollBar = new ScrollBar(orientation); // works for QScrollBar
a vertical scrollbar is drawn regardless of orientation.
Following by -

scrollBar->setOrientation(orientation); // not required for QScrollbar
provides a fix, but it wasn't needed when using the ancestor class.

Where am I going wrong?

jpn
24th July 2008, 18:55
It should be:


ScrollBar::ScrollBar ( Qt::Orientation orientation, QWidget * parent )
: QScrollBar::QScrollBar (orientation, parent)
{
}

DuncanG
24th July 2008, 20:34
Thanks JP, my C++ is rusty :o