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 set background color segfault
Qt 6.11 is out! See what's new in the release blog

QTableWidget set background color segfault

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 4 Posters 2.1k Views 3 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
    cdwijs
    wrote on last edited by
    #1

    Hi All,

    I'm trying to make one row of a table red, to signal an invalid combination. I have tried the following, but this yields a segfault:
    QTableWidget *tableWidget;
    tableWidget = new QTableWidget(0,9,this);
    tableWidget->insertRow(row);
    tableWidget->setCellWidget(row,IDXSTART,new QDateTimeEdit);
    tableWidget->item(row,0)->setBackgroundColor(QColor(255,0,0,127)); //segfault
    qtablewidget.h
    inline void setBackgroundColor(const QColor &color)
    { setData(Qt::BackgroundColorRole, color); }

    How can I set the background color of a row of my table to red?
    Kind regards,
    Cedric

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

      @cdwijs said:

      row

      Are you sure this variable has a valid value?
      I think item(row,0) returns NULL and u then call NULL->setBackgroundColor
      which will crash.

      1 Reply Last reply
      0
      • Paul ColbyP Offline
        Paul ColbyP Offline
        Paul Colby
        wrote on last edited by
        #3

        I think you'll need a call to QTableWidget::setItem prior to the QTableWidget::itemAt call. Something like:

        tableWidget->setItem(row, 0, new QTableWidgetItem(tr("test")));
        ...
        QTableWidgetItem * const item = tableWidget->item(row,0);
        if (item) setBackgroundColor(QColor(255,0,0,127));
        

        pc.

        A 1 Reply Last reply
        1
        • Paul ColbyP Paul Colby

          I think you'll need a call to QTableWidget::setItem prior to the QTableWidget::itemAt call. Something like:

          tableWidget->setItem(row, 0, new QTableWidgetItem(tr("test")));
          ...
          QTableWidgetItem * const item = tableWidget->item(row,0);
          if (item) setBackgroundColor(QColor(255,0,0,127));
          

          pc.

          A Offline
          A Offline
          alex_malyu
          wrote on last edited by
          #4

          @Paul-Colby said:

          I think you'll need a call to QTableWidget::setItem prior to the QTableWidget::itemAt call

          As Paul said. You need to create an item first.
          I would suggest to always check your expectations:

          QTableWidgetItem *const item = tableWidget->item(0,0);
          Q_CHECK_PTR( item );
          item->setBackgroundColor(QColor(255,0,0,127));

          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