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 ;
Related
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
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?
I am currently adding rows to my QTableView as such
QStandardItem* itm;
QStandardItemModel* model = new QStandardItemModel(this);
model->setColumnCount(2);
model->appendRow(new QStandardItem("Some Text in Column1");
How do I add items to column 2 dynamically by appending?
In the above example column 2 is empty. How do I add item to column 2?
Calling appendRow(QStandardItem *) only adds a single item to the first column. You would need to pass in a QList to appendRow() to add items to each column, e.g.:
QList<QStandardItem *> items;
items.append(new QStandardItem("Column 1 Text"));
items.append(new QStandardItem("Column 2 Text"));
QStandardItemModel* model = new QStandardItemModel(this);
model->setColumnCount(2);
model->appendRow(items);
See http://doc.qt.io/qt-5/qstandarditemmodel.html#appendRow for more detail.
I want to add Qdate to my table say QTableview.The problem is if i convert it into string i can add and retrieve the data.But i want to store as date only in my model.
void MainWindow::setUpTabel()
{
QDateTime myDate;
myDate.setDate(QDate::currentDate());
//myModel
QStandardItemModel model = new QStandardItemModel(this);
QStandardItem *item = new QStandardItem;
item.setData(myDate,Qt::UserRole);
//Myview is also created and set the model to it
m_tableView->setModel(model);
}
The problem is i'm not able to see the date in my table.
As the documentation says, you must set the item into the model specifying the row and columng where you are going to set the item.
http://qt-project.org/doc/qt-4.8/qstandarditemmodel.html
Modifying your code:
void MainWindow::setUpTabel()
{
int row = 0, column = 0; // here you decide where is the item
QDateTime myDate;
myDate.setDate(QDate::currentDate());
QStandardItemModel model = new QStandardItemModel(this);
QStandardItem *item = new QStandardItem(myDate);
model.setItem(row, column, item);
m_tableView->setModel(model);
}
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); });