sort row by column , when getting text always wrong - qt

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();

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);
}

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();

how to get selected rows in QTableView

After watching many threads about getting selected rows numbers, I am really confused.
How do you get ROW numbers in QTableView using QStandardItemModel I used below selection model and behavior as
setSelectionBehavior(QAbstractItemView::SelectRows);
setSelectionMode(QAbstractItemView::SingleSelection);
and if you have your own way of selecting can you explain how it works.
Thanks for the help!
The method selectionModel() return a QItemSelectionModel.
You can use QItemSelectionModel class to check/change/other selection(s)
Example:
QItemSelectionModel *select = table->selectionModel();
select->hasSelection() //check if has selection
select->selectedRows() // return selected row(s)
select->selectedColumns() // return selected column(s)
...
Check selectedRows method of the QItemSelectionModel Class .
QModelIndexList selection = yourTableView->selectionModel()->selectedRows();
// Multiple rows can be selected
for(int i=0; i< selection.count(); i++)
{
QModelIndex index = selection.at(i);
qDebug() << index.row();
}
try:
QModelIndexList indexList = yourTableView->selectionModel()->selectedIndexes();
int row;
foreach (QModelIndex index, indexList) {
row = index.row();
....
}
Since you use
setSelectionBehavior(QAbstractItemView::SelectRows);
setSelectionMode(QAbstractItemView::SingleSelection);
so only one row can be selected each time, then you can try this:
auto rowList = yourTableView->selectionModel()->selectedRows();
if(rowList.count() > 0)
int rowNumber = rowList.constFirst().row();
else
// no row is selected

QTableView + QSqlTableModel - how to read id of selected row

I'm using QTableView with QSqlTableModel. In my view I don't display the column containing record id. How can I acquire id of the selected row if its not displayed in any column ?
Thanks for help :)
Also you can retrieve id directly from QSqlQueryModel but I am not sure it's more convinient then proposed by soulSurfer.
Using QModelIndex for desired row:
QSqlQueryModel *model = tableView->model();
QSqlRecord record= model->record(desiredIndex->row());
QSqlField field = record.field(id_column_index);
int id = field.value().toInt();
Hmmm...one way is to get the ID from the model and hide it in the view with
void QTableView::setColumnHidden (int column, bool hide)
then you basically get it, but hide it, and from here, you can get it easily with from the model directly by using the index emited from
void QAbstractItemView::activated ( const QModelIndex & index )
Signal.

Resources