PDA

View Full Version : custom widget that contains moving parts



cgyan1
12th February 2012, 13:06
Hi,

I was wondering what the best approach for creating a custom widget that contains moving parts is. Say I was going to create something from scratch like a slider widget similar to QSlider, for example, then the handle of the slider obviously must be able to be dragged. If this widget was designed in QtDesigner then, unless I'm mistaken, in order for the handle to be promoted to be able to receive direct mouse events in a custom class, the handle itself must be in one of the structured layouts which defeats its purpose of it being movable. I experimented by placing some widgets inside a parent widget without any structured layouts applied and promoted a couple of the child widgets but they do not show when the code is run.

So what would be the best approach/structure of designing something along these lines?

Lykurg
12th February 2012, 22:54
If we stick to the slider example, I would subclass a plain widget and add a custom paint event. Inside the paint event draw all yourself. Reimp mouse***event and do the calculation if the slider (= the position) was pressed or not etc. Then call update(QRect) to force a paint update.

So no moving widget at all. Catch the mouse events, calculate what should change (position of the slider), store the results in member variable, call paint, and paint the appearance according to the member variables.

cgyan1
14th February 2012, 15:47
Yes, that's a nice idea and will probably work very well for what I have in mind and will also work well for other ideas I have.
Thanks very much!