QStringListModel sorting - c++

How can sort QStringListModel?
Thanks a lot.

By using the sort method.

An alternative to the QStringListModel::sort() method is to use the QStringList::sort() method on the string list stored into the model. This approach is not as efficient as using the QStringListModel::sort().
QStringList list = stringListModel->stringList();
list.sort();
stringListModel->setStringList(list);

You can use a QSortFilterProxyModel
QListView* view = new QListView;
QStringListModel* model = new QStringListModel(this);
QSortFilterProxyModel* proxyModel = new QSortFilterProxyModel(this);
proxyModel->setSourceModel(model);
view->setModel(proxyModel);
Then all you have to do is sort your proxy model with the sort method:
void QSortFilterProxyModel::sort(
int column, Qt::SortOrder order = Qt::AscendingOrder):
Since a string list model has only one column:
proxyModel->sort(0);
Additionally, if you want to sort your model each time a new row is inserted, you can use a connect to sort the proxyModel:
connect(model, &QStringListModel::rowsInserted,
this, [proxyModel](){ proxyModel->sort(0); });

Related

Show Last element in QListView

It sounds trivial, but I could not find the function to show the last added element in a QListView.
It works with a model
// Create model
model = new QStringListModel(this);
// Make data
QStringList List;
// Populate our model
model->setStringList(List);
// Glue model and view together
listView->setModel(model);
Elements are added with
void WidgetMessageList::addString(const QString & message)
{
if(model->insertRow(model->rowCount())) {
QModelIndex index = model->index(model->rowCount() - 1, 0);
model->setData(index, message);
}
}
In this function the shown element should also be the last.
QAbstractItemView::scrollTo
Scrolls the view if necessary to ensure that the item at index is
visible. The view will try to position the item according to the given
hint.
http://doc.qt.io/archives/qt-4.8/qabstractitemview.html#scrollTo
Create a class attibute to hold the last index
Connect QAbstractItemModel::rowsInserted to a slot in your application
In the slot update the index accordingly

How to identify which UI object is selected in QT?

In this case, I have 2 QTableViews in my form and I am making a copy function to copy and paste in Excel, but to make the function I need to declare a model from the table I am copying:
QAbstractItemModel *abmodel = ui->tableview1->model();
QItemSelectionModel *model = ui->tableview1->selectionModel();
QModelIndexList list = model->selectedIndexes();
so I am making an "if" to for the declaration depending on which QTableView I have selected:
if(ui->tableview1(selected)){
QAbstractItemModel *abmodel = ui->tableview1->model();
QItemSelectionModel * model = ui->tableview1->selectionModel();
QModelIndexList list = model->selectedIndexes();
}
if(ui->tableview2(selected)){
QAbstractItemModel *abmodel = ui->tableview2->model();
QItemSelectionModel *model = ui->tableview2->selectionModel();
QModelIndexList list = model->selectedIndexes();
}
Is there a way to make that happen?

how can I get indexes of multiple selected rows in QTableWidget

I have a table where user can select multiple rows, however I need to know indexes of top and last selected row, I tried playing with http://qt-project.org/doc/qt-5/QModelIndex.html so far I have this:
QItemSelectionModel *selections = this->ui->tableWidget->selectionModel();
QModelIndexList selected = selections->selectedRows(3);
But I have no idea how can I use QItemSelectionModel to reach the item of table. How can I do that? There is no function in TableWidget that returns item based on QModelIndex, only QPoint
In order to get the first and last item in the selection range you can simply sort that list. For example:
QItemSelectionModel *selections = this->ui->tableWidget->selectionModel();
QModelIndexList selected = selections->selectedRows(3);
qSort(selected);
QModelIndex first = selected.first();
QModelIndex last = selected.last();
And now let's get the first and last table items:
QTableWidgetItem *firstItem = this->ui->tableWidget->item(first.row(), first.column());
QTableWidgetItem *lastItem = this->ui->tableWidget->item(last.row(), last.column());
Is QTableWidget::item(int row, int column) together with QModelIndex::column () and QModelIndex::row (), respectively, of any help?

