PDA

View Full Version : Highlight an item in QCombobox by text color



AlekseyK
7th February 2011, 15:39
I have QCombobox, QStandardItemModel is used as a data model for it, every item in QCombobox it is a QStandardItem respectively, QTreeView is used as view. I try to select some (parent) combobox items by blue color like this:

QStandardItem* all = new QStandardItem("some text");
all->setForeground(QBrush(QColor(Qt::blue)));
or like this:

all->setData(Qt::blue, Qt::ForegroundRole);
In drop down list (tree) that element (item) is blue (highlighted), however when I select that item its text becomes black again which is default for all the items in the combobox. How can I fix that to keep desired item color when I select it?

franz
7th February 2011, 16:49
Try creating and setting a custom item delegate. The way selections are drawn by the delegate, and it is there that it is decided how to draw the selected item.

AlekseyK
7th February 2011, 22:57
I looked at delegates, however have not found how to draw selected item. Do You have any example link?

AlekseyK
8th February 2011, 17:08
Seems delegate does not solve the problem:
http://www.qtforum.org/article/26686/qcombobox-set-item-delegate-not-painting-for-current-item.html

AlekseyK
8th February 2011, 23:06
Have found the solution after writing bug report to nokia (http://bugreports.qt.nokia.com/browse/QTBUG-17309). ;) Solution: connect signal currentIndexChanged(int) of the combobox (signal activated(QModelIndex) from QTreeView was not emitted for some reason) and process it in a slot to change parent items color and font when selected:

connect(myCombobox, SIGNAL(currentIndexChanged(int)), myCombobox, SLOT(setupComboboxFont()));
in slot we write:

void KComboboxBox::setupComboboxFont()
{
QFont comboboxFont = font();
QPalette comboboxPalette = palette();
QModelIndex index = ((QTreeView *)view())->currentIndex();
if(!index.parent().isValid()) // parent index
{
comboboxFont.setBold(true);
comboboxPalette.setColor(QPalette::Text, Qt::blue);
comboboxPalette.setColor(QPalette::WindowText, Qt::blue);
}
else
{
comboboxFont.setBold(false);
if(parentWidget() != 0)
comboboxPalette = parentWidget()->palette();
}
setFont(comboboxFont);
setPalette(comboboxPalette);
}
That is all!!! Workable! :)

emp1953
18th March 2019, 21:12
Have found the solution after writing bug report to nokia (http://bugreports.qt.nokia.com/browse/QTBUG-17309). ;) Solution: connect signal currentIndexChanged(int) of the combobox (signal activated(QModelIndex) from QTreeView was not emitted for some reason) and process it in a slot to change parent items color and font when selected:

connect(myCombobox, SIGNAL(currentIndexChanged(int)), myCombobox, SLOT(setupComboboxFont()));
in slot we write:

void KComboboxBox::setupComboboxFont()
{
QFont comboboxFont = font();
QPalette comboboxPalette = palette();
QModelIndex index = ((QTreeView *)view())->currentIndex();
if(!index.parent().isValid()) // parent index
{
comboboxFont.setBold(true);
comboboxPalette.setColor(QPalette::Text, Qt::blue);
comboboxPalette.setColor(QPalette::WindowText, Qt::blue);
}
else
{
comboboxFont.setBold(false);
if(parentWidget() != 0)
comboboxPalette = parentWidget()->palette();
}
setFont(comboboxFont);
setPalette(comboboxPalette);
}
That is all!!! Workable! :)

In Line 5, I get a compiler error for view() are you casting view() to a (QTreeView *)

error: 'view' was not declared in this scope
QModelIndex index = ((QTreeView *)view())->currentIndex();

anda_skoa
18th March 2019, 22:31
In Line 5, I get a compiler error for view() are you casting view() to a (QTreeView *)

error: 'view' was not declared in this scope
QModelIndex index = ((QTreeView *)view())->currentIndex();

Well, view() is a valid method in a QComboBox subclass, so maybe you are not inside such a class?

Cheers,
_