Important: Please read the Qt Code of Conduct -

QGraphicsView/Item/Rects with events question

  • Hello. I am following some examples about handling events with the graphics area. I want to separate a grahics area, containing a picture to areas like a NxM grid. So I`ve came up with this prototype idea:

    The custom action item`s .h and .cpp files:
    #ifndef ACTIONITEM_H
    #define ACTIONITEM_H
    #include <QGraphicsRectItem>
    #include <QGraphicsSceneMouseEvent>
    #include <QDebug>
    #include <QtCore>

    class ActionItem:QGraphicsRectItem {

    int __privateId;
    QString __name;
    void mousePressEvent(QGraphicsSceneMouseEvent* event);
    ActionItem(int x, int y, int w, int h);
    #endif // ACTIONITEM_H

    #include "ActionItem.h"
    #include <QDebug>

    ActionItem::ActionItem() {
    setRect(0,0, 40,40 ); //some defaults for testing

    ActionItem::ActionItem(int x, int y, int w, int h) {

    setRect(x, y, w, h);


    ActionItem::mousePressEvent(QGraphicsSceneMouseEvent *event) {
    qDebug() << "Custom ActionItem clicked\n";


    The main window for testing purposes ( later it will be embeded into another window as a frame )
    #include <QWidget>
    #include <QGraphicsScene>
    #include "ActionItem.h"

    class MainWindow : public QWidget {

    MainWindow(QApplication*); //passed from the main


    QWidget* __window;
    QApplication* __qapp;
    QGraphicsScene* __scene;
    QGraphicsView* __view;


    #endif // MAINWINDOW_H


    #include "MainWindow.h"
    #include <QEvent>
    #include <QList>
    #include <QApplication>
    #include <QPoint>
    #include <QMouseEvent>
    #include <QDebug>

    MainWindow::MainWindow() { }

    MainWindow::MainWindow(QApplication* qapp) {

    __qapp = qapp;
    __scene = new QGraphicsScene;
    __view = new QGraphicsView(__scene);
    QList<ActionItem> actionItems ;
    __scene->setSceneRect(QRectF(0,0, 320, 240));
    int i=0;
    int j=0;
    for (i=0; i < 320;  i+= 40, j) {
        actionItems.append(ActionItem(i, 0, 40,40));
     //   __scene->addItem(;


    int main(int argc, char* argv[]) {
    QApplication qapp(argc, argv);
    new MainWindow(&qapp);
    return qapp.exec();


    So, actually this code fails me in ActionItem inheritance line ( 4 errors ):
    class ActionItem : QGraphicsRectItem {

    MOC says:
    /opt/Qt5.3.1/5.3/gcc/include/QtCore/qglobal.h:1001: error: 'QGraphicsRectItem& QGraphicsRectItem::operator=(const QGraphicsRectItem&)' is private
    Class &operator=(const Class &) Q_DECL_EQ_DELETE;

    However, the examples I saw were completely valid. So can you enlighten me on the topic. It`s my first step in QGraphics/View/Scenes/Items so a little hand will be most appriciated.

  • You need a list of ActionItem*, not just ActionItem. A GraphicsItem is not copyable.

  • Thanks. It does the trick :) It wasnt so scary afterall. I was about to ask if it is possible to place text or image in QGraphicsRectItem? I was able to repaint it by *setBrush()* method. Before I dig into QAssistant, Id rather know if it`s possible. My idea is parsing a image and update parts of the screen.
    [quote author="Asperamanca" date="1415176087"]You need a list of ActionItem*, not just ActionItem. A GraphicsItem is not copyable.[/quote]

  • For text, you can use QGraphicsSimpleTextItem or QGraphicsTextItem (editable, but relatively slow)
    For images, you can use QGraphicsPixmapItem

    If you want to combine this in one item, there are multiple way to do it:

    • Create your own QGraphicsItem, and do what you want in paint (drawRect, drawPixmap, drawText,...)
    • Create your own QGraphicsItem, and add child GraphicsItems (Rect, Text, Pixmap) as you need them. You basically combine primitives to a more elaborate item. In this case, you paint() method is empty.
    • Create a QGraphicsItemGroup, and add items as you need them. This is probably the quickest to do, but hardest to expand approach.

  • Thanks, I kind of achieved what I needed with relatively small amount of time, and with your help, ofcourse. Here is the almost-done product:
    There will be no pink sqares, just debug update to see if the mouse clicks are doing something. Now all I have to do is to update the WxH pictures, and do some logiscs. Thanks again. I am just a totaly newbie in Qt.

  • [quote author="heatblazer" date="1415187328"]I am just a totaly newbie in Qt. [/quote]

    That's where we all started once, and it's good not to forget it. :-)

Log in to reply