Edit QTableView with combo box in column - c++

I created my own TableView and implemented model for it, etc. Finally, I started to edit values : it went well with flags and setData functions reimplemented, but I can only edit string/int values. I need to add possibility to select from combo box there. I saw topics like this one, but QTableView doesn't have a setCellWidget method.
Is there any possibility to implement it with QTableView or do I have to switch to 'TableWidget' and re-do my work (which would be problematic)?

QTableView has a method setIndexWidget() to display a widget in the particular cell. But in your case you should use a delegate derived from the QItemDelegate and reimplement the createEditor() method to create your combo-box.

Adding to Tomas's answer you will also need to reimplement the setEditorData and setModelData functions.
setEditorData is used to get the data from the model and set the initial value of the editor.
setModelData updates the data in the model according to the data entered in the editor.

Related

How to give a model from qtreeview two clickable icons

my problem is i want to add a new function to the item in teeview but the method of QAbstractItemModel::data(index,role) only give one checkbox or one icon to set
now i want to set two picture,which is clickable.
can it use the method of 'paint' or others?

Proper way to find out which Tab of QTabWidget is currently selected

I have a QTabWidget with several tabs, every tab containing a QTableWidget with numbers. Outside of this QTabWidget, I have one button. When the button is pressed, the currently selected widget should be processed. The QTableWidgets are identical in their structure, they just display numbers, and if I have a pointer to one of these widgets, I cannot deduce which tab it came from.
A simple method to identify which widget is currently selected is to call currentIndex(). But this will break when I reorder the tabs in the designer and is probably not working with movable tabs.
Another way is to use currentIndex() and tabText(int index). Like this I have a stable way to find out which tab is currently selected. The disadvantage of this is that I am then dependent on having unique tab texts, and I in general don't like to rely on a UI property to implement functionality.
My solution for now is to give every Widget a different accessible name that I can then read out like this:
QWidget* widget = tabWidget->currentWidget();
QString* name = widget->accessibleName();
This works, but I wonder if there is a better solution, something like the Qt::UserRole that can be assigned to any cell in a QTableWidget.

Keep the selection after filtering a QTableView with a QSortFilterProxyModel

I created a QTableView linked to a QSortFilterProxyModel linked to another model.
Under the QTableView (in the GUI) there is a QLineEdit used for "searching" an element in the view.
My idea is to write in the QLineEdit what I'm looking for and let the view show only the matched elements. After filtering, I want to select the concerned item and then clean the QLineEdit for returning at the complete view.
Everything works but the selected item that will be filtered will also lose the selection because of the invalidation.
How can I solve this problem?
Why don't you remember the selected rows before the filtering and then just restore it when you're done with filtering.
You could use the QItemSelectionModel directly I'd imagine.
Use QItemSelectionModel::selectedRows() before filtering and select rows after filtering using QItemSelectionModel::select().
I know this thread is very old, but I thought I'd leave the comment for anybody else facing a similar problem.
From what you wrote it looks like the problem is in the QTableView loosing selection when you're cleaning your QLineEdit content. If you're starting your 'search' routine in the line edit's editingFinished() or textChanged() signals you can disconnect from them before changing the QLineEdit and then reconnect back again. Or use a boolean flag and don't change filtering when it's on. It would be much easier to answer your question if you would post up a simplified version of your code with the problem you're having.

QTableView - samples

How to use QTableView in Nokia Qt SDK (for mobiles). I referred some of documents but still I am not clearing about the QTableView. Please any one suggest how to use the QTableView.
I want to show the QTableView with three columns.
For the table data, you need to implement a model which will hold the data. If you don't require anything special, you can just subclass QAbstractTableModel.
Quoting the most important parts from the documentation:
When subclassing QAbstractTableModel,
you must implement rowCount(),
columnCount(), and data().
Editable models need to implement
setData(), and implement flags() to
return a value containing
Qt::ItemIsEditable.
You haven't specified where you get the data you are going to show in your table. That determines how you need to implement the required functions.
For even more simple model, use QStandardItemModel which already has a basic implementation for all required functions.

file selector in a qtablewidget

I'm trying to get one cell in a QTableWidget to be a box with button at end"..." with file selector, but don't know how to change what kind of widget the cell is.
To control the types of cells in a table, it's best to use the QTableView class. Then, by using QItemDelegate you can make some table cell a button, to which you may bind a signal that opens a dialog.
I recommend starting by reading about delegate classes in the Qt docs.