QListView item background color - c++

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.

Related

Draw more than one widget in QListView item

I created my own data model based on QAbstractListModel and connected it with QListView by simple ui->listView->setModel(prodModel) - it works fine.
Now I want to create custom view in QListView for each item. For example, I want to reach that every data row from model will be shown in style like Stack Overflow (question with rating). I want to use Qt widgets and arrange it on my own idea - using layouts etc.
Unfortunately, a Qt's guide about model/view doesn't have described how to use QItemDelegate to show my widgets - I'm not interested about editing data in view (but I want to interact like click - ideally if I could click and get signal/sth in each widget in my custom view).
I found some examples, but they describe drawing only one widget (QProgressBar) without any layouts.
Any help will be appreciable, thanks!

Is it possible to disable the light-blue mouse over highlighting on a QTreeWidgetItem?

I've a QTreeWidget and need to disable the mouse over highlighting on the childItems but not the click selection. The point here is, that I need to set this per Item because some are selectable. I was thinking about the QTreeWidget::itemEntered signal to check if the item should be highlighted or not but I can't get it to work because the description says
QTreeWidget mouse tracking needs to be enabled for this feature to
work.
and I can't figure out how.
So my questions for are: How can I enable mouse tracking?
Is there an easier way to disable the highlighting?
Simply invoke setMouseTracking() to enable mouse tracking for a specific widget.
I ran into this problem (I know this is an old post, but I might as well post my solution, since it can be useful for others).
I could not properly disable the mouse feedback while keeping the mouse tracking enabled, but I could make this feedback invisible. I'm using qss stylesheets, and I set the mousehover feedback color to transparent:
MyTreeWidget::item:hover {
background-color: transparent
}
It did the trick for me. Sadly it makes the feedback invisible all the time, rather than allowing to turn it off and on.
So as a next step, for when I needed it, I implemented my own feedback by using a delegate and overwritting the paint function.
The QTreeView overwrite mouseMoveEvent and sends mouse coordinates to the delegate. This way, the delegate can adapt what it does in paint to this position. It feels pretty heavy, and a bit dirty, but it works. Delegate should also allow to have different behavior for different items.
PS: If you're using a delegate, in most cases, that should be enough without the qss change. In my case it wasn't, because I call QStyledItemDelegate::paint in my overwritten paint method, so I inherited some unwanted behavior.

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!

gtkmm button coloring

I'm trying to change the background color of a button upon clicking it. I've connected the button to a clicking method just fine, but I can't seem to find the correct c++ syntax to create this. I've seen it done in python, but that doesn't exactly help me. Anyone have a tutorial or know the syntax?
EDIT: That makes sense. Thanks!
Buttons don't have color, they contain a child object and emit a signal when pressed, that's it.
You are likely putting a Label in the Button as the child object. A Label is text rendered by Pango, which lets you set attributes. What you think is the Button color is actually the background color of the Label text.
Gtk is pretty complex, but lets you do anything. If you want to do much with Gtk, look for a tutorial on Pango (text) and Cairo (images). If you want a simpler self-contained widget set, check out wx or tk.

stable sorting QTreeWidgetItems in QTreeWidget?

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.