I have created a QTableView
and set a delegate which uses a slider to edit cells in column 2. I call openPersistentEditor
on all cells in column 2:
MinMaxSliderDelegate *minMaxSliderDelegate = new MinMaxSliderDelegate(this);
table = new QTableView();
table->setModel(new ServoConfiguration(this));
table->setItemDelegate(minMaxSliderDelegate);
for(int r=0; r<table->model()->rowCount(); r++) {
table->openPersistentEditor(table->model()->index(r,2));
}
So now I have a table with sliders permanently displayed in column 2.
The code I use to create the slider in the subclass of QStyledItemDelegate is:
QWidget *MinMaxSliderDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const {
if (index.column()==2) {
MinMaxSlider *editor = new MinMaxSlider(Qt::Horizontal, index.row(), parent);
editor->setAutoFillBackground(true);
editor->setFocusPolicy(Qt::StrongFocus);
editor->setMinimum(750);
editor->setMaximum(2000);
editor->setMinValue(1000);
editor->setMaxValue(1500);
connect(uiObj, SIGNAL(setMinToCurrentValue(int)), editor, SLOT(setMinValueToCurrentValue(int)));
connect(uiObj, SIGNAL(setMaxToCurrentValue()), editor, SLOT(setMaxValueToCurrentValue()));
return editor;
}
return QStyledItemDelegate::createEditor(parent, option, index);
My problem is that I need to know when the user is editing a value by using the slider (it should have focus). I find that if I tab onto the slider from the table cell to its left, the slider becomes the current item returned by table->selectionModel()->currentIndex();
However if I click on a slider, the current item is not changed. I also find that although I can tab into the slider from the cell to its left, I cannot tab out of the slider.
I am sure that I am missing something simple, but I would appreciate some help.
In case it matters, I set StrongFocus for the slider in its constructor, and the QModelView
flags for the column of sliders are: Qt::ItemIsSelectable | Qt::ItemIsEditable | Qt::ItemIsEnabled;
I'm sorry that I cant tell the exact codes to answer your question. I just have the idea that maybe it has to do with
QRect
.void QTableView::setSelection(const QRect & rect, QItemSelectionModel::SelectionFlags flags)
http://doc.qt.io/qt-4.8/qtableview.html#setSelection