Retrieving a QStandardItem through QStandardItemModel by searching or key

Is there any way to assign a unique key to an entry in a QStandardItemModel so that we can check for the presence of that key. If it is present we get the relevant QstandardItem ?
Update:
Here is what I am trying to do. I have 3 column in my table so so i have 3 QStandardItem.
This is the code I am using:
QStandardItem* item0 = new QStandardItem("Column1");
QStandardItem* item1 = new QStandardItem("Column2");
QStandardItem* item2 = new QStandardItem("Column3");
Now my model is called model and I am attaching these to my model as such
moddel->setItem(0,0,item0);
moddel->setItem(0,1,item1);
moddel->setItem(0,2,item2);
I need to assign a row some unique key so that I could check the model for that key and the model would return the row number. Any suggestions.
You could use the setData function of QStandardItem in order to set a custom key for a user defined role, eg
#define MyRole Qt::UserRole + 2
myItem->setData(Qvariant(key), MyRole)
You can get the data of any index in your model by using the data call.
QVariant d = mymodel->data(anindex, MyRole)
Writing a function that checks if a key exists should be straight forward.
The answer by pnezis addresses the storing of a key but not the accessing of a QStandardItem from the model. I addressed the storing of data with a QStandardItem by sub classing QStandardItem as I needed to store a lot of complex data.
To obtain the QStandardItem from the model you need to create a QModelIndex instance with the row/column and then call itemFromIndex(index)
on the model.
My example is taken from a selection callback.
QModelIndex& selectedItem = itemsSelected.front();
QStandardItemModel* model = reinterpret_cast<QStandardItemModel*>(tableView->model());
if (nullptr == model)
return;
QStandardItem *item = model->itemFromIndex(selectedItem);
if (nullptr == item)
return ;

QTableView not properly updated using QSqlQueryModel and QSortFilterProxyModel

I'm using QTableView in order to display the results of QSqlQueryModel. The data in DB is permanently changed so I run the same script every time and need to get updated data. The query is executed in another thread after which it returns the result to main thread.
void SqlThread::setNewScript(QString script)
{
QSqlQueryModel * sqlModel = new QSqlQueryModel();
this->script = script;
QSqlQuery query = QSqlQuery(this->script, db);
sqlModel->setQuery(query);
emit queryFinished(sqlModel);
}
void myTable::onQueryFinished(QSqlQueryModel * model)
{
QAbstractItemModel * oldModel = this->table->model();
QSortFilterProxyModel * sort = new QSortFilterProxyModel();
sort->setSourceModel(model);
this->table->setModel(sort);
delete oldModel;
}
The problem appeared when I've tried to introduce sorting using QSortFilterProxyModel. Since I did it my table haven't received any updated data.
I checked that QSqlQueryModel doesn't receive any updated data while running the same script in DBMS gives me new results.
If I don't use QSortFilterProxyModel the table is updated normally.
I dont know the rest of your code, but this may help.
void SqlThread::setNewScript(QString script)
{
//QSqlQueryModel * sqlModel = new QSqlQueryModel();
//It's better to implement your model as [QSortFilterSqlQueryModel][1]
QSortFilterSqlQueryModel * sqlModel = new QSortFilterSqlQueryModel();
this->script = script;
QSqlQuery query = QSqlQuery(this->script, db);
sqlModel->setQuery(query);
//use select to start query
sqlModel->select();
emit queryFinished(sqlModel);
}
/*
void myTable::onQueryFinished(QSqlQueryModel * model)
{
QAbstractItemModel * oldModel = this->table->model();
QSortFilterProxyModel * sort = new QSortFilterProxyModel();
sort->setSourceModel(model);
this->table->setModel(sort);
delete oldModel;
}
rest of can be corrected like that if you really wanna pass model to
the slot(this does not seems to be good idea as your model is already on the heap)*/
void myTable::onQueryFinished(QSortFilterSqlQueryModel * model)
{
table->setModel(model)
table->setSelectionMode(QAbstractItemView::SingleSelection);//other option(s) you like
table->setSortingEnabled(true);
}