How to set Qtextedit background color?

I noticed that if in HTML we use type bgcolor="#ffd814" then in textedit the background color will change to the color. How can I get same result from QAction and QColorDialog?

I used this

void MainWindow::on_actionBackground_Color_triggered()
{    
    QColor color = QColorDialog::getColor(Qt::white,this);
    QPalette palette;
    palette.setColor(QPalette::Base,color);
    if(color.isValid())
        ui->textEdit->setPalette(palette);
}

But it does not change HTML, so after closing and opening the file background is white.

Remark: using bgcolor="#ffd814" in < body bgcolor="#ffd814" > does not change the scrollbar color in textedit.

If we use QColor::name it will return in QString format color name (#ffd814) how can we put it in a html. for example

void MainWindow::on_actionText_Color_triggered()
{
    QColor color = QColorDialog::getColor(Qt::white,this);
    if(color.isValid())
        ui->textEdit->setTextColor(color);
}

this changes color of selected text and saves it in html, so changes we made in textedit are remembered.


Create your QPalette with related object.

QPalette palette = ui->textEdit->pallette();

Here is a working example;

QColor color = QColorDialog::getColor(Qt::white,this); // in here your color pallete will open..

QPalette p = ui->textEdit->palette(); // define pallete for textEdit.. 
p.setColor(QPalette::Base, Qt::red); // set color "Red" for textedit base
p.setColor(QPalette::Text, color); // set text color which is selected from color pallete
ui->textEdit->setPalette(p); // change textedit palette
链接地址: http://www.djcxy.com/p/15370.html

上一篇: 使用getElementById()和bgcolor属性

下一篇: 如何设置Qtextedit背景颜色?