How to handle signals when a Qt object isn't created through Designer?
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 (i.e. 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.开发者_如何学C This does however rely on the QScrollBar
being called myscrollbar
. As I've created the object dynamically (i.e. not through designer) how do I capture this signal?
I'm guessing this is really simple and I'm missing the obvious :)
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.
精彩评论