QTreeView did not refresh data when dataChanged was emitted - qt

I have a very weird issue. Could anyone please help me out of this? Thanks in advance.
I have a QTreeview which shows data from a socket and it uses a custom QAbstractItemModel; When data arrives, dataChanged is emitted. And in the data method of my item model, I qDebug log information. The socket runs in a separate thread.
The weird thing is that if I set a breakpoint in the model's data method, then the method is hit and a cell of QTreeView is updated. But if there are no breakpoints in the data method, the method seems not to be called (no logging from qDebug) and the cell is not updated.
I guess it's related to thread stuff but I don't know what it is exactly. Any help will be greatly appreciated.

Possible you forgot update your QTreeView, in case of breakpoint this event happens automatically, but without it you should update QTreeView or its parent widget in program.

Just identified the root of the issue: The subnodes' parents are not correct. Thanks to all you guys. Happy new year!!!

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().

How do I setup a segue for a viewController to present itself

Ok, it's pretty simple with a storyboard to have one viewController create a segue to another viewController. Simply control-click and drag from one viewController to the other.
How do I create a segue, either in the storyboard, or programmatically to invoke or present another instance of itself?
Why would I want to do this?
I currently have a viewController with a UICollectionView presented from data in a simple one-dimensional array of objects. When I select an item it presents a detail viewController for that item.
What I want to do is modify my data for organizational reasons to allow the objects in the array to hold an array of objects like the original data array, as a folder with sub-folders. It seems reasonable to me that simply updating what the data source is pointing to, and calling the same viewController itself to display the next level should be relatively trivial. When finished, simply pop back a level and be right where you left off.
It seems nontrivial to click-drag from the viewController to itself. Prior to using storyboards and segues, this would be done by simply presenting the view contoller. What is the best way to do this with storyboards and segues?
Yes, it COULD probably be done by manipulating the data source and just redrawing the current viewController, but it seems like it SHOULD be cleaner to just call the viewController with a pointer into the sub-array as if it was the top-level array and re-present the same viewController, letting the view controller stack manage the individual levels without having to redraw the model from different starting points and remembering the starting points in some kind of stack.
Any advice on the best way to do this?
Apparently you cannot invoke or present your view controller on itself.
Likely this is because UIKit is not re-entrant or thread-safe. So, Apple doesn't let you use a segue, or use presentViewController with an argument of self (or anything that resolves to self).
A pity, since it would have been nice to not have to create a sub-view contoller which does all the same things the viewController does.
If someone knows of a better way to do this, please post below.

Qt: Action after QMediaPlayer stopped

I am trying to do some sort of operations/actions after a video of QMediaPlayer has ended. I know you can check the state of the player player->state()and that there is a state for "Stopped". But I would like to know how I can couple this state to a certain action, maybe by using a certain SIGNAL? Does anyone have experience doing this? or have an idea? I can't seem to find a solution to this.
Thanks in advance.
Have you tried the following signal QMediaPlayer::stateChanged( QMediaPlayer::State ) ? When you receive this signal QMediaPlayer::StoppedState parameter the player stopped.

Automatically disconnect after first signal emission

I am loading a web page from a file, then i replace some of the html in it:
self.template_web_page = QtWebKit.QWebPage()
self.template_web_page.mainFrame().load(QtCore.QUrl('template.html'))
def load(ok):
main_window.web_view.loadFinished.disconnect(load)
self.table_element = self.template_web_page.mainFrame().findFirstElement("#table")
self.table_element.setInnerXml(table_html)
main_window.web_view.loadFinished.connect(load)
Is there a way to connect to a signal just for one shot?
As already noted, there doesn't appear to be a better (more concise) way than this.
http://comments.gmane.org/gmane.comp.lib.qt.general/6883 suggests that such a solution is fine, although I have had issues with such a solution myself. I found that if I disconnected the slot at the start of the slot (as is done in the code in the question) and then tried to perform some GUI interactions (setting statusbar text was a problem, but not highlighting a row in a list view), I got an exception due to a NULL pointer dereference. This was with PyQt 4.6.2 for Python 2.6 for Windows. When I moved the disconnect() call to the end of the slot, the problem went away.
Apologies in advance if this is not relevant and it's just a stupid mistake I've made.

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