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. Change how selected a QGraphicsItem object is displayed
Forum Updated to NodeBB v4.3 + New Features

Change how selected a QGraphicsItem object is displayed

Scheduled Pinned Locked Moved Solved General and Desktop
2 Posts 2 Posters 333 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.
  • M Offline
    M Offline
    Michael Lehn
    wrote on last edited by Michael Lehn
    #1

    Hi,

    I have a class JoinItem derived from QGraphicsRectItem. When selected such an item should be painted blue and otherwise black. Instead of the default where it gets surrounded by a dashed rectangle. So I just want to change how a selected QGraphicsRectItem is displayed (everything else should be as before).

    My implementation works but I wonder whether it is the "right thing" ...

    In JoinItem the paint method is implemented as follows:

    void
    JoinItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
                    QWidget *widget)
    {
        QStyleOptionGraphicsItem opt = *option;
        if (isSelected()) {
            setPen(QPen(Qt::blue, 2));
            opt.state = QStyle::State_None;
        } else {
            setPen(QPen(Qt::black, 2));
        }
        QGraphicsRectItem::paint(painter, &opt, widget);
    }
    

    So my hack is that QGraphicsRectItem::paint simply never gets the state QStyle::State_Selected. Making a copy of option in ant case seems to be a crude hack.

    Pl45m4P 1 Reply Last reply
    0
    • M Michael Lehn

      Hi,

      I have a class JoinItem derived from QGraphicsRectItem. When selected such an item should be painted blue and otherwise black. Instead of the default where it gets surrounded by a dashed rectangle. So I just want to change how a selected QGraphicsRectItem is displayed (everything else should be as before).

      My implementation works but I wonder whether it is the "right thing" ...

      In JoinItem the paint method is implemented as follows:

      void
      JoinItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
                      QWidget *widget)
      {
          QStyleOptionGraphicsItem opt = *option;
          if (isSelected()) {
              setPen(QPen(Qt::blue, 2));
              opt.state = QStyle::State_None;
          } else {
              setPen(QPen(Qt::black, 2));
          }
          QGraphicsRectItem::paint(painter, &opt, widget);
      }
      

      So my hack is that QGraphicsRectItem::paint simply never gets the state QStyle::State_Selected. Making a copy of option in ant case seems to be a crude hack.

      Pl45m4P Offline
      Pl45m4P Offline
      Pl45m4
      wrote on last edited by
      #2

      @Michael-Lehn said in Change how selected a QGraphicsItem object is displayed:

      My implementation works but I wonder whether it is the "right thing" ...

      Should be fine since you only change the style. The selection behavior should not be affected


      If debugging is the process of removing software bugs, then programming must be the process of putting them in.

      ~E. W. Dijkstra

      1 Reply Last reply
      0
      • M Michael Lehn has marked this topic as solved on

      • Login

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