PDA

View Full Version : Drawing user defined images and displaying on buttons



sudhir
1st February 2010, 11:19
Hi ,
Iam new tothe QT programming.Please suggest me with code snippet how to draw images of our choice.iam totally new to the QT.Iam using QT 4.5.please find the image as attachment.

My task is,

I need to draw the attached image and display it on the button and once i click the button that image should display on the screen.I have gone through diagramscene example,it is of no use for me as it contains only polygons,i want to draw image of my choice.Please suggest me with the code snippet.

Thanks in advance.

Sudhir

high_flyer
1st February 2010, 12:26
I need to draw the attached image and display it on the button
You might find the QAbstractButton helpful.
Look at the setIcon() method.


and once i click the button that image should display on the screen.
QLabel comes to mind.
Have a look at QLabel::setPixmap() .

sudhir
2nd February 2010, 06:44
high_flyer thanks for the reply , ur suggestions helped me upto some extent.

can u suggest me in solving my main problem................... Thanks in advance


This is my Code,

#include <QtGui>
#include "diagramitem.h"
#include "arrow.h"

DiagramItem::DiagramItem(DiagramType diagramType, QMenu *contextMenu,
QGraphicsItem *parent, QGraphicsScene *scene)
: QGraphicsPolygonItem(parent, scene)
{
myDiagramType = diagramType;
myContextMenu = contextMenu;

QPainterPath path;
switch (myDiagramType) {
case StartEnd:
path.moveTo(200, 50); path.arcTo(150, 0, 50, 50, 0, 90);
path.arcTo(50, 0, 50, 50, 90, 90); path.arcTo(50, 50, 50, 50, 180, 90);
path.arcTo(150, 50, 50, 50, 270, 90); path.lineTo(200, 25);
myPolygon = path.toFillPolygon();
break;
case Conditional:
// path.moveTo(-60.0, -10.0);
path.lineTo(-40.0, 0.0);
path.moveTo(0,0);
path.lineTo(0,-40);
path.moveTo(0,0);
path.lineTo(0,40);

myPolygon = path.toFillPolygon();
//myPolygon << QPointF(0, 63) << QPointF(0, -63);
break;
case Step:
myPolygon << QPointF(-100, 100)<< QPointF(-100, -100);
break;
default:
// input/output
myPolygon << QPointF(-63, 0) << QPointF(63, 0);
break;
}
setPolygon(myPolygon);
setFlag(QGraphicsItem::ItemIsMovable, true);
setFlag(QGraphicsItem::ItemIsSelectable, true);
}


QPixmap DiagramItem::image() const
{
QPixmap pixmap(250, 250);
pixmap.fill(Qt::transparent);
QPainter painter(&pixmap);
painter.setPen(QPen(Qt::black, 12));
painter.translate(125, 125);
painter.drawPolyline(myPolygon);

return pixmap;
}

The problem is , when iam going with path.toFillPolygon(); the start and end points are getting joined.i want to draw the attached image .

high_flyer
2nd February 2010, 12:48
just do setIcon(), the button will do the drawing for you.