PDA

View Full Version : Can not cast into the derived class from QScrollBar



learning_qt
19th December 2008, 07:16
I defined a class ViewScrollBar derived from QScrollBar. There is also a public slot: void currentValue(i nt value); I hope to use this slot in another class.

#include <QScrollBar>
class ViewScrollBar: public QScrollBar
{
Q_OBJECT
public:
ViewScrollBar();
~ViewScrollBar();
int getCurrentValue();

public slots:
void currentValue(i nt value);

private:
int currentPos;
};

Then I get the scroll bar from the QGraphicsView by the method:
ViewScrollBar *hScrollBar=static_cast<ViewScrollBar*>(graphicsView->horizontalScrollBar());

I connect the signal valueChanged() from QScrollBar with my public slot currentValue(i nt value) in ViewScrollBar class.
connect(hScrollBar, SIGNAL(valueChanged(int)),hScrollBar, SLOT(currentValue(int)));

when I run, there is always warning that:
Object::connect: No such slot QScrollBar::currentValue(int).
The object hScrollBar is ViewScrollBar now. But it is still regarded as its parent class. How can cast it from QScrollBar to ViewScrollBar?

wysota
19th December 2008, 08:22
Do you have the Q_OBJECT macro and is the above code a spelling error with the space between "i" and "nt"? If you added Q_OBJECT later, did you remember to run qmake afterwards?

learning_qt
19th December 2008, 10:23
Yes, I have Q_OBJECT used in header file.

Sorry, actually, there is on misspelling in my code. Maybe when I wrote this post, I made a mistake.
void currentValue(int value)