Highlight item with mouse hover in QTreeView? - qt

I have a QStandardItemModel that I am displaying as a QTreeView with multiple columns. How can I make it highlight rows when the mouse hovers over them?
Related pages
How to catch mouse over event of QTableWidget item in pyqt?:
similar question using QTableWidget instead of Q*View.
Customizing QListView: suggests it is possible to do it very
easily with stylesheets, but I don't quite follow the c++.
How to highlight the entire row on mouse hover in QTableWidget
QListView selection with highlight/hover

You can achieve this by a stylesheet
treeView->setStyleSheet("QTreeView::item:hover{background-color:#FFFF00;}");

Lahiru's answer is easy to translate to PyQt/PySide, as the input to setStyleSheet doesn't need any modification: it is the same in Qt/PyQt/PySide:
treeView.setStyleSheet("QTreeView::item:hover{background-color:#999966;}")
I found it helpful to read the Overview of style sheet syntax for Qt. Also, this answer has some nice examples on using style sheets in PySide/PyQt.

Related

How can I make a QMenuBar item appear over its QMenu

First of all, I'm fairly new with Qt and Qt Creator so go easy if this is a stupid question.
I was practicing using Qt Creator, playing around with css styles. In particular, I'm trying to get the menubar and its menus to look something like this (on Windows): http://i.stack.imgur.com/9lMnQ.png.
However, the closest I've been able to get so far is this: http://i.stack.imgur.com/5Nlen.png.
I've searched online to see if anyone has tried something like this but I wasn't able to find anything.
The only possible solution I can think of is if the menubar item (with no bottom border) could be rendered in above the menu, so that they overlap, covering its top border over the width that they overlap.
If that won't work or is impossible or whatever please do suggest any other solutions/workarounds/hacks.
Thanks in advance!
I think that the only good solution is to avoid any tricks and create a new widget:
Create a new class inherited from QWidget with Qt::Popup attribute.
Place a QMenu into a layout of the widget.
Get a position of QMenuBar item which is clicked using QMenuBar::getActionGeometry.
Calculate position of the widget and of the tab in the widget to be placed over the menubar item.
Customize form of the widget using QWidget::setMask to make it look like a rectangle with a tab.
Show your widget instead of QMenu.

Tooltips for QDockWidget close button & float button?

Is there a way to set a tool tip text for the close button & float button in a QDockWidget ?
As ixSci mentioned, using setTitleBarWidget() is a potential way of solving this problem. Having said that I was looking for a much simpler solution, ideally using QSS (Qt Style Sheets). So after digging into the source code of QDockWidget I found an alternative way which suits my requirement better.
I wanted to find the place these float and close buttons are created. That is inside QDockWidgetPrivate::init() method in QDockWidget.cpp.
As for an example, the float button is created like this:
QAbstractButton *button = new QDockWidgetTitleButton(q);
button->setObjectName(QLatin1String("qt_dockwidget_floatbutton"));
QObject::connect(button, SIGNAL(clicked()), q, SLOT(_q_toggleTopLevel()));
layout->setWidgetForRole(QDockWidgetLayout::FloatButton, button);
Now all I need is to use the flexibility of Qt Style Sheets, for that I need only the Object Name, in this case it's "qt_dockwidget_floatbutton"
So all you need to do, to set tooltips for Close and Float buttons of a QDockWidget, is to add following two lines of styles in your application style sheet
QAbstractButton#qt_dockwidget_closebutton{qproperty-toolTip: "Close";}
QAbstractButton#qt_dockwidget_floatbutton{qproperty-toolTip: "Restore";}
You can implement whatever title widget you want and set it with setTitleBarWidget(). In that widget you can add whatever buttons with tooltips you need.

Subclassing QMessageBox

I need to customize QMessageBox. I need to remove the frame and title bar and add my own title bar and close button. Also need to replace the standard buttons and probably redo the background color of the box.
Is it possible to subclass it and achieve the above? is there any example anywhere for this? Or, should I just subclass Dialog and create my own message box?
This tutorial on custom windows might help you. It's in French but the code examples are in English, it shows how to compose your own title bar, create a window and attach the new title bar on it. I've been through it before, it's pretty straightforward once you've done it.
There is no need to subclass QMessageBox or QDialog. You can pass a QMessageBox the parameter Qt::FramelessWindowHint to remove the frame and buttons. You can also use Qt Style Sheets to style the background of the QMessageBox as well as the buttons. Something like this should work:
msgBox->setStyleSheet("QDialog {background-color: red;}"
"QPushButton {background-color: blue;}")
I haven't tested this but it should work or be pretty close.

What's the best way to show "focus" in a grid?

I'm drawing a standard grid view with rows and columns, but I want to do something unique and interesting to mark the focused or selected cell. Any ideas? The dotted-line rectangle is out. :)
Allow me to through an idea - hook the onfocus/onclick client events of the gridview cells and apply your css classes or some kind of jQuery animation when those events are raised.

Individual QTreeWidgetItem indentation

Is it possible to have individual indentation of items in a QTreeWidget?
In specific, I have have a column containing both text, icon and for some of them a CheckBox. The items without a CheckBox gets shifted to the left so the indentation of the icon and the text is not inline with the others. Could maybe be fixed with a hidden CheckBox if that is possible?
Maybe the use of Delegates will give you a nice and proper implementation. You'll have the opportunity to re-implement the paint() and sizeHint() methods, and therefore, choose the way your QTreeWidgetItem are being drawn...
More documentation here : http://doc.trolltech.com/4.6/model-view-delegate.html
An example : http://doc.trolltech.com/4.6/itemviews-pixelator.html
Hope it helps a bit !
You can try using the QWidget::setContentMargins() on the widget returned by QTreeWidget::itemWidget().

Resources