QTreeView: Setting all selected cells to the user-entered value at once - qt

I have a QTreeView which has the ExtendedSelection attribute (users may select more than one non-contiguous range of cells).
I would like my app to allow them to select these multiple cells and then enter a value and have all the cells take that value.
My issue is that I don't know how to get the full range of cells passed to my model. Right now the index that is passed to the setData method is only the active cell, not the full range of selected cells.
In the past, I have had my view store the currently selected range in the model every time it changes, and then use that to control which cells to modify. This seems kind of hacky and I wonder whether anyone has a better and more elegant solution.
I am using PyQt by the way, though I suspect this applies to QT by itself.
Thanks!

I believe you can use selectionModel method of the QAbstractItemView class to iterate through selected indexes and change values of the corresponding cells. Below is a small example:
foreach (QModelIndex index, ui->treeView->selectionModel()->selectedIndexes())
{
qDebug() << "Changing index " << index.row();
ui->treeView->model()->setData(index, "new data");
}
hope this helps, regards

Related

How to move programmatically to selected row in Qt?

I use QTreeView and QSortFilterProxyModel
// Here I determine the index, that was saved before (_lastAddObjectIndex - QModelIndex)
QModelIndex next_index = _proxyModel->index(_lastAddObjectIndex.row(), 0);
// Here I select the row programmatically, and after that I'd like to move to that row (because table might have many rows)
view->selectionModel()->select(next_index, QItemSelectionModel::ClearAndSelect | QItemSelectionModel::Rows | QItemSelectionModel::SelectCurrent);
I assume by moving, you mean scrolling. If so, you can achieve this by using this API:
view->scrollTo(next_index);
You can even change the scroll hint if you pass a second parameter to the method. This depends on whether you are happy with the default value, which just makes sure that the item is visible.
You can refer to the documentation for fine-tuning this behaviour further in case you need to.

How do I Insert an item at top of QTreeView

In my app I'd like to insert an item at the top of a QTreeView.
What I have so far will insert an item just above the currently selected item. The code (nicked, I think, from the EditableTreeviewDemo):
QModelIndex index = this->selectionModel()->currentIndex();
QAbstractItemModel *model = this->model();
if (!model->insertRow(index.row(), index.parent()))
return;
I guess what I need is the index to the current first row? How do I get this?
As a side question, what happens to the current index when a row is inserted? Does it continue to point to the same item, or the same row?
Well first you have to know that insertRow is a function from QAbstractItemModel and it will call insertRows (with an s). This function must be redefined in your model subclass if you want to allow insertion of data in your model.
http://doc.qt.io/qt-5/qabstractitemmodel.html#insertRows
Also consider that any parent of a topmost index is a invalid QModelIndex. Then the call to do would be :
model->insertRow(0, QModelIndex());
And because this is the default value for the second parameter, simply call :
model->insertRow(0);
Then in your redefinition of insertRows simply check the validity of you parent index to ensure you news underlying data is created where you want it to be.
For you question, inserting data in the model won't affect the current and selected items.

QT: QTableView read cell content

I'm writing a small program with QT creator (QT 5.2.1) under Windows 7 (32 bit) and I'm having problems reading the informations stored in a TableView. My application has 3 elements, a TableView to store text data, a TextBrowser to show info and a buttom.
I modified the TableView properties: when the user selects with the mouse a cell, the full row is selected and multiple row selection is not allowed.
The user select a row and when the buttom is pressed, I would like to read the content of a specific TableView cell and show it in a TextBrowser. In particular, I would like to know the row index of the selected row and read the content of the cell with that row index and a specific column index (example 2).
The pseudo-code is this:
void my_program::on_pushButton_clicked()
{
ui->textBrowser->append("button pressed");
QItemSelectionModel *select = ui->tableView->selectionModel();
int index_row = select->selectedRows();
int index_column = 2;
char cell_data[30] = ??[index_row][index_column]
ui->textBrowser->append(cell_data);
}
The main problem is that select->selectedRows() returns a QModelIndex that is a collection of indexs and I do not know how to convert it to int (since multiple selection are not allowed, it should have only one element).
I would be glad if someone can suggest me a way to proceed.
Thanks
Francesco
edit:
Hi Bogdan, thanks a lot!! I succeed to read the cell content by using
ui->textBrowser->append(ui->tableView->model()->data(ui->tableView->model()->index(2,5)).toString());
this give me the content of the cell in position 2,5.
not sure if this is the best way or not but it works !!.
Can you be a bit more precise about how to iterate the QModeIndexList ? thanks :)
selectedRows() returns QModelIndexList, thus you need to iterate over it and call QModelIndex::data() to get stored data.

QTableView problems with row

Hello guys i new to QT and i am doing Qtableview to add information in 3 columns to infinite row like
|--1--|--2--|--3--|
|--1--|--2--|--3--|
|--1--|--2--|--3--|
this is how i want to insert/append row but this is how i am getting after insert/append row functions.
|--1--|--2--|--3--|
|-----|-----|-----||--1--|--2--|--3--|
|-----|-----|-----||-----|-----|-----||--1--|--2--|--3--|
i am getting empty gaps and increased column count
i am using QStandardItemmodel for model this is code that creates the model item
void tableview::add_tableview() //this is used to add data to tableview
{
//to get data from line edit in add window
QStandardItem *item_1 = new QStandardItem(QString(enter1_edit->text()));
QStandardItem *item_2 = new QStandardItem(QString(enter2_edit->text()));
QStandardItem *item_3 = new QStandardItem(QString(enter3_edit->text()));
list << item_1 << item_2 << item_3;
model->appendRow(list);
//to set the model
main_tableview->setModel(model);
}
The values in the rows are added using qlineedit enter1_edit,enter2_edit & enter3_edit(its in other function/method)
main_tableview is the object of qtableview
Thanks for helping me!
Ding Ding Ding, I have it.
I tried to reproduce your problem without success unil I tried something...suboptimal :).
Is your list by any chance a global entity? If yes (and I assume it is) you keep adding items to it.
First time you call add_tableview() all is good, list is empty, gets three QStandardItem pointers added and is used to append a row with those three items. So far so god.
You call add_tableview() again, now you create another three QStandardItems and APPEND them to the list (which still contains the three from the last call). Invoking appendRow() actually tries to insert six items, from which the first three allready exist in the model. And as you know you mustn't add the same item pointer to a model twice. Thankfully Qt doesn't crash but inserts three empty columns for the item pointers allready in it.
Solution: At the end of your add_tableview() method call list.clear() or use a local variable for the list. The overhead should be minimal.
Best regards
D

Edit all selected items into one editor

Using QTableView I would like to be able to select multiple cells and change all selected cells at once. How I can do it?
I'm not sure exactly what you mean by "changing" the selected cells (content, formatting, something else?) but I think QTableView::selectedIndexes() (or QTableView::selectionModel() if you need more power) is going to help out. You can loop through the returned indexes and update your underlying model.
If you are using a QSortFilterProxyModel you will have to use the QSortFilterProxyModel::mapFromSource() and related methods to map from the selected cells on your table view to the actual model indexes.

Resources