views:

51

answers:

1

Hi I've got a spare moment so decided to look at QT and how easily I can port my windows applications to Qt.

My only real problem is a couple of controls that will need re-implementing under QT. I've already handled the basic drawing of the control but my control creates a child scroll bar. The problem is that this scrollbar is created dynamically as part of my new Widget (ie m_Scrollbar is a member of the widget). How can I then respond to movement of the scrollbar. Under other circumstances this is easy as I'd just create an "on_myscrollbar_sliderMoved" function under my "protected slots" and handle it there. This does however rely on the QScrollBar being called "myscrollbar". As I've created the object dynamically (ie not through designer) how do I capture this signal?

I'm guessing this is really simple and I'm missing the obvious :)

+4  A: 
connect( myScrollbar, SIGNAL( <signal signature>), this, SLOT( <slot signature>));

Call connect after creating the scroll bar (I presume that you need this signal handling immediately after creating the scroll bar).

I assumed myScrollbar is of type QScrollBar* and that the slot is defined as a member in your class.

When myScrollbar is destroyed, the connection is removed (disconnect is called).

See the documentation of QObject::connect and QObject::disconnect methods.

Later edit - to be more concrete, in your code it could be:

myScrollbar = new QScrollBar; // Create the scroll bar
// ... add it to the layout, etc.
// ... and connect the signal to your slot
connect( myScrollbar, SIGNAL( sliderMoved( int)), this, SLOT( handleSliderMoved( int)));

where handleSliderMoved is the slot method of your class.

Cătălin Pitiș
Sweet ... I knew it would be something simple! Cheers :)
Goz