I'm attempting to retrieve certain text from a QT based window (on Windows). The text is contained in a QGraphicsView (actually, a class that inherits QGraphicsView).
I'm able to intercept it with a series of hooks due a specific call hierarchy in the application, though this is not a very desirable solution.
While the particular text item is likely to be custom, it would be of great help to just enumerate all the items in a QGraphicsView - if possible with class names.
Any pointers on how to go about this will be appreciated.
Note:
I've been having problems with the items() function of QGraphicsView, it seems to be impossible to iterate over either with QListIterator or with items().at().
Related
How do you use QStyledItemDelegate / QItemDelegate to permanently display a complex widget, i.e. to handle the Qt.DisplayRole, not just Qt.EditRole?
The documentation eludes to using paint()... but that's just way to complex! Let's take for example rendering a QTreeView or QTableVeiw inside of a QTableView cell.
There is QAbstractItemView.setIndexWidget(), but that is a bad idea as it only used to display static content (and whats the fun in static models?).
Note
I found part of the answer in another post, but it was only a small subset of the answer, so I thought it warranted a new post with proper question.
The key is to use QAbstractItemView.openPersistentEditor() to always keep the cell in edit mode.
Some additional key elements
The Qt.EditRole flag will need to be provided for the cells which use delegates.
QStyledItemDelegate.sizeHintChanged.emit(index) needs to be called anytime the size of the editor widget has changed.
Implementing QStyledItemDelegate.sizeHint() can be tricky and tedious (or you can do index.internalPointer().editor_widget.sizeHint() assuming you saved a reference of the editor to the internal pointer during QStyledItemDelegate.createEditor()
here is a good post on how to determine sizes: What are the mechanics of the default delegate for item views in Qt?
Caution
It should be mentioned that opening editors is costly, so if you have thousands of indexes and they are all loaded at once, it can take a while. There are many ways to mitigate this issue:
Load the model incrementally using a thread
Use Qt's fetchMore() mechanism
call openPersistentEditor incrementally (using a timer, or as they come into view for the first time)
call openPersistentEditor when the parent is expanded and closePersistentEditor when the parent is collapsed, and possibly restrict the use of expand-all on nodes with many children.
I need to create a chat widget for communication with toy satellite. The satellite can send a child text or a picture, while a child can send only text.
I am programming this app on Qt, but stuck with proper model creation. Sure it have to be a list model, but in my case I've got two types of messages (picture or text) and also some additional data, like date and location (last only in messages from satellite).
What class I better to subclass or use as is for my task? Is standard model?enough or I need to subclass list abstract model?
Also I red, that some customization can be done even inside a model, like font color and so on. I am just wondering, is it a right place for it, or I better set background color of messages in delegate?
Just to mention, this class will be then used in a qml.
I think the easiest way for you is to go with QTreeWidget()
I am creating my first app in QT and wanted to design a list. The listitem has two texts and one icon.
The problem is, i cant find any example or helping material, Only helping link i found is : Customize QListWidgetItem but i dont understand it. Although i have the same problem which this links points to...
What i understand is, i have two options:
1- Customize QListWidgetItem to use with QListWidget
2- Make some delegate to use with QListView
I was hoping to get started with option 1. Now in the link, some reply talks about "myItem" which is taking 2 texts as input. I want to know the implementation of "myItem".
In the link you posted, MyItem is just a normal QWidget. This means that you can create a widget in Qt Designer and then set that new widget you created as the widget that the QListWidgetItem should use for display. In the above example, MyItem takes two strings because there is a vertical layout with two labels in it (that's my assumption at least).
You should also note, and is discussed in the above link, that taking the approach of setting an item widget to use for every item in the list is an expensive thing to do in terms of performance and memory consumption. Because a QListWidget is a QListView, you can set an item delegate on it just like any other view and come out with a lighter weight solution (one instantiation of class vs. one instantiation for every item in your list)
I'm trying to give a custom class that inherits from QLabel to be draggable. Towards that end, I'm trying to create a temporary copy of the class at the current mouse position using the following code inside of the class' mousePressEvent:
QLabel *child = qobject_cast{QLabel*}(this->childAt(event->pos()));
NOTE: this line has carrots instead of brackets, but stack overflow interprets it and takes it out
if (!child)
return;
The child is never created, and I can't figure out why. Any ideas?
If your code is within your QLabel-derived class, childAt() is not the right function. That would return a child widget contained within your label. It doesn't sound like that is what you are trying to do, but correct me if I am misunderstanding.
The object you want to copy is this, but "copy" can have many meanings in c++, and I am not sure exactly you are trying to do. You will probably have to implement it yourself, perhaps with a method called clone() that creates a new instance of your class and populates the values you need to reproduce.
I suspect, though, that there is a better way to implement the drag and drop functionality you are looking for without a copy.
I've got a QTableView for which I want to display the last column always in edit mode. (It's a QComboBox where the user should be able to always change the value.)
I think I've seen the solution in the Qt documentation, but I can't find it anymore. Is there a simple way of doing it?
I think I could archive this effect by using openPersistentEditor() for every cell, but I'm looking for a better way. (Like specifying it only one time for the whole column.)
One way to get the automatic editing behaviour is to call the view's setEditTriggers() function with the QAbstractItemView::AllEditTriggers value.
To display the contents of a given column in a certain way, take a look at QAbstractItemView::setItemDelegateForColumn(). This will let you specify a custom delegate just for those items that need it. However, it won't automatically create an editor widget for each of them (there could in principle be thousands of them), but you could use the delegate to render each item in a way that makes it look like an editor widget.
There are two possibilities:
Using setIndexWidget, but Trolltech writes:
This function should only be used to
display static content within the
visible area corresponding to an item
of data. If you want to display custom
dynamic content or implement a custom
editor widget, subclass QItemDelegate
instead.
(And it breaks the Model/View pattern…)
Or using a delegate's paint method. But here you have to implement everything like enabled/disabled elements yourself.
The QAbstractItemModel::flags virtual function is called to test if an item is editable (see Qt::ItemIsEditable). Take a look at Making the Model Editable in the Model/View Programming documentation.
I can't see an easy way to do this, but you might be able to manage by using a delegate. I honestly don't know exactly how it would work, but you should be able to get something working if you try hard enough. If you get a proper delegate, you should be able to set it on a whole view, one cell of a view, or just a column or row.