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. How to connect two QGraphicsScenes in separate QGraphicsViews?
Qt 6.11 is out! See what's new in the release blog

How to connect two QGraphicsScenes in separate QGraphicsViews?

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 3 Posters 1.1k 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
    Hristo Konstantinov
    wrote on last edited by Hristo Konstantinov
    #1

    https://imgur.com/a/kd9HwwV

    I'm a dentist, and this is the software I'm working on. The left QGraphicsView is fully functional and it has shortcut keys, rubberband selection, context menus and so on. Now I want to make some kind of control panel in the right QGraphicsView (for my dumber colleagues, or for eventual touchscreen interface). The control panel has to show the status of the tooth, which is selected (the carieses and the obturations per surface) and also be clickable.
    The data of all the teeth is stored in the main widget as a 2d boolean array ([32][number of possible diagnoses]), and both Scenes have access to it(they are custom, and the array is passed as a reference in their constructors), but the second one has to know, which tooth (or multiple teeth) is selected, so it can paint its status, and when part of this painted status is clicked, it must not only change the boolean array, but it must tell the left scene to update, and set back the focus to it.
    Needless to say I'm new to QT and programming in general. Thanks in advance!

    JonBJ 1 Reply Last reply
    0
    • H Hristo Konstantinov

      https://imgur.com/a/kd9HwwV

      I'm a dentist, and this is the software I'm working on. The left QGraphicsView is fully functional and it has shortcut keys, rubberband selection, context menus and so on. Now I want to make some kind of control panel in the right QGraphicsView (for my dumber colleagues, or for eventual touchscreen interface). The control panel has to show the status of the tooth, which is selected (the carieses and the obturations per surface) and also be clickable.
      The data of all the teeth is stored in the main widget as a 2d boolean array ([32][number of possible diagnoses]), and both Scenes have access to it(they are custom, and the array is passed as a reference in their constructors), but the second one has to know, which tooth (or multiple teeth) is selected, so it can paint its status, and when part of this painted status is clicked, it must not only change the boolean array, but it must tell the left scene to update, and set back the focus to it.
      Needless to say I'm new to QT and programming in general. Thanks in advance!

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @Hristo-Konstantinov
      Have you read through Signals & Slots? This is the way to go about things in Qt. Your two views can send signals about what happened (e.g. "this tooth clicked"), and slots in the other one (or in a controller) can act on the message. You can connect() the signals & slots in your "main widget" or some "controller".

      it must not only change the boolean array, but it must tell the left scene to update

      You can also have the model (here, your 2d boolean array) send a signal whenever any if its data is updated, and have views listen to that (slot) and redraw as necessary. Then you don't have to worry so much about having views "tell" each other what to do explicitly. There is also a QItemSelectionModel. https://doc.qt.io/qt-5/model-view-programming.html. Lots of possibilities! :)

      1 Reply Last reply
      2
      • H Offline
        H Offline
        Hristo Konstantinov
        wrote on last edited by
        #3

        Yes, but since qgraphicscene and graphicitem doesn't have signal and slot mechanism, I thought there must be some other way. If a Graphicsview can return which item is selected in it's scene, I'm all for the signals and slots. Thanks! :)

        mrjjM JonBJ 2 Replies Last reply
        0
        • H Hristo Konstantinov

          Yes, but since qgraphicscene and graphicitem doesn't have signal and slot mechanism, I thought there must be some other way. If a Graphicsview can return which item is selected in it's scene, I'm all for the signals and slots. Thanks! :)

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by mrjj
          #4

          Hi
          You can actually do like this
          class GraphicalXXX : public QObject, public QGraphicsItem

          And add Q_OBJECT to your class.

          Then its Also a QObject and can signals and slots.

          Make sure to clean the build folder and rebuild all.

          GraphicalXXX is your custom class

          Note this is a bit heavy if having TONS of items. In your case, it should not matter at all.

          1 Reply Last reply
          1
          • H Hristo Konstantinov

            Yes, but since qgraphicscene and graphicitem doesn't have signal and slot mechanism, I thought there must be some other way. If a Graphicsview can return which item is selected in it's scene, I'm all for the signals and slots. Thanks! :)

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by JonB
            #5

            @Hristo-Konstantinov said in How to connect two QGraphicsScenes in separate QGraphicsViews?:

            Yes, but since qgraphicscene and graphicitem doesn't have signal and slot mechanism

            QGraphicsScene/View do.

            If a Graphicsview can return which item is selected in it's scene

            https://doc.qt.io/qt-5/qgraphicsscene.html#selectedItems
            https://doc.qt.io/qt-5/qgraphicsscene.html#selectionChanged

            1 Reply Last reply
            2
            • mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Hi
              You can also use
              QGraphicsObject instead of QGraphicsItem as that is a QObject + QGraphicsItem already.
              https://doc.qt.io/qt-5/qgraphicsobject.html

              1 Reply Last reply
              2
              • H Offline
                H Offline
                Hristo Konstantinov
                wrote on last edited by
                #7

                Yes! It worked. Signals and Slots FTW :D Thanks everyone. Until next time!

                1 Reply Last reply
                2

                • Login

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