PDA

View Full Version : View with flow chart?



steg90
2nd October 2007, 08:15
Hi,

I want a view which has box's contained in it, a little like a flow chart, these box's can be clicked on and a corresponding dialog box be displayed showing the attributes of the box. What is a good way of doing this? Should I just use QPainter to draw rectangles for the box's and if so, how do I capture events on the box's?

Any help, advice is much appreciated.

Steve

marcel
2nd October 2007, 08:17
You can do it with a QListView and a custom delegate.
The delegate should paint the items as boxes.

Painting the boxes yourself on a widget involves keeping track of user interactions on the boxes. For the list view, the support is already there.

steg90
2nd October 2007, 08:22
Thanks Marcel,

Would I be able to place the box's side by side as well as above and below each other using what you suggested?

I don't know how the delegate would be able to paint box's anywhere within the list view?

Regards,
Steve

marcel
2nd October 2007, 08:25
It can do that, but the list view takes care of the items layout, not the delegate.
At most it can place them in something similar to a grid layout. It cannot give them random positions.

If you want random positions and also move the boxes with the mouse, then consider using a graphics view/scene.

steg90
2nd October 2007, 08:30
Thanks,

I will need random positions and possibly the option of moving them about. I did think maybe a good way would be to subclass QPushButton and override the repaint method, this way, you still have the functionality of the signals ( clicked( ) ) and can draw the button how you like. I don't know if this is possible, this is how I would have done it using MFC.

Regards,
Steve

marcel
2nd October 2007, 08:49
In this case using QGraphicsView is the solution, if you want to keep things simpler.
You can customize painting and the framework has builtin item interaction.

steg90
2nd October 2007, 09:05
Thanks again,

Just looking at QGraphicsView and QGraphicsScene now. I take it my items would be of QGraphicsItem and I override the paint event to draw my item?



class CBoxItem : public QGraphicsItem
{
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
// do whatever
painter->drawRoundRect( 10, 10, 20, 20 );
}
};



May even derive from QGraphicsPolygonItem...

How do I know when a user clicks on one of the GraphicItems, is this part of the QGraphicsScene - i.e. mousePressEvent?

Regards,
Steve

marcel
2nd October 2007, 09:14
You have to override the item's mousePressEvent.
Take a look at the protected functions in the QGraphicsItem class.

steg90
2nd October 2007, 09:26
Many thanks for your help Marcel,

Going to have a derived class from QGraphicsItem, override paint and boundingRect and the mousePressEvent. I did notice that it would also be possible to create a slot in the main window which could be emited from the scene when an item is selected ( capture the mouseEvent within the scene ) - although I think just overriding the mousePressEvent within the subclassed QGraphicsItem class seems simpler.

Regards,
Steve