PDA

View Full Version : Need Some help with QPainter.



ayanda83
14th April 2015, 11:39
Hi there guys, this is what I have done with QPainter 11086 and this is what I am aiming for 11087.
Here is my code below.
QPainterPath path;
path.moveTo(150, 200);
path.quadTo(150, 150, 200, 150);
path.lineTo(402, 150);

path.lineTo(402, 400);
path.quadTo(402, 450, 350, 450);
path.lineTo(150, 450);

path.closeSubpath();

painter->setPen(pen);
painter->drawImage(boundingRect(), image);
painter->drawPath(path);
My problem here is that I don't know how to remove the image corners popping out at the top-left hand corner and the bottom-right hand corner. All I did was to draw the paintPath over the edges of the image. I am not ever sure if that should have been the approach.

Any help will be greatly appreciated.

thank you.

wysota
14th April 2015, 11:43
Set clipping on the painter before you draw the image or use a composition mode that will draw on an area specified by a given mask.

ayanda83
14th April 2015, 17:06
Thank you, I can't believe how easy that was. I have small problem with this GraphicsItem. I have set the flag to make the item movable, but the item wont move when dragged. Could it be because the item is a child item to another item? Here is the code below.
Picture::Picture(QRectF itemRect)
{
rect = itemRect;

setFlag(QGraphicsItem::ItemIsMovable);
setFlag(QGraphicsItem::ItemIsSelectable);
setFlag(QGraphicsItem::ItemIsFocusable);

pixmap = new QPixmap();
}
and the code below is where i initialize the object in the main function.
photo = new Picture(QRectF(150,150,252,300));
photo->setPicDir(":/TestPics/Test_images/myPic.jpg");
photo->setParentItem(page_1);
photo->setPainterID(1);
scene->addItem(photo);
Am I doing something wrong here?

wysota
14th April 2015, 17:34
Did you reimplement any mouse events (e.g. in the view)?

ayanda83
14th April 2015, 19:17
Yes I did, but in the scene class and the mouseEvent function I implemented has no relationship with the graphicsItem I am trying move around the scene. The mouse event function that I implemented is specifically for handling where to insert a QGraphicstextItem in the scene. here is the code below
void Scene::mousePressEvent(QGraphicsSceneMouseEvent *mouseEvent)
{
if (mouseEvent->button() != Qt::LeftButton)
return;

textItem = new TextItem();

textItem->setFont(myFont);
textItem->setTextInteractionFlags(Qt::TextEditorInteraction) ;
textItem->setZValue(1000.0);
connect(textItem, SIGNAL(lostFocus(TextItem*)),
this, SLOT(editorLostFocus(TextItem*)));
connect(textItem, SIGNAL(selectedChange(QGraphicsItem*)),
this, SIGNAL(itemSelected(QGraphicsItem*)));
addItem(textItem);
textItem->setDefaultTextColor(myTextColor);
textItem->setPos(mouseEvent->scenePos());
emit textInserted(textItem);

QGraphicsScene::mousePressEvent(mouseEvent);
}

wysota
14th April 2015, 22:43
Yes I did, but in the scene class and the mouseEvent function I implemented has no relationship with the graphicsItem I am trying move around the scene.
But it breaks the built-in move functionality. Make sure the default implementation of all mouse events is always executed if you want items to be movable.

ayanda83
15th April 2015, 09:22
But it breaks the built-in move functionality. Make sure the default implementation of all mouse events is always executed if you want items to be movable.

Thank you for your reply but I am not sure about how to go about ensuring that the default implementation of all mouse events is always executed. Could you further elaborate on this if you don't mind please. Does it mean I must not implement mouse event functions or what?

wysota
15th April 2015, 09:26
Could you further elaborate on this
This is not C++ kindergarden.

d_stranz
15th April 2015, 16:51
This is not C++ kindergarden.

To elaborate, if you derive from a class, how do you ensure that when you override a virtual method for that class that the base class (default) method also gets executed? It's taught in C++ preschool...

ayanda83
15th April 2015, 21:07
This is not C++ kindergarden.

I am familiar with Polymorphism; I was just a little confused by your reply. I didn't know that you were talking about Polymorphism but thanks to "d_stranz" for explaining a little, now I understand what you meant. And thanks to you wysota, you are always helpful and sorry for the misunderstanding.