Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED] Text Edit: choose/change the whole background color
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved General and Desktop
7 Posts 2 Posters 6.7k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    Giova84
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • S Offline
      S Offline
      Seamus Boyle
      wrote on last edited by
      #2

      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()));
      @

      1 Reply Last reply
      0
      • G Offline
        G Offline
        Giova84
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        • G Offline
          G Offline
          Giova84
          wrote on last edited by
          #4

          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).

          1 Reply Last reply
          0
          • G Offline
            G Offline
            Giova84
            wrote on last edited by
            #5

            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..

            1 Reply Last reply
            0
            • S Offline
              S Offline
              Seamus Boyle
              wrote on last edited by
              #6

              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.

              1 Reply Last reply
              0
              • G Offline
                G Offline
                Giova84
                wrote on last edited by
                #7

                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.

                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved