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. [Solved] Can't move QPushButton in QGraphicsScene?
Forum Updated to NodeBB v4.3 + New Features

[Solved] Can't move QPushButton in QGraphicsScene?

Scheduled Pinned Locked Moved General and Desktop
16 Posts 4 Posters 15.9k 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.
  • R Offline
    R Offline
    rokemoon
    wrote on last edited by
    #7

    Andre, thanks a lot, this is exactly what I want.
    And again thanks

    1 Reply Last reply
    0
    • F Offline
      F Offline
      feliperfranca
      wrote on last edited by
      #8

      Andre
      Could you show how would be implemented "paint" method?
      I tried but had no success

      thanks

      1 Reply Last reply
      0
      • A Offline
        A Offline
        andre
        wrote on last edited by
        #9

        If you show us what you tried, we may be able to help you fix the issue.

        1 Reply Last reply
        0
        • F Offline
          F Offline
          feliperfranca
          wrote on last edited by
          #10

          Ok
          I'm doing in pyqt

          @class ItemUI(QGraphicsRectItem):

          def init(self, itemUI, scene=None, parent=None):
          super(ItemUI, self).init(parent, scene)
          self.setFlag(self.ItemIsMovable, True)
          self.setFlag(self.ItemIsSelectable, True)
          self.item = itemUI
          self.setRect(QRectF(10, 10, 100, 50))

          def paint(self, painter, option, widget=None):
          self.item.style().drawControl(QStyle.CE_PushButton, option, painter, widget)@

          • itemUI is the UI Item, for example a QPushButton
          • Nothing shows on the screen
          1 Reply Last reply
          0
          • A Offline
            A Offline
            andre
            wrote on last edited by
            #11

            I still find Python hard to read, so I might be off here:

            I think you will need to construct your own option struct, and fill it with the proper values for the button that you want to draw. You can not just pass a QStyleOptionGraphicsItem if you want to draw a button and expect it to work.

            1 Reply Last reply
            0
            • F Offline
              F Offline
              feliperfranca
              wrote on last edited by
              #12

              OK
              I have no idea of how to create a option struct but I'll try here...
              any doubt I will return here
              Thank you for your assistance and availability.
              and sorry for my poor english =]

              1 Reply Last reply
              0
              • R Offline
                R Offline
                rokemoon
                wrote on last edited by
                #13

                try this creation and further adapt to your implementation
                @
                void GraphicsRectItemButton::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget widget)
                {
                QStyle
                style = qApp->style();
                QStyleOptionButton buttonOption;

                buttonOption.rect = option->rect;
                buttonOption.state = option->state;
                buttonOption.text = "ItemButton";
                style->drawControl(QStyle::CE_PushButton, &buttonOption, painter, widget);
                }
                @

                1 Reply Last reply
                0
                • B Offline
                  B Offline
                  bootchk
                  wrote on last edited by
                  #14

                  feliperfranca:

                  I think I understand what you are trying to do: pass a QPushButton to your ItemUI instance, then ask the button to paint itself, as the implementation of your ItemUI.paint().

                  I believe Andre is saying that the paint method of a QGraphicsRectItem is passed an option of type QStyleOptionGraphicsItem but QStyle.drawControl expects an option of type QStyleOptionButton. Both types are subclasses of QStyleOption, but they may have different attributes.

                  Also, who knows the default attributes of a QPushButton's style? One would hope it defaults to something sensible and visible.

                  Also, if the QPushButton instance is not realized (shown), possibly it's paint method (style.drawControl()) returns a None path (just calls Python pass)?

                  Possibly you should do the painting yourself, instead of asking the button widget to paint itself. You could access the style of the button for attributes needed to paint. But I agree with your design, if you use the button's paint implementation, it would be foolproof against changes to Qt toolkit. You would get a style that matches the style of other buttons in the GUI. Your ItemUI would be a graphics item, not a widget, but would look like a button widget.

                  I would be interested in any solution you came up with.

                  1 Reply Last reply
                  0
                  • A Offline
                    A Offline
                    andre
                    wrote on last edited by
                    #15

                    What you could do, is look into the way the Quick components for desktop are implemented. As you may or may not be aware of, in Quick 1.x, items there are QGraphicsItems. The desktop components render these items using the Qt style API. I think that could be a good source of inspiration. The full source code is online for it.

                    1 Reply Last reply
                    0
                    • B Offline
                      B Offline
                      bootchk
                      wrote on last edited by
                      #16

                      Thanks, thats a good idea.

                      I think you are saying that you don't need to create a dependency on QWidget (shouldn't, since the QGui module is deprecated on some platforms?) Instead the Qt style API has all you need to insure that the style of your custom GUI items (whatever you call them: widgets, controls, etc.) conform to the platform's guidelines and the user's preferences. Don't arbitrarily fabricate a style attribute if it can come from the Qt style API.

                      This is pertinent to my case because I am implementing a custom control that in a QGraphicsView. It is a tracking button. It appears on hover. It can be clicked. It follows the mouse (to a certain extent.) It is part of a tracking menu (a tracking menu is a set of tracking buttons.) I am studying whether it needs to be a QWidget (so that it receives mouseMoveEvent easily) or a QGraphicsItem. This is different from what Rokemoon wants (his tracks, ie follows the mouse, when clicked, mine tracks before clicked.) The handling of mouseMoveEvent seems to be easier if it is a QWidget, but as you suggest, it could be a QGraphicsItem as in Qt Quick. I need to study Qt Quick and the design reasons their controls are QGraphicsItems. It is very interesting the differences between event dispatch in QWidget trees and QGraphicItem trees.

                      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