stable sorting QTreeWidgetItems in QTreeWidget? - c++

I have a list of QTreeWidgetItems (with children) in a QTreeWidget. I do not use a model for my data.
From another window in my application the user can navigate thru the same set of data (viewed differently) and the QTreeWidget in the first window then highlights that specific row by setting the background colour.
However, when the QTreeWidget is sorted on a column where some of the items have the same value it is undefined which item is first. When I then navigate using the other window and the background colour of the item is set, the equal items swaps place in the view automatically. This looks very strange.
I suspect this is due to the sorting algorithm of QTreeWidget, but does anyone know a possible workaround to this?

Are you using QItemSelectionModel to do this, or did you write it yourself?
If you wrote it yourself I would suggest using QItemSelectionModel.
If you didn't, it sounds like you want a custom sorting algorithm which would require creating a derived QTreeWidget, if you are doing that, you might as well just use QTreeView and a custom QAbstractItemModel.
Also, if you have two views of the same data, I would HIGHLY recommended using the Model/View framework and a QTreeView.

Related

Customizing QT QTreeView with custom widgets

In my raster drawing program I need to create a layers interface like in Photoshop or Sketchbook Pro. I read the documentation and figured out that I have to use QTreeView. But I didn't find a lot of information in the documentation about creating QTreeView with custom widgets. So:
1) How to insert custom widgets into tree view?
2) What is the difference between QTreeView and QTreeWidget?
3) What is the difference between QAbstractItemModel and qitemdelegate?
4) Any examples/articles/guides?
5) Maybe I should use something else?
QTreeWidget is a model and a view in one class, it's called a convenience view. It works against the good practice of separatng the views and the models, and probably shouldn't be used in a system where the notion of document layers belongs in the document handling code.
QTreeView is just a view, without any bundled models. When you have a model, you can set it on a view, making the view display the model.
A QAbstractItemModel is the data model. It has nothing to do with views or delegates at all - the model can exist and be useful without a view at all.
A delegate provides display and editing widgets for items of data in a view. It is a property of the view, not of the model. Different views can show the same model using different delegates, all at the same time.
Although the delegate lets you provide the custom widgets you're after, its use may be unnecessary. If the item you display has static contents, you can simply provide a QImage or a QPixmap as the data.
Special for your case (5): DON'T use any of QTreeView, QStandardItemModel and other such classes. If you need interaction with widgets + if you need widgets to be animated then you should use simple QScrollArea with QVBoxLayout inside of it.
Qt MVC is designed to process big amount of cognate data. It is not designed to provide widget-based interaction. So, if you want to "assign" one widget to each item and to interact with them - you will have a lot of problems with implementing delegates (tracking mouse events, providing editor's factory). Ofc, you may create your own delegates with custom drawing and custom processing of mouse events, but it's much easy to use simple widgets.

QT C++ QTableWidget(Item) partially italicize (emphasize) text

Problem
I'm using QTableWidget(Item) for presentation of some data. For that i need to italicize or set bold some parts of text. Problem: I don't want to italicize the whole cell/box, just parts of text in a cell.
Ideas
: Using the QTableWidgetItem refering to the correspondent cell and trying to modify the style (e.g. setFont(font)). But this operations always seem to alter the whole cell?
I also found this which doesn't help me for my QTableWidget problem.
Wanted
: To Have a QTableWidget with e.g. 2 rows and collumns containing cells like:
Exa mple Cell Text
(Nice Notation like Exa<b>mple</b> Cell <i>Text</i> is only a secondary aspect)
Note: Example should be one word.
Solved
I didn't see the wood for the trees. Thanks to Salvatore Avanzo, he is totally right.
All Item Widget classes provide functionality to render/paint something custom in each cell.
QTableWidget provides setCellWidget(.. QWidget..); QListWidget and QTreeWidget provide setItemWidget(..QWidget..). Therefore you can use any Subclass of QWidget (base of all ui objects) to paint something custom. For my aim i needed the capability to set a custom partially emphasized/italicized text. The Refering Subclass of QWidget for painting texts is QLabel (It itself is also a subclass of QPainDevice). A QWidget/Subclass of QWidget is drawed when paintEvent(..) is invoked (QLabel reimplemented that). QLabel also supports RichText functionality (flag is used by default), e.g. you can use <b>foo</b> to get foo.
So that's perfectly fits my problem and i will now use this solution.
Forecast for better structured solution
The unattractive aspect of the above mentioned solution is, that design and data get totally mixed. Editing Text in a QLabel is possible if you set the refering Interaction tags. But then you always need to use QLabel to get the text. When additionally setting an QTableWidgetItem in the Cell which also uses QLabel, they overlay ugly. So one could use QTableView. Here is a link.

A custom widget to display and select among multiple graphic views

I have a collection of images, dynamically generated through QGraphicsView widgets, and i'd like my users to choose between them. For that purpose, i would display inside a custom widget available images in some kind of grid and have users click the one they are interested in.
Multiple questions arise :
is there an existing widget that already fits this purpose ?
should i find a way to disable all mouse event handling by QGraphicsView items, or could i add a transparent widget in front of graphic views which would intercept them ?
is there a performance issue displaying many QGraphicsView widgets (up to a few hundreds) ? Should i export them to plain images first ?
First off, no, there's no widget designed specifically for that purpose.
I don't think you are grasping what QGraphicsView is for. It's for displaying a QGraphicsScene, which is meant to hold many QGraphicsItems. Based on your post, I can't see why you would need multiple QGraphicsViews. You can simply have one QGraphicsView and display many images inside of its scene. For example, see QGraphicsPixmapItem.
You definitely should not have hundreds of QGraphicsViews. You probably just want one (although a few could be justified in certain circumstances), in which you display many QGraphicsItems in a QGraphicsScene. You can definitely have hundreds of QGraphicsItems visible at once. In your case, you probably want QGraphicsPixmapItems, which are a subclass of QGraphicsItem. You could even have multiple QGraphicsScenes, and display whichever one is relevant using QGraphicsView::setScene. If you want the user to be able to select an image from a grid, and then work with that image, I would look to the State Pattern.
I can't think of any reason to disable mouse handling in QGraphicsViews, QGraphicsScenes, or QGraphicsItems. Why should these not handle their own mouse events? You can (and should, where necessary) subclass them and reimplement mousePressEvent, mouseMoveEvent, mouseRelease event, etc. to obtain the functionality you want.
Good luck!

QListView item background color

I'm trying to change the background color of the item which currently has the mouse hover.
What I've done so far is: I subclassed QListView and in the ctor:
connect(this,SIGNAL(entered(QModelIndex)),this,SLOT(enteredSlot(QModelIndex)));
The job basically needs to be done in the slot enteredSlot(QModelIndex) but I have no idea how.
You can achieve what you are trying to do a bit easier by setting a style sheet for your QListView. If you haven't worked with style sheets before you may want to back track in the documentation a bit but most common activities related to customizing the appearance of widgets can be done using them. Do not confuse them with QStyles which is a different styling mechanism.

How to make a QTreeWidget that looks like the WidgetBoxTreeWidget from the designer?

I'd like a toolbox thing sort of like the widget tree in the designer. As far as I can tell, for the categories it simply adds a top level tree item to itself. Also afaict, it is a basic subclass of QTreeWidget, so I should be able to see what it's doing and make the same calls on a QTreeWidget to get the same behavior.
I can't seem recreate the way the categories draw themselves in the designer though. I'm missing something. How is this done?
You'll want to look at Qt's View Delegates. These allow you to change how things are drawn in their item view classes.