How to replace rows with columns in QSqlTableModel? - replace

I found a lot of answers how to transform rows into columns in SQL. But I need to transforms rows into columns in QSqlTableModel.
As I understand it should not be a very difficult task but I can't find any idea of how to realize it.
Perhaps data(), setData() and some other methods could be reimplemented, but I am afraid to miss something...
Or, maybe, some methods of QTableView should be reimplemented.

As I understand QIdentityProxyModel could be used to solve this problem.
Unfortunately, QIdentityProxyModel is available since version 4.8.
So I inherited QAbstractProxyModel and implemented mapToSource() and mapFromSource(), rowCount(), columnCount() and few more methods to switch rows with columns.

Related

Wxsmith how to make WxGrid infinite rows

I want to keep writing data into WxGrid, but i need to set the rows for WxGrid. I've searched some way but the answer that i get is to use Wx.GridTableBase which is for WxPython.
Is there any way to make WxGrid rows infinite?
I'm using codeblocks c++ with WxSmith.
If you're going to have a lot of data in your grid, it is much better (i.e. more efficient) to define your own grid table, i.e. a class deriving from wxGridTableBase, and store your data there.
Okay so i just need to use AppendRows() everytime i need to add rows. it's on the manuals https://docs.wxwidgets.org/3.0/classwx_grid.html

Insert row on top of a qtablewidget

I'll like to know if it's possible to insert a row on top of a qtablewidget ?
Something like:
ui->myqtablewidget->insertRow(0);
Just a heads up - I'm answered based on the documentation and C++
knowledge, not direct experience with Qt.
It would seem that QTableWidget has an insertRow() function. You simply need to specify where you want the row placed. Read the documentation here.
Thus, it would seem from the documentation, you can insert a row at the beginning ("top") of your table with that exact line of code...
ui->myqtablewidget->insertRow(0);
...assuming ui and myqtablewidget are declared properly.

Re-implement sorting for QTableWidget?

Is there a way to re-implement the sorting function for QTableWidget? I use QComboBox (multicheck) in the first row of my table to filter the data. I also want be able to sort the data in the table, but I want my widget to stay in the first row no matter how I sort the data.
I know that I can re-implement the < operator and set a value as well as a widget in a cell, but that does of course not help me.

QTreeView - Sort and Filter a model

I am trying to create a QTreeView which displays some sorted information. To do this I use a QSortFilterProxyModel between the view and my model.
The problem is that I want to limit the number of rows to the first n rows (after sorting). The filter function from the model receives the original sourceRow so I cannot use it.
I've tried chaining two QSortFilterProxyModel: the first for the sorting and the second for the filtering. But it seems that the second proxymodel(filtering) doesn't receive the rows sorted....
Is there another way to do it?
Has anyone use this technique(chaining of 2 proxy models) and it works?
thank you
EDIT:
I've tried with the rowCount and it doesn't work.
I've also tried to chain 2 proxy models but the problem is that the view calls the sort function for the model it receives. So if the first proxy sorts and the second filters the sort will be called on the filter model and the data won't be sorted.
EDIT2: I've looked into the qt source code and the filtering is done before sorting, so in the filterAcceptsRow() I don't know any sorting order.
Just out of curiousity, have you tried overriding the rowCount method and just return 25 (or whatever n is in your case)? It might be as simple as that... well, if you'll always have at least n items.
Otherwise, you could try chaining the models. I don't know why it wouldn't work, but I've never tried something like it myself.
After trying a number of overcomplicated ways to solve this I've done a small hack for my problem: after I insert/remove a row I call setRowHidden to hide the first n rows.
This is not the most elegant solution and is particular for my needs, but I am unable to find a better alternative.
I like to mention that on gtk, because the filter and the sort proxy models are separated, this can be done fairly easy.
I'm still hoping someone can provide a better solution to this.

most effective row removal strategy for QStandardItemModel

I have a QStandardItemModel with several 100,000 records of data, and a QSortFilterProxyModel on top of it for filtering and sorting capabilities. I want to remove a substantial number of records, say 100,000, based on the value of one of the columns.
The current implementation iterates over the source model, tests for the value in the appropriate column, and calls removeRow. This turns out to be an extremely slow approach, I don't know why (I've already turned off the signalling of the source model and the sortfilterproxymodel).
What is a more efficient approach?
Can the QSortFilterProxyModel help, e.g. by creating a selection of records to be deleted, and using removeRows?
Thanks,
Andreas
QAbstractItemModel::removeRows() is a candidate, provided that the rows are contiguous. If the model is sorted by the column you are using to do the removal test, then you should be able to use this.
The more effecient approach would be implementing your own model with QAbstractItemModel interface instead of using QStandardItemModel. Then you can build custom indexes which will help you increase performance while deleting items.