@Pl45m4 Got i solved by just handling the selection in the mousepress and release event. However now im stuck with painting handles to resize the groupitem. I don't really know how to do that as qgraphicsgrouptitem has no setRect method.
Here is my graphicsgroupitem;
#include "resizablegraphicsgroupitem.h"
#include "qpainter.h"
#include <QPen>
ResizableGraphicsGroupItem::ResizableGraphicsGroupItem()
{
// Initialisierungen hier, falls benötigt
setFlags(QGraphicsItem::ItemIsSelectable | QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemSendsScenePositionChanges | QGraphicsItem::ItemSendsGeometryChanges);
setOwnerItem(this);
}
QRectF ResizableGraphicsGroupItem::boundingRect() const
{
return childrenBoundingRect();
}
void ResizableGraphicsGroupItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
qDebug() << "Test";
Q_UNUSED(option);
Q_UNUSED(widget);
painter->save();
//Malt eine Linie um das Rechteck
painter->setPen(QPen(Qt::green));
painter->drawRect(boundingRect());
painter->restore();
}
I coded such a handle class before, but i don't know how to get it to work with a qgraphicsgroupitem:
#include "resizablehandlerect.h"
#include <QGraphicsScene>
#include <QDebug>
#include <QCursor>
ResizableHandleRect::ResizableHandleRect()
{
}
ResizableHandleRect::~ResizableHandleRect()
{
}
void ResizableHandleRect::drawHandles()
{
qDebug() << "ResizableHandleRect DrawHandles triggered!";
// Erstellen und Hinzufügen der Handles zur Szene
//Populate handles in list
if(handleList.count() == 0){
handleList.append(new HandleItem(HandleItem::TopLeft));
handleList.append(new HandleItem(HandleItem::TopCenter));
handleList.append(new HandleItem(HandleItem::TopRight));
handleList.append(new HandleItem(HandleItem::RightCenter));
handleList.append(new HandleItem(HandleItem::BottomRight));
handleList.append(new HandleItem(HandleItem::BottomCenter));
handleList.append(new HandleItem(HandleItem::BottomLeft));
handleList.append(new HandleItem(HandleItem::LeftCenter));
}
//Set up pen
QPen mPen;
mPen.setWidth(2);
mPen.setColor(Qt::black);
//Top left handle
QPointF topLeftCorner = selectorFrameBounds().topLeft() + QPointF(-8.0,-8.0);
topLeftHandleRect = QRectF(topLeftCorner,QSize(8,8));
handleList[0]->setRect(topLeftHandleRect);
if(!handleList.isEmpty() && (!handlesAddedToScene)){
handleList[0]->setPen(mPen);
//handleList[0]->setBrush(QBrush(Qt::black));
ownerItem->scene()->addItem(handleList[0]);
handleList[0]->setParentItem(ownerItem);
handleList[0]->setCursor(Qt::SizeFDiagCursor);
}
//Top Center
QPointF topCenterCorner = QPointF(selectorFrameBounds().left() + selectorFrameBounds().width() / 2.0 - 4.0, selectorFrameBounds().top()-4.0);
topCenterHandleRect = QRectF(topCenterCorner,QSize(8,8));
handleList[1]->setRect(topCenterHandleRect);
if(!handleList.isEmpty() && (!handlesAddedToScene)){
handleList[1]->setPen(mPen);
//handleList[1]->setBrush(QBrush(Qt::gray));
ownerItem->scene()->addItem(handleList[1]);
handleList[1]->setParentItem(ownerItem);
handleList[1]->setCursor(Qt::SizeVerCursor);
}
//Top right
QPointF topRightCorner = selectorFrameBounds().topRight() + QPointF(0,-8.0);
topRightHandleRect = QRectF(topRightCorner,QSize(8,8));
handleList[2]->setRect(topRightHandleRect);
if(!handleList.isEmpty() && (!handlesAddedToScene)){
handleList[2]->setPen(mPen);
//handleList[1]->setBrush(QBrush(Qt::gray));
ownerItem->scene()->addItem(handleList[2]);
handleList[2]->setParentItem(ownerItem);
handleList[2]->setCursor(Qt::SizeBDiagCursor);
}
//Right Center
QPointF rightCenterCorner = QPointF(selectorFrameBounds().right() - 4.0, selectorFrameBounds().top() + selectorFrameBounds().height() / 2.0 - 4.0);
rightCenterHandleRect = QRectF(rightCenterCorner, QSize(8, 8));
handleList[3]->setRect(rightCenterHandleRect);
if (!handleList.isEmpty() && (!handlesAddedToScene)) {
handleList[3]->setPen(mPen);
ownerItem->scene()->addItem(handleList[3]);
handleList[3]->setParentItem(ownerItem);
handleList[3]->setCursor(Qt::SizeHorCursor);
}
//Bottom right
QPointF bottomRightCorner = selectorFrameBounds().bottomRight() + QPointF(0,0);
bottomRightHandleRect = QRectF(bottomRightCorner,QSize(8,8));
handleList[4]->setRect(bottomRightHandleRect);
if(!handleList.isEmpty() && (!handlesAddedToScene)){
handleList[4]->setPen(mPen);
//handleList[2]->setBrush(QBrush(Qt::gray));
ownerItem->scene()->addItem(handleList[4]);
handleList[4]->setParentItem(ownerItem);
handleList[4]->setCursor(Qt::SizeFDiagCursor);
}
// Bottom Center
QPointF bottomCenterCorner = QPointF(selectorFrameBounds().left() + selectorFrameBounds().width() / 2.0 - 4.0, selectorFrameBounds().bottom()-4.0);
bottomCenterHandleRect = QRectF(bottomCenterCorner, QSize(8, 8));
handleList[5]->setRect(bottomCenterHandleRect);
if (!handleList.isEmpty() && (!handlesAddedToScene)) {
handleList[5]->setPen(mPen);
ownerItem->scene()->addItem(handleList[5]);
handleList[5]->setParentItem(ownerItem);
handleList[5]->setCursor(Qt::SizeVerCursor);
}
//Bottom left
QPointF bottomLeftCorner = selectorFrameBounds().bottomLeft() + QPointF(-8,0);
bottomLeftHandleRect = QRectF(bottomLeftCorner,QSize(8,8));
handleList[6]->setRect(bottomLeftHandleRect);
if(!handleList.isEmpty() && (!handlesAddedToScene)){
handleList[6]->setPen(mPen);
//handleList[3]->setBrush(QBrush(Qt::gray));
ownerItem->scene()->addItem(handleList[6]);
handleList[6]->setParentItem(ownerItem);
handleList[6]->setCursor(Qt::SizeBDiagCursor);
}
// Left Center
QPointF leftCenterCorner = QPointF(selectorFrameBounds().left() - 4.0, selectorFrameBounds().top() + selectorFrameBounds().height() / 2.0 - 4.0);
leftCenterHandleRect = QRectF(leftCenterCorner, QSize(8, 8));
handleList[7]->setRect(leftCenterHandleRect);
if (!handleList.isEmpty() && (!handlesAddedToScene)) {
handleList[7]->setPen(mPen);
ownerItem->scene()->addItem(handleList[7]);
handleList[7]->setParentItem(ownerItem);
handleList[7]->setCursor(Qt::SizeHorCursor);
}
handlesAddedToScene = true;
}
void ResizableHandleRect::setOwnerItem(QGraphicsItem *value)
{
ownerItem = value;
}
void ResizableHandleRect::drawHandlesIfNecessary(bool ShouldDrawHandles)
{
qDebug() << "DrawHandles" << ShouldDrawHandles << ownerItem->isSelected();
if(!ownerItem){
qWarning() << "ResizableHandleRect : No ownerItem set. Not drawing any\
handle. Please call setOwnerItem on your ResizableHandleRect subclass";
return;
}
if(ownerItem->isSelected() && ShouldDrawHandles == true){
drawHandles();
handlesAddedToScene = true;
}else if(!ownerItem->isSelected() | (ShouldDrawHandles == false)){
//Remove the handles
foreach (HandleItem * handleItem, handleList) {
ownerItem->scene()->removeItem(handleItem);
}
qDeleteAll(handleList);
handleList.clear();
handlesAddedToScene = false;
}
}