How can I assign different selection modes? - c++

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.

Related

Change label of QComboxBox without using lineEdit

Currently I want to implement a widget to allow user to enable/disbale all provided options of a QSet. So i took a combobox and added selectable items. So far so good, but how I can change text displayed in combobox? Currently all my items have just ItemIsUserCheckable and ItemIsEnabled as enabled flags (ItemIsSelectable is not enabled), so text of ComboBox is always text of first item. Instead I want as text "Flag1, Flag 3, Flag6" if there multiple flags and user enabled Flag 1, 3 and 6. But setCurrentText and setEditText requiring setEditable(true) or an custom lineEdit. But using an lineEdit is changing appearance. So is there another way?
I had a similar problem a while back, I ended up 'solving' it by adding an extra item to my model that was first in the list and always set as the current item. I then updated it's text to say 'Select items...' or 'X item(s) selected' as appropriate.

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);

QTableView re-focus the view to specific column

I have a QTableView with 80+ columns displayed in it. The subclass i have created for QTableView allows has functionality for the standard table stuff i wanted e.g order by columns move columns and rows hide column and rows etc.
However the problem i have is the view's focus. lets say you have all the data in the table, you scroll all the way to the right so you are looking at columns 70-80 (assuming 10 columns fit on the screen at once) if i click the header of row 80 (to order by column 80) the table reorders (as expected) however it also jumps the view to the last focused cell (which as far as i can tell is the cell that was last clicked)
What i want to do is not necessarily refocus on the column that was clicked because this might still change the view what im looking for is to just keep the view's focus exactly as it is instead of having it jump back to where the last clicked cell was?
Is there some flag im missing for focus policy or will i need to get the current view and set the view back to that view after mouse clicks that reorder the table and if so how could this be done.
Im aware i have provided no code for this question but it doesnt seem there is any needed since its not a bug im looking to fix more of a feature im unaware of, if you want to see any just comment
EDIT:
Im using a QSortFilterPorxyModel, this seems to get set once (when i call this->setSortingEnabled(true); after the initial call to this on contruction my table model never calls sort again. I have a slot linked to the header clicked signal, and i set the scroll bars to scrollTo() to the index clicked, but i think the sort is happening after this so it does nothing, any idea of what signal are emitted after sorting so i can catch them and then set the view back maybe?
thanks
Before you do your sort, store the current values for where the scroll bars are with
int vPos = yourQTableView->verticalScrollBar()->sliderPosition();
int hPos = yourQTableView->horizontalScrollBar()->sliderPosition();
then after the sort, set it back
yourQTableView->verticalScrollBar()->setSliderPosition(vPos);
yourQTableView->horizontalScrollBar()->setSliderPosition(hPos);
The signals you are looking for are signals of QSortFilterProxyModel inherited from QAbstractItemModel:
layoutAboutToBeChanged()
layoutChanged()
I couldn't reproduce your symptoms by creating a QTableWidget, filling it up with random stuff, and then sorting a particular column. The selected cell stays selected, but does not become visible if it has scrolled off screen.
So the question is, what is causing the behavior you're seeing. It sounds as though scrollTo() is being called by some other function. Since that's a virtual function, I would override it with a pass-through function and see when it's getting called.

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.

GtkTreeView Column Header Click Event

I have a question. I have a GtkListStore and a GtkTreeView, and I want to sort the GtkListStore and update the result to the GtkTreeView when the user clicks on a certain column of the GtkTreeView. I am assuming that the columns are clickable, and cannot be re-ordered, so the numerical order of the columns can be used to set the sorting column's index. But I cannot seem to find which signal gets emitted when the user clicks on the header of a particular column. I have gone through the GTKMM documentation time and again, but it does not seem to be mentioned!
Use Gtk::TreeView::get_column(<column-no>) to get a particular column and attach to its "clicked" signal using Gtk::TreeViewColumn::signal_clicked():
Gtk::TreeViewColumn* col = myview.get_column(SOME_COLUMN_NUMBER);
col->signal_clicked().connect(sigc::mem_fun(*this,&some_method));
That's not how you're supposed to do it.
There can be several views hooked up to the same model; sorting is not something you do to the model, it's something you do to the view.
See the GtkTreeSortable interface (and its GtkTreeModelSort implementation).