Home > Back-end >  How to turn off selection cells by tab and shif tab in QTableWidget in slot?
How to turn off selection cells by tab and shif tab in QTableWidget in slot?

Time:12-13

I turn on editing some columns of QTableWidget by this

QObject::connect(ui->tableWidgetAdminEmployee, &QTableWidget::clicked,
                     ui->tableWidgetAdminEmployee, [=](const QModelIndex& index) {
if(index.column()!=0&&index.column()!=7&&index.column()!=8&&index.column()!=9)
   if (ui->tableWidgetAdminEmployee->item(index.row(), index.column())->flags() & Qt::ItemIsEnabled )
       ui->tableWidgetAdminEmployee->edit(index);
    });

But if i choose editable cell and then start editing it and press tab next cell will be selected and become editable even if it shouldn't

I turned off tabKeyNavigation in QTableWidget, but non-editable cells still can be selected by tab

I tryed this but didn't help

QObject::connect(ui->tableWidgetAdminEmployee, &QTableWidget::clicked,
                 ui->tableWidgetAdminEmployee, [=](const QModelIndex& index) {
if(index.column()!=0&&index.column()!=7&&index.column()!=8&&index.column()!=9)
{
    if (ui->tableWidgetAdminEmployee->item(index.row(), index.column())->flags() & Qt::ItemIsEnabled )
       ui->tableWidgetAdminEmployee->edit(index);
}
else
       ui->tableWidgetAdminEmployee->item(index.row(), index.column())->setFlags(Qt::ItemIsEnabled);
    });

CodePudding user response:

It seems you want to handle editing by yourself. This can be a valid approach.

However, you need to disable Qt's default editing capabilities. For your case, I guess turning off all editing triggers is the easiest way (cf edit triggers)

ui->tableWidgetAdminEmployee->setEditTriggers(QAbstractItemView::NoEditTriggers);

Thus Qt will in no way start editing by itself, and you have full control.

CodePudding user response:

Solved

        for(int row=0; row<ui->tableWidgetAdminEmployee->rowCount(); row  ){
        for(int col=0; col< ui->tableWidgetAdminEmployee->columnCount(); col  ){
            auto item=new QTableWidgetItem;
            if(col==0 || col==7 || col==8|| col==9)
                item->setFlags(item->flags() &  ~Qt::ItemIsEditable); //non-editable columns
            ui->tableWidgetAdminEmployee->setItem(row, col, item);
        }
    }
    
    ui->tableWidgetAdminEmployee->setSelectionMode(QAbstractItemView::NoSelection);
    ui->tableWidgetAdminEmployee->setEditTriggers(QAbstractItemView::NoEditTriggers);

    QObject::connect(ui->tableWidgetAdminEmployee, &QTableWidget::clicked,
                     ui->tableWidgetAdminEmployee, [=](const QModelIndex& index) {

        if (ui->tableWidgetAdminEmployee->item(index.row(), index.column())->flags() & Qt::ItemIsEditable )
            ui->tableWidgetAdminEmployee->edit(index);
    });
  •  Tags:  
  • c qt
  • Related