Highlight an item in QCombobox by text color
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:
or like this:
Code:
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?
Re: Highlight an item in QCombobox by text color
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.
Re: Highlight an item in QCombobox by text color
I looked at delegates, however have not found how to draw selected item. Do You have any example link?
Re: Highlight an item in QCombobox by text color
Re: Highlight an item in QCombobox by text color
Have found the solution after writing bug report to nokia. ;) 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:
Code:
connect(myCombobox, SIGNAL(currentIndexChanged(int)), myCombobox, SLOT(setupComboboxFont()));
in slot we write:
Code:
void KComboboxBox::setupComboboxFont()
{
QFont comboboxFont
= font
();
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! :)
Re: Highlight an item in QCombobox by text color
Quote:
Originally Posted by
AlekseyK
Have found the solution after writing
bug report to nokia. ;) 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:
Code:
connect(myCombobox, SIGNAL(currentIndexChanged(int)), myCombobox, SLOT(setupComboboxFont()));
in slot we write:
Code:
void KComboboxBox::setupComboboxFont()
{
QFont comboboxFont
= font
();
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();
Re: Highlight an item in QCombobox by text color
Quote:
Originally Posted by
emp1953
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,
_