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. QTableWidget - How to set background-color with widget
Forum Updated to NodeBB v4.3 + New Features

QTableWidget - How to set background-color with widget

Scheduled Pinned Locked Moved General and Desktop
12 Posts 2 Posters 13.8k Views 2 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.
  • F Offline
    F Offline
    fgdevel
    wrote on last edited by
    #1

    Hello,

    I try to set a background color to my QTableWidget (particular celle or row) with setBackgroundColor.
    This function works with QTableWidgetItem but crashes with others items like QCheckBox are contained in the grid

    Is there any other function to set the color in a cell/row ?

    I've already tried *setstylesheet *function, which crashes too...

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Can you share the code you use to setup the background color and widgets ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      F 1 Reply Last reply
      0
      • SGaistS SGaist

        Hi,

        Can you share the code you use to setup the background color and widgets ?

        F Offline
        F Offline
        fgdevel
        wrote on last edited by
        #3

        @SGaist

        yes of course :

        [...]
        tableItem = new QTableWidgetItem("");
        tableItem->setFlags(tableItem->flags() & ~(Qt::ItemIsEditable));
        //Add Table items here With Default Cell Texts
        ui->tableWidget->setItem(y,0,new QTableWidgetItem(""));
        ui->tableWidget->setItem(y,1,tableItem);
        ui->tableWidget->setItem(y,2,new QTableWidgetItem(""));
        ui->tableWidget->setCellWidget(y,3,new QCheckBox(""));
        ui->tableWidget->setCellWidget(y,4,new QCheckBox(""));
        ui->tableWidget->setCellWidget(y,5,new QCheckBox(""));
        [...]

        The background-color works only for "QTableWidgetItem" cells (without widget)

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          When you set a cell widget you don't see the item underneath (and you don't have any under), you have to set the background on the widget itself.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          F 1 Reply Last reply
          0
          • SGaistS SGaist

            When you set a cell widget you don't see the item underneath (and you don't have any under), you have to set the background on the widget itself.

            F Offline
            F Offline
            fgdevel
            wrote on last edited by fgdevel
            #5

            @SGaist

            is there any global function to set the background-color ? (which works with or without widget...) ?

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              You can try by modifying the palette of the application.

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              F 1 Reply Last reply
              0
              • SGaistS SGaist

                You can try by modifying the palette of the application.

                F Offline
                F Offline
                fgdevel
                wrote on last edited by
                #7

                @SGaist

                I want to modify a specific TableWidget and not all grids or all widget contained in the application...Have you an example ?

                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  You want to modify only that widget and all its children ?

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  F 1 Reply Last reply
                  0
                  • SGaistS SGaist

                    You want to modify only that widget and all its children ?

                    F Offline
                    F Offline
                    fgdevel
                    wrote on last edited by
                    #9

                    @SGaist

                    I want to modify all cells of QTableWidget, several cells contain QTableWidgetItem, several cells contain QCheckbox for example

                    1 Reply Last reply
                    0
                    • SGaistS Offline
                      SGaistS Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      Then you can write the style sheet so that it applies to both your QTableWidget and its children widgets

                      Interested in AI ? www.idiap.ch
                      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                      F 1 Reply Last reply
                      0
                      • SGaistS SGaist

                        Then you can write the style sheet so that it applies to both your QTableWidget and its children widgets

                        F Offline
                        F Offline
                        fgdevel
                        wrote on last edited by
                        #11

                        @SGaist

                        It seems to work on QTableWidget but not on children widgets when I set the style sheet

                        F 1 Reply Last reply
                        0
                        • F fgdevel

                          @SGaist

                          It seems to work on QTableWidget but not on children widgets when I set the style sheet

                          F Offline
                          F Offline
                          fgdevel
                          wrote on last edited by
                          #12

                          I have resolved my problem with :

                          ui->tableWidget->item(i,j)->setBackground(Qt::gray);

                          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