How to get QString from QListView selected item in Qt? - qt

I need to get the selected item name in QListView as a QString. I have tried to google, but I haven't found anything useful.

It depends on selectionMode lets say you have ExtendedSelection which means you can select any number of items (including 0).
ui->listView->setSelectionMode(QAbstractItemView::ExtendedSelection);
you should iterate through ui->listView->selectionModel()->selectedIndexes() to find indexes of selected items, and then call text() method to get item texts:
QStringList list;
foreach(const QModelIndex &index,
ui->listView->selectionModel()->selectedIndexes())
list.append(model->itemFromIndex(index)->text());
qDebug() << list.join(",");

In case if QAbstractItemView::ExtendedSelection is disabled (only possible to select one item at a time), this is how you can do it without any loop:
QModelIndex index = ui->listView->currentIndex();
QString itemText = index.data(Qt::DisplayRole).toString();

Related

How to set QModelIndex to QListView

Im really a noob at QT so go easy on me
So i have 2 QListviews
1. TimeList
2. DateList
What im trying to Do is that when i select an item in Timelist the same index in Datelist will be selected and after that i will send the data to a datetime edit
I tried to use that QAbstractItemmodel model->index(row,column);
and createindex but i dont know syntax that well so i messed it up
QModelIndex i = ui->TimeList->currentIndex(); //it gets the index when an item is clicked
ui->DateList->setcurrentIndex(i); //it sets the same index in the other QListView
ui->DateList->clearSelection();
ui->DateList->selectionModel()->select(i, QItemSelectionModel::Select); //This highlights the same index in other QListView and it works fine
QTime t = i.data(Qt::DisplayRole).toTime(); // It converts to QTime fine
i = ui->DateList->currentindex(); // i try to change the value of index to get the date
QDate d = i.data(Qt::DisplayRole).toDate();
ui->TimeEdit->setTime(t);
ui->DateEdit->setDate(d);
What i have gathered is that i.data output is invalid for QDate because it says so in qDebug so.
So i suppose index doesnt only hold row and column values but how to assign it to Datelist is beyond me. (:
Thanks in advance
I just needed to learn syntax to create a new QIndexModel
QModelIndex i = ui->TimeList->currentIndex();
QModelIndex i2 = ui->DateList->model()->index(i.row(), 0);
So i use the same row value from index1 but its assigned to the second QListview

Invert selection on QListView

I have a QListView and I have already set a model to it. How can I implement a method for invert selection?
I have already tried making a new QItemSelection that contains all the items of my model in order to call ui.listView->selectionModel()->select(selection, QItemSelectionModel::Toggle); but my QItemSelection returns empty.
I have also thought of using the same method for each QModelIndex of my model, but I found no way to get a list of all QModelIndex of my model.
Any ideas?
I finally found it. It is all about understanding QModelIndex and not asking the model for its items. First thing is to get the QModelIndex that is the root of the current level/branch of the QListView and then get the QModelIndexs of the first and last children of this root QModelIndex.
After that you can easily create a new QItemSelection that contains every child of this root.
Finally you use this QItemSelection to toggle the selection-model of the QListView.
void BrowserWidget::invertSelection() {
QModelIndex rootIndex = ui.listView->rootIndex();
QModelIndex first = rootIndex.child(0, 0);
int numOfItems = m_itemsModel->rowCount(rootIndex);
QModelIndex last = rootIndex.child(numOfItems - 1, 0);
QItemSelection selection(first, last);
ui.listView->selectionModel()->select(selection, QItemSelectionModel::Toggle);
}

How to change color of item in QListView

I have my own subclass of QListView and I would like to change the color of an item with index mLastIndex . I tried with
QModelIndex vIndex = model()->index(mLastIndex,0) ;
QMap<int,QVariant> vMap;
vMap.insert(Qt::ForegroundRole, QVariant(QBrush(Qt::red))) ;
model()->setItemData(vIndex, vMap) ;
But it didn't change the color, instead, the item wasn't displayed anymore. Any idea about what was wrong?
Your code are simply clear all data in model and leaves only value for Qt::ForegroundRole since your map contains only new value.
Do this like that (it will work for most of data models not only standard one):
QModelIndex vIndex = model()->index(mLastIndex,0);
model->setData(vIndex, QBrush(Qt::red), Qt::ForegroundRole);
Or by fixing your code:
QModelIndex vIndex = model()->index(mLastIndex,0) ;
QMap<int,QVariant> vMap = model()->itemData(vIndex);
vMap.insert(Qt::ForegroundRole, QVariant(QBrush(Qt::red))) ;
model()->setItemData(vIndex, vMap) ;

QT QStandardItemModel - how to store a list of items in one cell

I am new with QT, so I'll apprciate any help.
In my application, I'm creating a QStandardItemModel with rows and columns. Now I want to save in one cell a list of QStrings - but I dont know how to do that.
I've tried to write this code:
QStandardItem* dataRecords = new QStandardItem();
QList<QStandardItem* > list;
QList<QString>::const_iterator dataRecord;
for( dataRecord = i.value()->begin(); dataRecord != i.value()->end(); ++dataRecord )
list << new QStandardItem((*dataRecord));
dataRecords->appendRows(list);
model->setItem(row, 3, dataRecords);
i is a QList of QString.
Now, I dont know how can I access abd retrive the QString values from the model.
Can anyone please help me? or suggest me another way to do that?
Thanks!
You should read some docks about Model\View proggramming in Qt
To access data stored in model you should use: QVariant QStandardItemModel::data ( const QModelIndex & index, int role = Qt::DisplayRole )
To get QModelIndex for particular cell use: QModelIndex QStandardItemModel::index ( int row, int column, const QModelIndex & parent = QModelIndex() )
Some code example...
QModelIndex superIndex = model->index(i,j);
QString superData= model->data(superIndex).toString();

sort row by column , when getting text always wrong

I'm trying to get text and data from column in index number 0 from row that is selected
but I never get the right data I'm using simple model view TreeView with QSortFilterProxyModel proxy to sort the columns and QStandardItemModel as the model
This is the slot function that is triggered on each doubleClicked
connect(ui.treeView_mainwindow, SIGNAL(doubleClicked( const QModelIndex &)), this,SLOT(tree_itemClicked( const QModelIndex &)));
....
...
void MainWindowContainer::tree_itemClicked(const QModelIndex & index)
{
int iSelectedRow = index.row();
QString groupID;
QString groupName;
groupID = m_model->item(iSelectedRow,0)->data(Qt::UserRole).toString();
groupName = m_model->item(iSelectedRow,0)->text();
}
UPDATE:
Well, I found the answer but I have another question, the answer is :
QString groupID = index.model()->index(index.row(), 0, index.parent()).data(Qt::UserRole).toString();
QString groupName = index.model()->index(index.row(), 0, index.parent()).data(Qt::DataRole).toString();
}
My other question is how do I set data to column in index ( for example: 3 ) in the selected row?
The problem here most likely is that index.row() points to the row in the proxy model after sorting. This is most likely not the same row in your unsorted source model.
Try the following instead:
groupID = m_proxy_model->index(iSelectedRow,0).data(Qt::UserRole).toString();
Have you tried using
QStandardItem * QStandardItemModel::itemFromIndex ( const QModelIndex & index ) const;
Perhaps your rows are not set properly.
If this does not help, you should give an example tree, indicate what you click, what you expect, what you get.
QModelIndex modelIndex = m_proxy_model->index(iSelectedRow,0);
m_proxy_model->data (modelIndex ,Qt::UserRole).toString();

Resources