Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How to set background color of my QTextEdit?



  • Hi,

    I have QTextEdit object. I would like to change his background color to color, which my MainWindow has ( it is gray ). How can I do that?

    I tried like this:

        QPalette palette2 = textEdit->palette();
        palette2.setColor(QPalette::Window,palette().color(QWidget::backgroundRole()));
        textEdit->setPalette(palette2);
    


  • @qwe3
    I believe if you want to do it this way you have to set autoFillBackground.

    You don't have to do that if you use stylesheet instead.


  • Lifetime Qt Champion

    Hi
    It has to be the right role. QWidget::backgroundRole() is not used it seems.

    Try

        QPalette p = ui->textEdit->palette();
        p.setColor(QPalette::Base, Qt::red); // BG
        p.setColor(QPalette::Text, Qt::white); // (plain) TEXT
        ui->textEdit->setPalette(p);
    

    alt text


Log in to reply