Nominate our 2022 Qt Champions!

QGraphicsScene and mouseEvent

  • Good afternoon. There is a tied QGraphicsScene to QGraphicsView. I overloaded the mouse event, for drawing on the stage. But I do not understand how to return a pointer to the selected drawn object. By default, double-clicking the mouse highlights the object, I set the flag: line-> setFlag (QGraphicsItem :: ItemIsSelectable).
    Object: QGraphicsLineItem * line = new QGraphicsLineItem .
    Help to return the pointer to the selected object, to change it and view the coordinates of the line (beginning and end).

  • Lifetime Qt Champion


    Did you already took a look at the Graphics View Examples ? You might find there several source of inspiration for your application.

  • @lenina15i25
    Have you looked at
    I also tried to get the pointer of drawn object on scene by passing pointer. Strange, but I could not get it.
    In the example,

    bool DiagramScene::isItemChange(int type)
        foreach (QGraphicsItem *item, selectedItems()) {
            if (item->type() == type)
                return true;
        return false;

    It might be the function you need.
    Then you try something like

    void DiagramScene::setLineColor(const QColor &color)
        myLineColor = color;
        if (isItemChange(Arrow::Type)) {
            Arrow *item = qgraphicsitem_cast<Arrow *>(selectedItems().first());

    to get the pointer of selected item and do something on it.

Log in to reply