QLabel: set color of text and background

后端 未结 6 1642
清歌不尽
清歌不尽 2020-11-30 20:03

How do I set color of text and background of a QLabel ?

6条回答
  •  爱一瞬间的悲伤
    2020-11-30 20:19

    The ONLY thing that worked for me was html.

    And I found it to be the far easier to do than any of the programmatic approaches.

    The following code changes the text color based on a parameter passed by a caller.

    enum {msg_info, msg_notify, msg_alert};
    :
    :
    void bits::sendMessage(QString& line, int level)
    {
        QTextCursor cursor = ui->messages->textCursor();
        QString alertHtml  = "";
        QString notifyHtml = "";
        QString infoHtml   = "";
        QString endHtml    = "
    "; switch(level) { case msg_alert: line = alertHtml % line; break; case msg_notify: line = notifyHtml % line; break; case msg_info: line = infoHtml % line; break; default: line = infoHtml % line; break; } line = line % endHtml; ui->messages->insertHtml(line); cursor.movePosition(QTextCursor::End); ui->messages->setTextCursor(cursor); }

提交回复
热议问题