How to completely disable selection for qtableview (for all cells)? - c++

I want my table to be not selectable, so that only check boxes or radio buttons could be selected, but not the cell itself. Now I have:
How can I fix this?
Solutions for QTableWidget can help too.

QTableView *test = new QTableView();
test->setSelectionMode(QAbstractItemView::NoSelection);
gives the wanted result.

Related

Can't edit selected cell in QTableWidget Qt

I have following problem - if i select cell and then select empty space in tableWidget i can't again edit cell, i have to select another cell and again previous to be able edit it.
In vide i'm trying to edit second cell, but can't do this.
https://youtu.be/ibAFT1OkeHQ
I read about QTableWidget properties but did't find anything useful.
Found solution
QObject::connect(ui->tableWidgetData, &QTableWidget::clicked,
ui->tableWidgetData, QOverload<const QModelIndex&>::of(&QTableWidget::edit));
And on QTableWidget choose selection mode NoSelection.

How can I assign different selection modes?

I have default QTableView.
I want to get following selection behaviour:
If we selecting cells, selection will work like if we accepted SelectionMode::ContiguousSelection
If we selecting rows/column by clicking on QHeaderView section, selection will work like we accepted SelectionMode::ExtendedSelection, but deselect all cells, if any were selected.
I tried set SelectionMode to headers in QTableView constructor, but it doesn't work.
Question is how can I do it properly?
Okay, got it.
All I had to do is just make custom selection model and handle everything in there. But I had to change SelectionMode::ContiguousSelectionto SelectionMode::ExtendedSelection in my view to get meaningful indexes in my selection model.

interactivly resizable rows in QListWidget

In a QTableWidget I can configure the rows to be resizable by the user on run time by setting the verticalHeader's resizeMode to Interactive like this:
table.verticalHeader().setResizeMode(QtGui.QHeaderView.Interactive)
How would I configure a similar behavior for QListWidget? Unfortunately the QListWidget resizeMode does not have an Interactive item and I haven't found anything similar.
The best would be to configure it for the whole list but when it's possible for single rows/items that would be ok, too.
As doc said:
This view does not display horizontal or vertical headers; to display
a list of items with a horizontal header, use QTreeView instead.
So you should use QTreeView (or QTreeWidget) with one column and maybe with specific style.
Another approach. There are no header, so you can provide some instrument (dialog window, slider or something else) where user will be able to change row height, to change row height you should just use setData() and set QSize() to Qt::SizeHintRole. For example:
ui->listWidget->model()->setData(ui->listWidget->currentIndex(),
QSize(40,40),Qt::SizeHintRole);

QTableWidget - combobox delegate how do I allow different options per cell

Aloha
I have a QTableWidget with two columns that are currently using a ComboboxDelegate (my subclass of QItemDelegate) to present options to the user. I'd like the choice in the first column to effect the options available in the second, for the current row only.
E.g have a list of cars in the first column, and in the second a list of colours which are available for that car. Other rows to have different cars selected and thus different colour choices available.
From what I can see, I can only set an item delegate per row or column, so I can't see how to change the options in the second column's delegate without affecting all the other rows.
Is this possible? I'd really like to avoid going to a full view/model separation as I have quite a bit of code looking at this QTableWidget already (and I'm under time pressure)
Well for those interested; I went back to my pre-delegate approach, which was to use QTableWidget::setItemWidget() to provide a combobox widget for each cell.
I subclassed qcombobox to take a reference to the table, and connected the combobox CurrentIndexChanged with a slot to update the table data.
(setting a widget in a cell does not affect the tablewidget data unless you do this).
Using a full combobox like this is more expensive than an itemdelegate, but my tables are very small so I can get away with it. The rendering of the combobox is not as nice as the delegate (the combobox is visible all the time instead of only during editing in the delegate's case), but with time I'm sure I can improve on this.

QTableWidget change selection mode

When clicking on QTableWidget cell, it selects only the cell. How to configure tablewidget, so that when click on a cell , the whole row will be selected which contains the cell?
It can be done using signal,slots. I'm curious is there standard way doing it?
Simply use setSelectionBehavior
QTableView * tmp = new QTableView();
tmp->setSelectionBehavior(QAbstractItemView::SelectRows);
http://doc.qt.io/qt-5/qabstractitemview.html#SelectionBehavior-enum