Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QGraphicsView/Item/Rects with events question
Qt 6.11 is out! See what's new in the release blog

QGraphicsView/Item/Rects with events question

Scheduled Pinned Locked Moved General and Desktop
6 Posts 2 Posters 2.4k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    heatblazer
    wrote on last edited by
    #1

    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:
    [CODE]
    #ifndef ACTIONITEM_H
    #define ACTIONITEM_H
    #include <QGraphicsRectItem>
    #include <QGraphicsSceneMouseEvent>
    #include <QDebug>
    #include <QtCore>

    class ActionItem:QGraphicsRectItem {

    private:
    int __privateId;
    QString __name;
    protected:
    void mousePressEvent(QGraphicsSceneMouseEvent* event);
    public:
    ActionItem(int x, int y, int w, int h);
    ActionItem();
    };
    #endif // ACTIONITEM_H
    [/CODE]

    [CODE]
    #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";
    }

    [/CODE]

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

    class MainWindow : public QWidget {
    Q_OBJECT
    public:

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

    private:

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

    };

    #endif // MAINWINDOW_H

    [/CODE]

    [CODE]
    #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);
    __scene->addLine(QLineF(0,0,320,240));
    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(actionItems.at(j));
        j++;
    }
    __view->show();
    

    }

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

    [/CODE]

    So, actually this code fails me in ActionItem inheritance line ( 4 errors ):
    [CODE]
    class ActionItem : QGraphicsRectItem {
    ...
    [/CODE]

    MOC says:
    [CODE]
    /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;
    ^
    [/CODE]

    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.

    1 Reply Last reply
    0
    • A Offline
      A Offline
      Asperamanca
      wrote on last edited by
      #2

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

      1 Reply Last reply
      0
      • H Offline
        H Offline
        heatblazer
        wrote on last edited by
        #3

        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]

        1 Reply Last reply
        0
        • A Offline
          A Offline
          Asperamanca
          wrote on last edited by
          #4

          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.
          1 Reply Last reply
          0
          • H Offline
            H Offline
            heatblazer
            wrote on last edited by
            #5

            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:
            https://www.flickr.com/photos/heatblazer/15717775992/
            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.

            1 Reply Last reply
            0
            • A Offline
              A Offline
              Asperamanca
              wrote on last edited by
              #6

              [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. :-)

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved