[Solved] Second QPlainTextEdit acts as if "inactive" when focused
-
Edit: I am using Qt 5.2 on Mac OS 10.9 (under PyQt5.2).
I have two QPlainTextEdit widgets visible, separated by a splitter. Each has a focus policy of Strong focus. I can click in either one and edit text there as expected.
However, only one of them uses the correct palette color for highlighted text. In this QPlainTextEdit, selected text is displayed in a lemon-yellow (#fbed73) when it has the focus, and when focus goes elsewhere, the selection becomes gray.
The second one displays highlighted text with a gray overlay at all times, as if it was inactive, regardless of whether it has the focus or not.
The second widget has a focusInEvent() handler. Here I put code to print out (Python syntax here):
@self.palette().color(QPalette.Highlight).name()@and it displayed #fbed73, lemon yellow. I also printed out self.palette().currentColorGroup() and it was 0 (QPalette::Active) as it should be. Yet the highlight color displayed remains gray as it were using the inactive palette at all times.
Suggestions?
Edit: there is one difference between these editors: the one that works, is layed out within a QWidget with a couple of smaller widgets below it. The one that looks inactive all the time is just a QPlainTextEdit (derived) object displayed as a tab in a tabset.
-
Hi,
You should also add which version of Qt you are using and your OS
-
I have verified that in the second QPlainTextEdit, the highlight for selected text is using the QBrush from the "Inactive" color group even after the widget has received the focus.
I am using the following code, which I regard as a kludge, to get around the problem. First, in the __init__() method of the class I put the following (this is Python with PyQt, but the translation to C++ should be obvious):
@ self.palette_active = QPalette(self.palette())
self.palette_inactive = QPalette(self.palette())
b = self.palette().brush(QPalette.Active,QPalette.Highlight)
self.palette_active.setBrush(QPalette.Inactive,QPalette.Highlight,b)
@This establishes two member variables, each a QPalette. palette_inactive is a copy of the default palette. palette_active has the same brush value for the Highlight role in both the Active and Inactive color groups.
Then in the focusInEvent() handler I placed the line,
@ self.setPalette(self.palette_active)@
And in the focusOutEvent() handler,
@ self.setPalette(self.palette_inactive)@
This causes correct behavior: selections are bright yellow when the focus is in the editor, and turn to mid-gray when focus leaves it.
I would still like to know why this is necessary. It should not be necessary.
-
Indeed it should not, can you reproduce that behavior with it's C++ equivalent ?
-
I have attempted to make a simple failing test case and cannot get it to fail. It is not simply the complex layout: edit#1 is part of a layout in a widget that is a tab in a tabwidget on the left of a splitter, and edit#2 is itself a tab in a tabwidget on the right of the splitter. I made a simple version of all that in like 20 lines of python and it works fine.
So there is something in the fine details of the setup. One possibility: the widget that contains edit#1 sets up an eventFilter on the editor, in order to trap certain keystrokes (*). Something doesn't smell right about this, mainly because the eventFilter doc is rather confusing to me. I think I am going to rip this up and redo it. That will be after a July holiday.
So thanks for your time, I am going to mark this [solved] and maybe in august I will start a new thread or file a bug.
DC
(*) Why an eventFilter? Because the whole layout was made with Designer and I couldn't see how to use a QPlainTextEdit subclass with a custom keyEventHandler, from Designer. I will probably toss the Designer layout and do it manually, that will eliminate the need for eventFilter-ing.
-
You would need to create that widget yourself, and then use it in Designer.
Indeed, if it looks suspicious a little analysis and refactor is a good idea.
Happy coding !