Good day everyone
I am struggling to implement a mouseDoubleClickEvent on a QGraphicsRectItems initialized in another class. I have a class called GatewayWidget where i have created the QgraphicsRectItems in a scene using myshape class, but i cannot get the mousedDoubleClickEvent to click on a QgraphicsRectItem on a scene. When i insert a breakpoint at(scene->items()) no items are returned, but the same mouseDoubleClickEvent works when I call a widget(named logWidg) on a doubleclicking of the same QGraphicsRectItem. However when I call on the QGraphicsRectItem to change colour on doubleclick it does not work. please help! I appreciate it advance! please see my code below
void GatewayWidget
::receivedTitlesEx(const QString &_rStrDescription,
const CORE
::Time &_rSimTime,
const QList<std
::shared_ptr<DM
::Object> >
&_rTitles,
int _eType
) { // I add my QGraphicsRectItems on the scene here
myshape* shape1 = new myshape(text);
//shape1->setFlag(QGraphicsItem::ItemIsMovable);
shape1->setPos(0, 25);
scene->addItem(shape1);
void GatewayWidget::receivedTitlesEx(const QString &_rStrDescription, const CORE::Time &_rSimTime, const QList<std::shared_ptr<DM::Object> > &_rTitles, int _eType)
{ // I add my QGraphicsRectItems on the scene here
myshape* shape1 = new myshape(text);
//shape1->setFlag(QGraphicsItem::ItemIsMovable);
shape1->setPos(0, 25);
scene->addItem(shape1);
shape1->setFlag(QGraphicsItem::ItemIsMovable);
shape1->setFlag(QGraphicsItem::ItemIsSelectable);
shape1->setFlag(QGraphicsItem::ItemIsFocusable);
To copy to clipboard, switch view to plain text mode
}
{
GatewayWidget gw; //this is the class that this method is implemented, and also where i have initialized the scene and drawn qgraphicsitems
gw.on_btnSelectItem_clicked(); //on_btnSelectItem_Clicked is the method implemented on Gatewaywidget that is supposed to change QGraphicsRectItem on double click, but it does not work. please help
//logWidg = new loginWidget();// but this widget can launch successfully when i click on the QGrapicsRectItem
//logWidg->show();
QList<QGraphicsItem*> stackOfShapes = gw.scene->items();// I tried to check if gw can return items but the breakpoint i inserted returns zero items
void myshape::mouseDoubleClick(QGraphicsSceneMouseEvent *event)
{
QGraphicsItem::mousePressEvent(event);
GatewayWidget gw; //this is the class that this method is implemented, and also where i have initialized the scene and drawn qgraphicsitems
gw.on_btnSelectItem_clicked(); //on_btnSelectItem_Clicked is the method implemented on Gatewaywidget that is supposed to change QGraphicsRectItem on double click, but it does not work. please help
//logWidg = new loginWidget();// but this widget can launch successfully when i click on the QGrapicsRectItem
//logWidg->show();
QList<QGraphicsItem*> stackOfShapes = gw.scene->items();// I tried to check if gw can return items but the breakpoint i inserted returns zero items
To copy to clipboard, switch view to plain text mode
myshape .h file is as follows:
public:
setRect(0, 0, 80, 80);
}
protected:
private:
GatewayWidget *gatew; //i have implemented the myshape::mousedoubleclick event on the class(GatewayWidget)
virtual QSizeF sizeHint
(Qt
::SizeHint which,
const QSizeF
& constraint
= QSizeF()) const { Q_UNUSED(which);
Q_UNUSED(constraint);
return boundingRect().size();
}
virtual void setGeometry(const QRectF& rect) {
setPos(rect.center());
}
};
class myshape :public QObject, public QGraphicsRectItem{
public:
myshape(QGraphicsItem *item) {
setPen(QPen(QBrush(Qt::black), 1));
setBrush(QBrush(Qt::green));
setRect(0, 0, 80, 80);
}
protected:
void mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event);
private:
GatewayWidget *gatew; //i have implemented the myshape::mousedoubleclick event on the class(GatewayWidget)
virtual QSizeF sizeHint(Qt::SizeHint which,
const QSizeF& constraint = QSizeF()) const {
Q_UNUSED(which);
Q_UNUSED(constraint);
return boundingRect().size();
}
virtual void setGeometry(const QRectF& rect) {
setPos(rect.center());
}
};
To copy to clipboard, switch view to plain text mode
on_btnSelectItem_clicked() is the method i call that is supposed to change colour of QGraphicsRectItem on mouseDoubleClickEvent but it does not work
void GatewayWidget::on_btnSelectItem_clicked()
{
{
if (!rect)
continue;
br.setColor(Qt::black);
rect->setBrush(br);
rect->update();
}
}
void GatewayWidget::on_btnSelectItem_clicked()
{
foreach(QGraphicsItem *item, scene->selectedItems())
{
QGraphicsRectItem *rect = qgraphicsitem_cast<QGraphicsRectItem *>(item);
if (!rect)
continue;
QBrush br(Qt::SolidPattern);
br.setColor(Qt::black);
rect->setBrush(br);
rect->update();
}
}
To copy to clipboard, switch view to plain text mode
Bookmarks