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> How to forbid only empty cells in a QTableWidget from selection (or to allow a certain CellWidget() to be selected)
Forum Updated to NodeBB v4.3 + New Features

<SOLVED> How to forbid only empty cells in a QTableWidget from selection (or to allow a certain CellWidget() to be selected)

Scheduled Pinned Locked Moved Unsolved General and Desktop
qtablewidgetselection
3 Posts 2 Posters 3.5k 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.
  • O Offline
    O Offline
    Olivier Ronat
    wrote on last edited by Olivier Ronat
    #1

    Hello
    I'm using a QTableWidget and I populate some cells with widgets with setCellWidget( row, column, &widget)..
    I want to be able to select only cells with widget but not empty cells.
    How to achieve it?

    1 Reply Last reply
    0
    • Chris KawaC Offline
      Chris KawaC Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Something like this should do the trick:

      for(int i = 0; i < table->rowCount(); ++i) {
          for(int j = 0; j < table->columnCount(); ++j) {
              auto item = table->item(i, j);
              if(!item) { // make sure there's an item in that cell
                  item= new QTableWidgetItem();
                  table->setItem(i, j, item);
              }
              if(!table->cellWidget(i,j))
                  item->setFlags(item->flags() & ~Qt::ItemIsSelectable);
          }
      }
      
      1 Reply Last reply
      0
      • O Offline
        O Offline
        Olivier Ronat
        wrote on last edited by
        #3

        Thank you
        Note that I succeed in first buiding my QTableWidget with each item set by item->setFlags(Qt::NoItemFlags) and then when I want to insert a wiget proceed with delete takeItem(row,column) before the setCellWidget(row, column, &widget) to insert a widget and retrieve the normal behavior

        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