jpo38
jpo38

Reputation: 21514

How to make a QTextEdit look disabled

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:

Upvotes: 2

Views: 1470

Answers (2)

king_nak
king_nak

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

p-a-o-l-o
p-a-o-l-o

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

Related Questions