[Solved] Item delegates inactive problem
-
Hi, recently i realized that when window becomes inactive (focus on another window), item delegates in views seems active. First i experienced this problem in my project. I could not find a solution. Then i looked at the Qt Demos (Star Delegate Example) and same problem exists here too. Image that shows the problem: !http://s18.postimage.org/j5wsubtu1/image.png(problem)!
Anybody knows a solution?
-
Interesting question!
First, I think you'd need to get a pointer to the widget you're painting on. You can do this directly by just adding an argument to the constructor of your delegate, or you might be able to retreive it via the painter argument of the paint() method (QPainter::device() returns a QPaintDevice*, which you could try to cast to a QWidget*).
QWidget has a method isActiveWindow(). That should help you decide on the style to use for painting.
Next thing to check is if activating/deactivating the window causes a repaint already or not. If not, you will have to trigger one. You should be able to do that by listening for the QEvent::WindowActivate and QEvent::WindowDeactivate events.
-
Afaik, when focus out from a view, delegates are repainted. But by major problem is i can't draw my delegate's background like the other columns. I'm drawing my delegate as a widget. A sample code:
@
void ValueItemDelegate::paint(QPainter *painter,
const QStyleOptionViewItem &option,
const QModelIndex &index) const
{
const Data data = index.data(Qt::DisplayRole).value<Data>();/* I created a designer form class (DataView) to ease painting. */ DataView form(data); form.setAutoFillBackground(true); form.resize(option.rect.size()); if (option.state & QStyle::State_Selected) { form.setBackgroundRole(QPalette::Highlight); } else { if (option.version > 1) { if (QStyleOptionViewItemV2(option).features & QStyleOptionViewItemV2::Alternate) form.setBackgroundRole(QPalette::AlternateBase); else form.setBackgroundRole(QPalette::Base); } else { form.setBackgroundRole(QPalette::Base); } } painter->drawPixmap(option.rect, QPixmap::grabWidget(&form, form.rect()));
}
@I tried setting a new palette that current color group is set to Inactive or Disabled. It didn't work. I looked for correct color roles and couldn't find one.
-
So, it seems your real issue is that you can't seem to convince your form that it is disabled. I think what happens is that a widget sets the color group it uses for rendering itself. Setting it in advance would not work then. You could try to modify the palette by copying the values for Inactive to the values for the Active color group if the window is not the active window.