我注意到,如果在 HTML 中我们使用 type bgcolor="#ffd814",那么在 textedit 中背景颜色将更改为颜色。如何从 QAction 和 QColorDialog 获得相同的结果?
我用过这个
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);
}
但它不会改变 HTML,所以在关闭和打开文件后背景是白色的。
备注:在
中使用bgcolor="#ffd814"不会改变textedit中的滚动条颜色。如果我们使用 QColor::name 它将以 QString 格式返回颜色名称 (#ffd814) 我们如何将其放入 html 中。例如
void MainWindow::on_actionText_Color_triggered()
{
QColor color = QColorDialog::getColor(Qt::white,this);
if(color.isValid())
ui->textEdit->setTextColor(color);
}
这会改变所选文本的颜色并将其保存在 html 中,因此我们在 textedit 中所做的更改会被记住。
最佳答案
使用相关对象创建您的 QPalette。
QPalette palette = ui->textEdit->pallette();
这是一个工作示例;
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
关于html - 如何设置Qtextedit背景颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31554179/