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. QTextEdit - text color won't change
Forum Updated to NodeBB v4.3 + New Features

QTextEdit - text color won't change

Scheduled Pinned Locked Moved Solved General and Desktop
2 Posts 1 Posters 299 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.
  • C Offline
    C Offline
    CodeCreate
    wrote on last edited by CodeCreate
    #1

    Hello everyone,

    Usually I am pretty good at debugging issues, but this issue with the QTextEdit text color not changing has me really confused. I feel like I must be missing something small.

    Basically I have inherited a QTextEdit for a custom class in order to log my compile information. When inserting the the logs into the QTextEdit I change the text color to display the current time in a different color than what the log information will be. But, no matter what I do the text color does not change. I also ran the debugger and it also seems that the text doesn't even get added until later after the insertPlainText function is called. So, this had me thinking maybe it was having issues since I also have 2 custom QTextEdits that insert text with syntax highlighting, but I disabled that and the text color still won't change when inserting the log text.

    Here is the .h for my custom QTextEdit class:

    #ifndef OUTPUTTEXTEDIT_H
    #define OUTPUTTEXTEDIT_H
    
    #include <QTextEdit>
    #include <QColor>
    #include <QTime>
    
    class OutputTextEdit : public QTextEdit
    {
        Q_OBJECT
    
        QColor m_timeColor;
        QColor m_textColor;
    
    public:
        OutputTextEdit(QWidget* parent = nullptr);
        ~OutputTextEdit();
    
        void insertCompileLog(const QString& text);
    
        void setTimeColor(const QColor color) { m_timeColor = color; }
        void setTextColor(const QColor color) { m_textColor = color; }
    
    private:
        void setup();
    };
    
    #endif // OUTPUTTEXTEDIT_H
    

    Here is the .cpp for my custom QTextEdit class:

    #include "OutputTextEdit.h"
    
    OutputTextEdit::OutputTextEdit(QWidget* parent) :
        QTextEdit(parent),
        m_timeColor(QColor(16, 144, 144)),
        m_textColor(QColor(234, 221, 158))
    {
        setup();
    }
    
    OutputTextEdit::~OutputTextEdit()
    {
    
    }
    
    void OutputTextEdit::insertCompileLog(const QString &text)
    {
        // Ensures the cursor is at the end before inserting text
        QTextCursor myCursor = this->textCursor();
        myCursor.movePosition(QTextCursor::End);
        this->setTextCursor(myCursor);
    
        // Add the time that the shader compiled
        this->setTextColor(m_timeColor);
        this->insertPlainText(QTime::currentTime().toString() + "\n");
    
        // Insert the log text
        this->setTextColor(m_textColor);
        this->insertPlainText(text);
    }
    
    void OutputTextEdit::setup()
    {
        this->setReadOnly(true);
        this->setWordWrapMode(QTextOption::NoWrap);
        //this->setStyleSheet("color:red;");
    }
    

    Screenshot of my text:
    debug1.png

    Has anyone ever experienced an issue like this before?

    Thank you for the help!

    1 Reply Last reply
    0
    • C Offline
      C Offline
      CodeCreate
      wrote on last edited by
      #2

      Well, couldn't figure out why text color wasn't changing, so I just decided to use my custom syntax highlighter to deal with changing the text color and it works quite well.

      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