Data driven selection in Qt view - qt

I am looking for an elegant way to change the selection in a Qt TreeView based on the data of the underlying model. So my dynamic data contains information about the currently active element which should be selected in the TreeView.
Background: I have a setup with a 3d viewer for a bunch of objects and a Qt TreeView that lists the objects. The user should be able to select objects by klicking them either in the TreeView or in the 3d viewer. The 3d viewer is non-Qt code and modifies the data directly and sets the a flag indicating the selection.
I implemented a dataModel and a selectionModel (as I have to synchronize two views on the data).
My current idea is to emit a signal from the dataModel to the selectionModel to modify the selection when the data has changed. But this seems awkward to me...
Is there a recommended Qt way of doing this?

Related

Passing Objects from own Model to QTableWidget

I am trying to do the following in QT, but I'm unsure of how to do it, or even do it efficiently.
Any input would be greatly appreciated.
I have a QListView and an underlying model that I implemented myself.
The rows of the model include another class "CJTAGPin" which determine the data.
In the rows of the ListView I only need to display certain elements of this class, which works just fine.
In the same window, I have a QTableWidget, where i want rows of the Listview to be allowed to dropped.
But in this Widget, the name displayed in the List ist not enough,
and I would need the instances of "CJJTAGPin" accessible (when pressing a button accessing the cells where Objects were dropped and knowing which instance of CJTAGPin it is).
How can this be achieved?
I already did some research and found that subclassing QMimeData would be an option, but I'm not quite sure which Class I would need to be subclassing and how to rewrite hasFormat(), formats(), and retrieveData().

Qt OpenGL data synchronization / Model/View implementation

I am trying to develop an application with Qt 5.5 and OpenGL. The basic work of the application will be to load simple objects, modify their positions in a scene and save them together with other attributes (material, name, parents/child relations etc...).
The only thing I am struggling about for a week now is that I really don't know how I should take on the problem of synchronizing data. Let's say I have some kind of SceneGraph class which takes care of all SceneObjects. Those SceneGraphs should be rendered in a SceneView-Widget which can be used to modify it's Objects via transformations. Now how would I tell every SceneView that an Object changed it's position?
I thought of the Model/View architecture for a moment but I am not really sure how this implementation should look like.
What would be the best way to handle Objects like that in different Windows/Widgets but still have one single piece of data?
SceneObject:
Holds the mesh-information (verticies, uvs, etc..)
Has a name (QString)
Has a material
Has a transform storing position, rotation and scaling information
(Important: these datatypes should be synchronized in all views)
SceneGraph:
Contains different SceneObjects and is passed to SceneViews
SceneView:
The QWidget responsible for drawing the Scene correctly in any QWindow.
Has it's own camera to move around.
Handles UserInput and allows transformation of SceneObjects.
You could use the signal and slot to observe position updates of SceneObjects and process them in SceneView.

Communication between multiple dialogs in Qt, using signals and slots or references

Im developing a Qt GUI application, with multiple QDialog's open at any given time.
These dialogs need to communicate and notify with each other when special data is received on a separate network thread.
Usually a dialog will hold a reference to at least one other dialog, and thus can communicate using this.
My question is whether to use this reference to call a function in a another dialog, or emit a signal and absorb that signal in a slot in another dialog? Im aware that this might be a preference - but I would still like to get some input on this.
I suggest you start by looking at Qt's Model View Programming. Based on the Model View Controller design pattern, you would be better off separating data (the model) and the view of that data.
If you follow Qt's Model View Programming, you'll find that when the model containing your data changes, the view(s) are notified and react accordingly. You shouldn't need to message from one dialog to another.

Qt View/Model/Data Paradigm: How to modify data?

I just read the Model/View documentation for Qt 4.8. I believe to understand how the Model/View (Delegates/SelectionModel) work. One thing that I'm unsure about is how to manipulate data when I have a for example a TreeView.
Imagine having a TreeView to display a list and buttons to remove elements from this list when an item/row is selected. I see two approaches here
1) In the slot of the PushButton I retrieve the SelectionModel and the ItemModel of the TreeView and call model->removeRow(index.row ...). This way the model (that i subclassed from QAbstractItemModel) manipulates the data that it is supposed to represent.
2) In the slot of the PushButton I remove the item directly from the data source, that the TreeView's model represents. Then I can link the data with the model via signals/slot, such that the model can then tell the TreeView that the underlying data has changed.
The same scenario can be imagined with adding elements. Should I add the new element to the data which signals its changed state to the ItemModel which the informs the TreeView, or should I add the new item through the ItemMode?
I haven't found any Best Practices documentation on this. The two approaches differ strongly, such I would like to know in advanced which one is preferable.
Does anybody have a suggestion which path to follow?
Thanks
1) is preferable - you should probably avoid manipulating your data source directly from the UI code - your life will be better if you go through the model. At the very least add a method to your model to do the data manipulation, and call that method from your UI code.
You will find that some of Qt's methods are protected such that they can only be called from the model itself (e.g. endInsertRows etc.)

Updating a QListView when objects change externally

I've a simple question regarding the update of a QTreeView (or any subclass of QAbstractItemView) when a model object changes externally. Let's say that a list shows a subclass of QAbstractItemModel, and an item of that model gets changed outside of the list window, and we would like to update the list with the change. What is the usual strategy to achieve something like this ? I've looked at the Qt documentation of QAbstractItemModel and there is a signal named 'dataChanged' that is (or should be) emited when data from the model changes. But since this signal (as all QAbstractItemModel functions/signals/slots) work with a QModelIndex, which is not persistent as the documentation clearly says, am i supposed to store somehow a mapping of my data to QPersistentModelIndex(es), so when my data change i will be able to find the corresponding QPersistenModelIndex and use that as argument to the various QAbstractItemModel functions ? Is that what QPersistentModelIndex(es) are used for ? Or am i missing something ?
Thank you.
ps: I guess i could just reload the QTreeView, but then i wouldn't know which items were expanded or which were selected. Is there an strategy to overcome this problem and just reload the list ?
QTreeView already handles the case in which the underlying model's data changed (i.e. the model has emitted the dataChanged() signal). That means you don't need to do any additional work on the view.
If you're implementing your own model (a derived class of QAbstractItemView), and you're making a change to the contents of the model, you simply need to emit the dataChanged() signal when your change is complete. The signal/slot mechanism will automatically inform the view using that signal.

Resources