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. How to set background color of my QTextEdit?
Forum Updated to NodeBB v4.3 + New Features

How to set background color of my QTextEdit?

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 3 Posters 3.4k 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.
  • Q Offline
    Q Offline
    qwe3
    wrote on last edited by
    #1

    Hi,

    I have QTextEdit object. I would like to change his background color to color, which my MainWindow has ( it is gray ). How can I do that?

    I tried like this:

        QPalette palette2 = textEdit->palette();
        palette2.setColor(QPalette::Window,palette().color(QWidget::backgroundRole()));
        textEdit->setPalette(palette2);
    
    JonBJ 1 Reply Last reply
    0
    • Q qwe3

      Hi,

      I have QTextEdit object. I would like to change his background color to color, which my MainWindow has ( it is gray ). How can I do that?

      I tried like this:

          QPalette palette2 = textEdit->palette();
          palette2.setColor(QPalette::Window,palette().color(QWidget::backgroundRole()));
          textEdit->setPalette(palette2);
      
      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #2

      @qwe3
      I believe if you want to do it this way you have to set autoFillBackground.

      You don't have to do that if you use stylesheet instead.

      1 Reply Last reply
      0
      • mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Hi
        It has to be the right role. QWidget::backgroundRole() is not used it seems.

        Try

            QPalette p = ui->textEdit->palette();
            p.setColor(QPalette::Base, Qt::red); // BG
            p.setColor(QPalette::Text, Qt::white); // (plain) TEXT
            ui->textEdit->setPalette(p);
        

        alt text

        1 Reply Last reply
        2

        • Login

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