Solved How to connect two QGraphicsScenes in separate QGraphicsViews?
-
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! -
@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 canconnect()
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 aQItemSelectionModel
. https://doc.qt.io/qt-5/model-view-programming.html. Lots of possibilities! :) -
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! :)
-
Hi
You can actually do like this
class GraphicalXXX : public QObject, public QGraphicsItemAnd 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.
-
@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 -
Hi
You can also use
QGraphicsObject instead of QGraphicsItem as that is a QObject + QGraphicsItem already.
https://doc.qt.io/qt-5/qgraphicsobject.html -
Yes! It worked. Signals and Slots FTW :D Thanks everyone. Until next time!