Reputation: 21514
At some point, I need to make a QTextEdit
look like it was disabled.
I'm not against calling setEnabled(false)
, but then the QTextEdit
does not receive QContextMenuEvent
event anymore (and I need a right-click context menu to be available....because that's how the QTextEdit
gets disabled by the user, so that's how I want him to enable it back).
So I do:
QColor mainWindowBgColor = palette().color(QPalette::Window);
// for the current widget
setStyleSheet(QString("background-color: %0").arg(mainWindowBgColor.name(QColor::HexRgb)));
This looks good, unless you right-click the widget and show it's context menu: The context menu appears but looks bad. Item highlighting does not work and then selected text is hardly visible (painted in white on a grey background).
How could I either:
setEnabled(false)
and have right-mouse click context menu be accessible setStyleSheet
but make sure it won't be usd by the context menu widget begin paintedUpvotes: 2
Views: 1470
Reputation: 11513
Style sheets are inherited by all sub-widgets. To make a style sheet apply only to a certain widget (or a type of widgets), you have to specify accessors.
E.g.
setStyleSheet(QString(
"QTextEdit { background-color: %0 }"
).arg(mainWindowBgColor.name(QColor::HexRgb)));
Upvotes: 1
Reputation: 10047
I would try subclassing QTextEdit
and overriding contextMenuEvent
. There I would show (exec) a standard menu, after changing its stylesheet:
#include <QTextEdit>
#include <QContextMenuEvent>
#include <QMenu>
class TextEdit : public QTextEdit
{
public:
TextEdit(QWidget* p) : QTextEdit(p){}
protected:
void contextMenuEvent(QContextMenuEvent * event)
{
QMenu * menu = createStandardContextMenu();
menu->setStyleSheet("background-color: gray");
menu->exec(QCursor::pos());
}
};
In the above example I set the menu background color to gray, but the goal of this example is to show one can override the menu style sheet, so to prevent the menu to use the one inherited from its parent.
Upvotes: 2