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

  • 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?

  • Moderators

    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);
                item->setFlags(item->flags() & ~Qt::ItemIsSelectable);

  • 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

Log in to reply