【问题标题】:Qt QPlainTextEdit backgroundQt QPlainTextEdit 背景
【发布时间】:2009-10-06 23:36:18
【问题描述】:

我想更改QPlainTextEdit 的背景颜色,我该怎么做?

【问题讨论】:

    标签: c++ qt


    【解决方案1】:

    修改纯文本编辑的调色板。示例程序:

    #include <QApplication>
    #include <QPlainTextEdit>
    
    int main(int argc, char* argv[])
    {
      QApplication app(argc, argv);
    
      QPlainTextEdit edit;
      QPalette p = edit.palette();
    
      p.setColor(QPalette::Active, QPalette::Base, Qt::red);
      p.setColor(QPalette::Inactive, QPalette::Base, Qt::red);
    
      edit.setPalette(p);
    
      edit.show();
      return app.exec();
    }
    

    当然可以替换任何你想要的颜色。

    【讨论】:

    • 请注意,使用此方法,将样式表应用于父级或控件本身将禁用此调色板。刚才有一些有趣的故障排除:)
    • 很高兴知道,谢谢!我还没有使用样式表,所以感谢您的提前通知。
    【解决方案2】:

    如果 QPlainTextEdit 支持样式表,你可以这样做:

    myPlainTextEdit->setStyleSheet("background-color: yellow");
    

    qApp->setStyleSheet("QPlainTextEdit {background-color: yellow}");
    

    【讨论】:

    • 请注意,这也会影响滚动条颜色,这可能不是您想要的。
    【解决方案3】:

    他们称之为角色而不是颜色/颜色,有点令人困惑。

    https://doc.qt.io/qt-5/qwidget.html#setBackgroundRole

    提示 - 如果您找不到特定控件的功能,请单击显示继承的成员 - 大多数常规设置都在 qWidget 中,这是在屏幕上绘制的一切的基础。

    【讨论】:

    【解决方案4】:

    您可能需要致电QPlainTextEdit::setBackgroundVisible(true)

    【讨论】:

      【解决方案5】:

      为了修改背景,您需要修改 QPlainTextEdit 的palette 并设置背景可见:

      myPlainTextEdit->setPalette(QPalette(/*Select the constructor you need*/));
      myPlainTextEdit->setBackgroundVisible(true);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-06-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-04-01
        • 2010-10-01
        相关资源
        最近更新 更多