Important: Please read the Qt Code of Conduct -

[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;
    int xcoord;
    int ycoord;
    int __privateId;
    QString __name;
    ActionEllipseItem(int x, int y, int w, int h, QString name, int id, MainWindow* pw);
    MainWindow* parent;

    void mousePressEvent(QGraphicsSceneMouseEvent* event);
    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;
    this->setPen(QPen(QColor(0,0,0,255))); // show action items

    QString ActionEllipseItem::toString() {
    static QString s;
    return s;

    void ActionEllipseItem::mousePressEvent(QGraphicsSceneMouseEvent event) {
    qDebug() << "Custom ActionItem clicked\n";
    qDebug() << "Item`s toString():" << this->toString() << "\n";
    // ((QMediaPlayer
    QPainter painter;
    QFont font = painter.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:
    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.

Log in to reply