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. Custom QGraphicsItem Explanation

Custom QGraphicsItem Explanation

Scheduled Pinned Locked Moved General and Desktop
7 Posts 3 Posters 2.8k 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.
  • D Offline
    D Offline
    Ducky
    wrote on last edited by
    #1

    Hi all,

    Just a quick question because i don get it.

    I have followed "This":https://www.youtube.com/watch?v=hgDd2QspuDg&list=PL2D1942A4688E9D63&index=82 Video that lets you create a Custom QGraphicsItem but i was a little bit puzzled by something i did.
    I built the project before i entered (line 8)"setFlag(ItemIsMovable);"
    if i click the Square it only changed the collor to red.
    but when release the mouse button it did not change it back into green.

    But when id do enter (line 8) "setFlag(ItemIsMovable);" it works like it supposed to.

    Can some one explain why it don't work without the setFlag option.

    @#include "square.h"
    #include <QGraphicsItem>
    #include <QDebug>

    Square::Square()
    {
    Pressed = false;
    setFlag(ItemIsMovable);

    }

    QRectF Square::boundingRect() const
    {
    return QRectF(0,0,100,100);
    }

    void Square::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
    {
    QRectF rec = boundingRect();
    QBrush brush(Qt::blue);

    if(Pressed)
    {
        brush.setColor(Qt::red);
    }
    else
    {
        brush.setColor(Qt::green);
    }
    
    painter->fillRect(rec,brush);
    painter->drawRect(rec);
    

    }

    void Square::mousePressEvent(QGraphicsSceneMouseEvent *event)
    {
    Pressed = true;
    update();
    QGraphicsItem::mousePressEvent(event);
    }

    void Square::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
    {
    Pressed = false;
    update();
    QGraphicsItem::mouseReleaseEvent(event);
    }

    @

    Well I used to write code in Autoit and work on Windows.
    And now i am trying to write code in QT and work on Ubuntu

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      What happens if you call event->accept() in mousePressEvent ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • D Offline
        D Offline
        Ducky
        wrote on last edited by
        #3

        Hi Thanx for the response

        QT is not recognizing the accept()
        i can do a acceptedMouseButtons() but that is also not the solution.

        Well I used to write code in Autoit and work on Windows.
        And now i am trying to write code in QT and work on Ubuntu

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          What do you mean by Qt doesn't recognize accept ? What did you write ?

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • A Offline
            A Offline
            andreyc
            wrote on last edited by
            #5

            If I understood the docs correctly then a graphics item should not get mouse events if a correct flag is not set.

            From "QGraphicsItem::​mousePressEvent()":http://doc.qt.io/qt-5/qgraphicsitem.html#mousePressEvent
            "The event is QEvent::ignore()d for items that are neither movable nor selectable."

            And from "QGraphicsItem::​setFlags()":http://doc.qt.io/qt-5/qgraphicsitem.html#setFlags
            "By default, no flags are enabled."

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Right ! I've missed the flag part !

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0
              • D Offline
                D Offline
                Ducky
                wrote on last edited by
                #7

                HI sorry for the late replay needed to work for some day's.

                oke i understand it now.

                The way the video explain it was more like "well we put this also into the mix just for fun"

                but it is essential to get the mouse events to set the flag selectebel or movable.

                Thanx allot for explaining that.

                Well I used to write code in Autoit and work on Windows.
                And now i am trying to write code in QT and work on Ubuntu

                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