How to set QTableWidget editable with a few non-editable columns, and editing by one click on cell?
-
I have following solution everything works right, but when i click on enabled cell in console appears line "edit: editing failed"
QObject::connect(ui->tableWidgetAdminEmployee, &QTableWidget::clicked, ui->tableWidgetAdminEmployee, QOverload<const QModelIndex&>::of(&QTableWidget::edit)); for(int i = 0; i < ui->tableWidgetAdminEmployee->rowCount(); i++) { ui->tableWidgetAdminEmployee->item(i,0)->setFlags(Qt::ItemIsSelectable|Qt::ItemIsEnabled ); ui->tableWidgetAdminEmployee->item(i,7)->setFlags(Qt::ItemIsSelectable|Qt::ItemIsEnabled ); ui->tableWidgetAdminEmployee->item(i,8)->setFlags(Qt::ItemIsSelectable|Qt::ItemIsEnabled ); ui->tableWidgetAdminEmployee->item(i,9)->setFlags(Qt::ItemIsSelectable|Qt::ItemIsEnabled ); }
-
I have following solution everything works right, but when i click on enabled cell in console appears line "edit: editing failed"
QObject::connect(ui->tableWidgetAdminEmployee, &QTableWidget::clicked, ui->tableWidgetAdminEmployee, QOverload<const QModelIndex&>::of(&QTableWidget::edit)); for(int i = 0; i < ui->tableWidgetAdminEmployee->rowCount(); i++) { ui->tableWidgetAdminEmployee->item(i,0)->setFlags(Qt::ItemIsSelectable|Qt::ItemIsEnabled ); ui->tableWidgetAdminEmployee->item(i,7)->setFlags(Qt::ItemIsSelectable|Qt::ItemIsEnabled ); ui->tableWidgetAdminEmployee->item(i,8)->setFlags(Qt::ItemIsSelectable|Qt::ItemIsEnabled ); ui->tableWidgetAdminEmployee->item(i,9)->setFlags(Qt::ItemIsSelectable|Qt::ItemIsEnabled ); }
@Kuji
The only thing which strikes me is void QAbstractItemView::edit(const QModelIndex &index):To provide consistent navigation behavior, call
setCurrentIndex()
before this function with the same model index.Not saying that is your issue, but does it make any difference if you do this in the slot? Change to a lambda for the slot which calls
setCurrentIndex()
beforeedit()
, this will also allow you to see whether the problem lies in the connection ofclicked()
signal to slot or in the call toedit()
from the slot. -
@Kuji
The only thing which strikes me is void QAbstractItemView::edit(const QModelIndex &index):To provide consistent navigation behavior, call
setCurrentIndex()
before this function with the same model index.Not saying that is your issue, but does it make any difference if you do this in the slot? Change to a lambda for the slot which calls
setCurrentIndex()
beforeedit()
, this will also allow you to see whether the problem lies in the connection ofclicked()
signal to slot or in the call toedit()
from the slot. -
@JonB Sorry i don't understand can u explain about where i should do this: Change to a lambda for the slot which calls setCurrentIndex() before edit()?
QObject::connect(ui->tableWidgetAdminEmployee, &QTableWidget::clicked, ui->tableWidgetAdminEmployee, [=](const QModelIndex& index) { qDebug() << "In slot"; ui->tableWidgetAdminEmployee->setCurrentIndex(index); qDebug() << "Calling edit()"; ui->tableWidgetAdminEmployee->edit(index); qDebug() << "Called edit()"; });
Something like that (untested). I don't suppose doing
setCurrentIndex(index)
will solve your problem, but it's worth a try. -
QObject::connect(ui->tableWidgetAdminEmployee, &QTableWidget::clicked, ui->tableWidgetAdminEmployee, [=](const QModelIndex& index) { qDebug() << "In slot"; ui->tableWidgetAdminEmployee->setCurrentIndex(index); qDebug() << "Calling edit()"; ui->tableWidgetAdminEmployee->edit(index); qDebug() << "Called edit()"; });
Something like that (untested). I don't suppose doing
setCurrentIndex(index)
will solve your problem, but it's worth a try. -
@JonB Added, when i choose cell which is non-editable console writes:
In slot
Calling edit()
edit: editing failed
Called edit()When i choose editable cells console writes:
In slot
Calling edit()
Called edit()@Kuji said in How to set QTableWidget editable with a few non-editable columns, and editing by one click on cell?:
when i choose cell which is non-editable console writes:
What do you expect otherwise? I mean - it just tells you that a non-editable item can not be edited. So either life with it or don't call edit() on such an item.
-
@JonB Added, when i choose cell which is non-editable console writes:
In slot
Calling edit()
edit: editing failed
Called edit()When i choose editable cells console writes:
In slot
Calling edit()
Called edit()@Kuji said in How to set QTableWidget editable with a few non-editable columns, and editing by one click on cell?:
@JonB Added, when i choose cell which is non-editable console writes:
I did not realize you were doing this on the non-editable cells! So it is behaving well/as it should, what is the issue? Earlier you wrote:
but when i click on enabled cell in console appears line "edit: editing failed"
Now you show that works fine on editable cells.
-
How can i get rid of lines in console:edit: editing failed? By don't calling edit() for these cells then how exclude these cells?
-
if (ui->tableWidgetAdminEmployee->itemFromIndex(index)->flags() & Qt::ItemIsEnabled ) ui->tableWidgetAdminEmployee->edit(index);
Something like that.
-
@JonB Can't write instead of this code line because of en error, or i should write this in another place?
ui->tableWidgetAdminEmployee->edit(index);
@Kuji said in How to set QTableWidget editable with a few non-editable columns, and editing by one click on cell?:
Can't write instead of this code line because of en error,
Then fixing the error would be a good start.
-
@Kuji said in How to set QTableWidget editable with a few non-editable columns, and editing by one click on cell?:
Can't write instead of this code line because of en error,
Then fixing the error would be a good start.
@Christian-Ehrlicher i mean this error
error: 'itemFromIndex' is a protected member of 'QTableWidget'
here
QObject::connect(ui->tableWidgetAdminEmployee, &QTableWidget::clicked, ui->tableWidgetAdminEmployee, [=](const QModelIndex& index) { ui->tableWidgetAdminEmployee->setCurrentIndex(index); if (ui->tableWidgetAdminEmployee->itemFromIndex(index)->flags() & Qt::ItemIsEnabled ) ui->tableWidgetAdminEmployee->edit(index); });
-
@mpergand This worked
QObject::connect(ui->tableWidgetAdminEmployee, &QTableWidget::clicked, ui->tableWidgetAdminEmployee, [=](const QModelIndex& index) { ui->tableWidgetAdminEmployee->setCurrentIndex(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 now after making some columns editable i can't make them non-editable
-
@mpergand This worked
QObject::connect(ui->tableWidgetAdminEmployee, &QTableWidget::clicked, ui->tableWidgetAdminEmployee, [=](const QModelIndex& index) { ui->tableWidgetAdminEmployee->setCurrentIndex(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 now after making some columns editable i can't make them non-editable
-
@Kuji said in How to set QTableWidget editable with a few non-editable columns, and editing by one click on cell?:
But now after making some columns editable i can't make them non-editable
Change the flag of the item accordingly.
@mpergand I tryed this, but after clicking on the cell it's editable and then it becomes unselectable
QObject::connect(ui->tableWidgetAdminEmployee, &QTableWidget::clicked, ui->tableWidgetAdminEmployee, [=](const QModelIndex& index) { ui->tableWidgetAdminEmployee->setCurrentIndex(index); ui->tableWidgetAdminEmployee->item(index.row(), index.column())->setFlags(Qt::ItemIsSelectable); });
-
@mpergand I tryed this, but after clicking on the cell it's editable and then it becomes unselectable
QObject::connect(ui->tableWidgetAdminEmployee, &QTableWidget::clicked, ui->tableWidgetAdminEmployee, [=](const QModelIndex& index) { ui->tableWidgetAdminEmployee->setCurrentIndex(index); ui->tableWidgetAdminEmployee->item(index.row(), index.column())->setFlags(Qt::ItemIsSelectable); });
-
@mpergand cells are editable when i click on them and after that they are non-editable but write "edit: editing failed" in console