[SOLVED] Drawing a text or image in QGraphicsRect(Ellipse)Item



  • Hello,
    I am trying to apply a drawing to my custom inherited item from QGraphicsEllipseItem. This is my header file of the custom ellipse:
    @
    class MainWindow;

    class ActionEllipseItem:QGraphicsEllipseItem {
    friend class MainWindow;
    private:
    int xcoord;
    int ycoord;
    int __privateId;
    QString __name;
    ActionEllipseItem(int x, int y, int w, int h, QString name, int id, MainWindow* pw);
    ActionEllipseItem();
    MainWindow* parent;

    protected:
    void mousePressEvent(QGraphicsSceneMouseEvent* event);
    public:
    QString toString();
    ActionEllipseItem* getSelf();
    };
    @
    The implementation is as follows:
    @
    ActionEllipseItem::ActionEllipseItem() {

    }
    ActionEllipseItem* ActionEllipseItem::getSelf() {
    return this;
    }
    ActionEllipseItem::ActionEllipseItem(int x, int y, int w, int h, QString name, int id, MainWindow* pw) {
    xcoord = x; ycoord =y;
    this->parent = pw;
    this->__name = name;
    this->__privateId = id;
    setRect(x,y,w,h);
    this->setPen(QPen(QColor(0,0,0,255))); // show action items

    }
    QString ActionEllipseItem::toString() {
    static QString s;
    s.append(this->__name);
    s.append(QString::number(this->__privateId));
    return s;
    }

    void ActionEllipseItem::mousePressEvent(QGraphicsSceneMouseEvent event) {
    qDebug() << "Custom ActionItem clicked\n";
    qDebug() << "Item`s toString():" << this->toString() << "\n";
    this->setBrush(QBrush(QColor(255,0,0,100)));
    // ((QMediaPlayer
    )parent->getPlayer())->play();
    QPainter painter;
    painter.setPen(QColor(100,100,100,150));
    painter.setBrush(QColor(240,0,0,0));
    painter.save();
    QFont font = painter.font();
    font.setPointSize(16);
    painter.setFont(font);
    painter.drawText(QPoint(xcoord2, ycoord2), "A");
    /* paint() ??? */
    }
    @

    OK, so I must call the virtual paint() but, must I override it? And how exactly to do that? I saw some examples, but they were not working. Exactly what should I pass to paint() and how to override it?
    [EDIT] To provide more infro here is what I am trying to do:
    https://www.flickr.com/photos/heatblazer/15876900895/
    Instead the plain paint of the color, a number is supposed to appear or a new image to be displayed on the pink ellipses.
    [EDIT] I was able to find a way to do it. Here is the result. However I must see if it`s appliable to images and other items.
    https://www.flickr.com/photos/heatblazer/15695544130/


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.