Remove CellWidget from QTableWidget when focus out
-
I have a QTableWidget in which some cells have QComboBox as cellWidget. I want to create the comboBox only when the user clicks in the cell. For this, I have caught itemClicked signal and created the comboBox and set it as cellWidget.
Now, I want to delete this comboBox in two scenarios - if the user selects some value from the comboBox, or the user simply focusses out (click anywhere in the dialog).
For the first case, I have use the signal QComboBox::activated which is invoked when something is selected in the drop-down. There I delete the comboBox and it works fine.
However, I am not sure how to proceed for the second case (delete the comboBox when the user focusses out of the drop-down without selecting anything).
I tried capturing eventFilter for focusOut for the tablewidget as well as the comboBox, but it didn't help.
-
@shobhitmittal
Hello and welcome.It sounds like you are trying to re-invent the editing capabilities already built into
QTableWidget
/QTableView
, without any need to callsetCellWidget()
which is "frowned upon". The inbuilt facilities are capable of showing and removing aQComboBox
only when editing. Have you looked at/understood that, before you start doing your own stuff here? -
@JonB As far as I understand, if I need to show a comboBox in a tableWidget cell, I need to use setCellWidget with correct row, column and the widget. As soon as I do so, the comboBox is visible in the cell. Though the comboBox is not pop-down, but the notch (down arrow) is visible which makes one understand that there is a comboBox box and I want to avoid it. I want to show the comboBox only when I click in that cell and not before or after that.
As mentioned in my question, showing is straightforward. Even removing the widget is fine when something is selected from the comboBox. But, if nothing is selected, then how to remove the widget, that' my question.
-
@shobhitmittal said in Remove CellWidget from QTableWidget when focus out:
As far as I understand, if I need to show a comboBox in a tableWidget cell, I need to use setCellWidget
No, you do not need to do this (via
setCellWidget()
).Search the
QTableWidget
docs page, and that ofQTableView
from which it inherits, foredit
. Qt has the "framework" for going in & out of edit mode on a cell without you having to to ever callsetCellWidget
. I believe that by doing that you will find that if the user clicks away from the combobox editor (which appears when editing starts) the framework will get rid of that comboxbox for you.Actually the editor facilities are shown under QAbstractItemView Class, from which
QTreeView
(and thereforeQTableWidget
too) inherits. You will use code includingtableWidget->setEditTriggers(QAbstractItemView::AllEditTriggers); item->setFlags(item->flags() | Qt::ItemIsEditable);
Those lines make an item editable by double-clicking (I think) on a cell.
Ultimately you should use void QAbstractItemView::setItemDelegate(QAbstractItemDelegate *delegate). You should subclass your own
QAbstractItemDelegate
. Override its QWidget *QAbstractItemDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const. Have that return anew QComboBox
for your case. Then the Qt infrastructure will be responsible for showing and destroying the combobox for you.Qt's Star Delegate Example provides editablity and shows what you need to do. There may be other examples. Ultimately you will be creating a
QStyledItemDelegate
and overriding its "editor" methods to return a combobox to use and populating that combo's items.