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. [solved] Trouble getting color from QTableWidgetItem

[solved] Trouble getting color from QTableWidgetItem

Scheduled Pinned Locked Moved General and Desktop
3 Posts 1 Posters 952 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 trying to do things like change the background color of a QTableWidgetItem when I click on it.

    @ QTableWidgetItem *cell = ui->tableWidget->itemAt( index.column(), index.row() );
    QColor color = cell->background().color(); //test code
    int r=40; int g=60; int b=80; int a=0;
    color.getRgb( &r, &g, &b, &a ); //OR
    r=color.red(); g=color.green(); b=color.blue();
    qDebug() << "r=" << r << " g=" << g << " b=" << b;@

    Both getRgb and color.red() etc. are returning 0,0,0. Some of the cells are gray, some of them are white. Why am I not getting the correct color?
    Thanks
    Ron

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

      Apparently I am not getting the cell correctly because cell->setText("abc") does not work.

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

        I figured it out. QTableWidgetItem::itemAt "returns the item at the position equivalent to QPoint(ax, ay) in the table widget's coordinate system" not column and row in the table. I changed itemAt() to item() and reversed row/column. Here is what works:

        @QTableWidgetItem *cell = ui->tableWidget->item( index.row(), index.column() );
        QColor color = cell->background().color();
        int r=0; int g=0; int b=0; int a=0;
        color.getRgb( &r, &g, &b, &a );
        qDebug() << "r=" << r << " g=" << g << " b=" << b;
        qDebug() << "checked=" << cell->checkState(); //I made some of the cells Qt::ItemIsUserCheckable@

        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