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. Suddenly getting crash when setting background color of QTableWidget
Forum Updated to NodeBB v4.3 + New Features

Suddenly getting crash when setting background color of QTableWidget

Scheduled Pinned Locked Moved General and Desktop
5 Posts 3 Posters 2.3k 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.
  • B Offline
    B Offline
    BasicPoke
    wrote on last edited by
    #1

    Hello, I am setting up a QTableWidget in the constructor of my main window as follows:
    @ ui->tableWidget->setRowCount(5);
    ui->tableWidget->setColumnCount(8);
    //QStringList *labels = new QStringList();
    QStringList labels;
    labels.append("Intake Pressure");
    labels.append("Intake Temperature");
    labels.append("Motor Temperature");
    labels.append("X Vibration");
    labels.append("Y Vibration");
    ui->tableWidget->setVerticalHeaderLabels(labels);@

    I get a crash below when I try to set the background color of some of the cells. This was working, then I was making changes unrelated to this widget, and it suddenly started crashing. I first noticed it crashing when I replaced the 0's in the second column with a variable. Then when I change them all back to 0, it still crashes.
    @//Set color of the cells to red
    QColor color( 240,0,0 );
    ui->tableWidget->item( 0, 0 )->setBackgroundColor( color ); //Crashing here
    ui->tableWidget->item( 1, 0 )->setBackgroundColor( color );
    ui->tableWidget->item( 2, 0 )->setBackgroundColor( color );
    ui->tableWidget->item( 3, 0 )->setBackgroundColor( color );
    ui->tableWidget->item( 4, 0 )->setBackgroundColor( color );@

    At the crash, the cursor is on the following line in qtablewidget.h:
    @ inline QColor backgroundColor() const
    { return qvariant_cast<QColor>(data(Qt::BackgroundColorRole)); }
    inline void setBackgroundColor(const QColor &color)
    { setData(Qt::BackgroundColorRole, color); } //Cursor is here@

    The crash window has a title bar "Signal received" and the content is "The inferior stopped because it received a signal from the Operating System. Signal name: SIGSEGV, Signal meaning: Segmentation fault" Any ideas what is going on?

    Thanks a lot,
    Ron

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

      Hi,

      You are not creating any item for your table widget so you are accessing a null pointer.

      You first need to populate your QTableWidget with QTableWidgetItems

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

      1 Reply Last reply
      0
      • B Offline
        B Offline
        BasicPoke
        wrote on last edited by
        #3

        You are right of course. What is different now is that no items get put in the table before I try to set the color. What I need to do is detect whether there are items there before I set the color.
        ...
        What I did was fill the table with '-' items as soon as the table is created.
        Thanks for the help.

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

          You can also set items with empty text if you don't want anything in the cells

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

          1 Reply Last reply
          0
          • B Offline
            B Offline
            Bharath Ram
            wrote on last edited by
            #5

            Thanks, this helped!!!

            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