[SOLVED] Text Edit: choose/change the whole background color



  • hello everyone, I'm new here.
    I write here to find some help about qt coding, in this case about Text Edit demo ( ttp//qt-project.org/doc/qt-4.8/demos-textedit.html ).
    To better learning qt coding (i am a newbie about developing/coding), i am adding features/improvements on Text Edit: for now i have added the ability to choose the color of background text, and now i'd like add the ability to choose the background color of the whole sheet/document.

    This is the part of code about color picker/selector:

    @ pix.fill(Qt::white);
    actionBackgroundColor = new QAction(QIcon::fromTheme("bg-color", QIcon(rsrcPath + "/colorpicker.png")),
    tr("&Background Color..."), this);
    connect(actionBackgroundColor, SIGNAL(triggered()), this, SLOT(backgroundColor()));
    tb->addAction(actionBackgroundColor);
    menu->addAction(actionBackgroundColor);@

    And the following is the part of code which should insert the html tag about bg color:

    @void TextEdit::backgroundColor()
    {
    QColor color = QColorDialog::getColor(Qt::white, this);
    if (color.isValid())
    textEdit->insertHtml("<body bgcolor="" + fn + """);
    }
    @

    When i compile Text Edit with this code, a new color selector is added to toolbar, but choosing the color doesn't affects the document. Could someone tell me where the code is wrong and how can I fix it?

    Thank you very much.



  • Your slot, doesn't use the colour in the string passed to insertHtml(), and the body tag isn't closed.
    @
    // replace
    textEdit->insertHtml("<body bgcolor="" + fn + """);
    // with
    textEdit->insertHtml(QString("<body bgcolor='%1'>").arg(color.name()));
    @



  • Hi Seamus, thank you for the reply.

    I have tried to change the code as you suggest, but the result is the same as before: doesn't affect the document and no html code is inserted.

    In anyway i have found this:

    @textEdit->setStyleSheet("background-color:yellow ");@

    If i insert this line, and then i choose any bgcolor, the sheet color will change to yellow (obviously; 'cause the color is fixed) but this will not apply any html code: the color is only present inside Text Edit sheet.



  • UPDATE:

    if i insert:

    @textEdit->setStyleSheet(QString("background-color:'%1' ").arg(color.name()));@

    I can change sheet color, but no html code is applied (so the color is temporary: when i save the document (html or pdf) the color is lost).



  • Any idea? seems that tags "<body bgcolor=" and "<body style=" can't be inserted as html.. And in fact, if i try to replace them with another tag (eg "img src=" ) this different tag will be correctly inserted as html..



  • Instead of using QTextEdit::insertHtml(), you have to modify the existing body tag, if one exists. The modification needed would depend on the document loaded. If it already contains CSS that affects the background color you would have to modify its value, if it doesn't you could use the body tags bgcolor attribute, but CSS has higher priority. The example.html file shipped with the example project contains a html style attribute in the body tag, but doesn't contain anything that affects the background, so for learning purposes its probably easier to only add or modify the body bgcolor attribute, ignoring css, as it would be easier to parse and update the value.

    I'm not sure the best way to update the existing body tag, without losing undo/redo history in the editor.



  • With the precious help of a friend developer, i have solved :-)
    This is the code which can insert the html tag about bgcolor:

    @void TextEdit::backgroundColor()
    {
    QColor color = QColorDialog::getColor(Qt::white, this);
    if (color.isValid())
    {
    qDebug() << color.name(); // colorChanged(color.name());
    }
    else
    {
    return;
    }
    // qDebug() << textEdit->toHtml();

    QString str = textEdit->toHtml();
    
    //First get rid of any old background color markup:
    str.replace(QString("bgcolor="), QString(""));
    
    //Now set the new background color:
    str.replace(QString("&lt;body"), QString("&lt;body bgcolor=\"" + color.name() + "\""));
    

    // qDebug() << str;

    textEdit->setHtml(str);
    

    // actionSave->setEnabled(true);
    textEdit->document()->setModified(true);
    }@

    Best regards.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.