Qt - SelectedItems in QTableView - qt

I'm trying to return a vector of selected rows from a QTableView widget(snippet copied below), however the values returned do not correspond to the selection and I believe I've not understood QModelIndexList/QModelIndex with respect to QTableView. Can you let me know where I'm wrong or the right way to access selected items from a QTableView?
C_model is of type QStandardItemModel
for(int i = 0; i < c_model->rowCount(); i++)
{
if (selectionModel->isRowSelected(i, QModelIndex()))
{
QStringList selection;
std::vector<std::string> currentIndexValues;
for (int j = 0; j < c_model->columnCount(); j++)
{
QVariant q = c_model->data(c_model->index(i, j));
selection.append(q.toString());
currentIndexValues.push_back(q.toString().toLocal8Bit().constData());
printf(" %s\t ", currentIndexValues[j].c_str());
}
printf("\n");
v_selectedItems.push_back(currentIndexValues);
}
}
Thanks

QAbstractItemView (the base class of QTableView) offers a QItemSelectionModel for this purpose. You can access that model via QTableView::itemSelectionModel() and then retrieve the selected rows via QItemSelectionModel::selectedRows():
QModelIndexList selectedRows = yourTableView->selectionModel()->selectedRows();
foreach( QModelIndex index, selectedRows )
{
int row = index.row();
}

Related

How to loop over QAbstractItemView indexes?

I want to fire QAbstractItemView::doubleClicked slot programaticaly for an item that has specific text. I want to do this using QAbstractItemView class and not it's implementations if possible.
This task boils down to looping over items and comparing strings. But I cannot find any method that would give me all QModelIndexes. The only method that gives any QModelIndex without parameters is QAbstractItemView::rootIndex. But when I look into QModelIndex docs, I again cannot see a way to access it's children and siblings.
So how to access all QModelIndexes in QAbstractItemView?
The indexes are provided by the model, not by the view. The view provides the rootIndex() to indicate what node in the model it considers as root; it might be an invalid index. Otherwise it has nothing to do with the data. You have to traverse the model itself - you can get it from view->model().
Here's a depth-first walk through a model:
void iterate(const QModelIndex & index, const QAbstractItemModel * model,
const std::function<void(const QModelIndex&, int)> & fun,
int depth = 0)
{
if (index.isValid())
fun(index, depth);
if ((index.flags() & Qt::ItemNeverHasChildren) || !model->hasChildren(index)) return;
auto rows = model->rowCount(index);
auto cols = model->columnCount(index);
for (int i = 0; i < rows; ++i)
for (int j = 0; j < cols; ++j)
iterate(model->index(i, j, index), model, fun, depth+1);
}
The functor fun gets invoked for every item in the model, starting at root and going in depth-row-column order.
E.g.
void dumpData(QAbstractItemView * view) {
iterate(view->rootIndex(), view->model(), [](const QModelIndex & idx, int depth){
qDebug() << depth << ":" << idx.row() << "," << idx.column() << "=" << idx.data();
});
}

QTableView shows 1d data in exactly one row/column. How to show it two-dimensionally?

Say I have a one-dimensional QStandardItemModel and a QTableView instance:
QStandardItemModel model;
for (int i = 1; i < 10; ++i) {
QStandardItem *item = new QStandardItem(QString::number(i));
model.appendRow(item);
}
QTableView tableView;
tableView.setModel(&model);
tableView.show();
This shows the data in the first column, but I want to show it in a two-dimensional way like this:
1 2 3
4 5 6
7 8 9
Additionally the user should be able to select the data, which means that a custom QStyledItemDelegate probably isn't the way to go to implement this.
So one needs to create a custom QAbstractItemView, where the documentation is unfortunately a bit lacking in my opinion. Help?
First of all, if you want show data as 2d array, you shoul write another loop. For example this:
int counter = 0;
QStandardItemModel *model = new QStandardItemModel(this);
for (int i = 0; i < 3; ++i)
for (int j = 0; j < 3; ++j)
{
counter++;
QStandardItem *item = new QStandardItem(QString::number(counter));
model->setItem(i,j,item);
}
ui->tableView->setModel(model);
On my computer this works exactly as you want.
Secondly. User can do different actions with cells. QTableView have a few very good signals. With this signal, we can communicate with cells. I write one more code snippet.
int counter = 0;
QStandardItemModel *model = new QStandardItemModel(this);
for (int i = 0; i < 3; ++i)
for (int j = 0; j < 3; ++j)
{
counter++;
QStandardItem *item = new QStandardItem(QString::number(counter));
model->setItem(i,j,item);
}
connect(ui->tableView,SIGNAL(clicked(QModelIndex)),this,SLOT(clickedIndex(QModelIndex )));
ui->tableView->setModel(model);
//...
void MainWindow::clickedIndex(QModelIndex index)
{
if(index.isValid())
{
ui->tableView->model()->setData(index,QString("you press %1 , %2 cell").arg(index.row()).arg(index.column()));
}
}
In this example, when user clicked on some cell, text in the cell tell him number of this cell.
I hope,it helps.

QT :how to check if there Empty cell in qt table widget

i have table widget with specific row and column ,
my function is as follow
get value from the first column and second column
compare between them and return result in the third column
Ex: first column :1 2 3 Second column 2 2 3 Result column No Yes Yes
I make sure that my code work by using the qDebug, however when I compile and run it the mainwindow stopped and crash.
I use for loop to go throw all rows for(int row=0;rowtableWidget->rowCount();row++)
I think this line rowtableWidget->rowCount() coz when it read empty cells the app Freeze and stop working .
how can I void that to happen
void MainWindow::GenerateRes() {
QString Result;
for(int row = 0; row < ui->tableWidget->rowCount(); row++) {
QString R1 = ui->tableWidget->item(row, 0)->text();
QString R2 = ui->tableWidget->item(row, 1)->text();
if(R1 == R2) {
Result = "P" ;
} else {
Result = "F" ;
}
QTableWidgetItem *Item = new QTableWidgetItem(Result);
ui->tableWidget->setItem(row, 2, Item);
qDebug() << Item;
}
}
To check whether the cell(i,j) in QTableWidget is empty or not, use isNull() or isEmpty().
Example:
for(int i=0; i < ui->tableWidget->rowCount(); i++)
{
for(int j=0; j < ui->tableWidget->columnCount(); j++)
{
bool flag = ui->tableWidget->item(i,j)->text().isNull();
if (!flag) /* the cell is not empty */
{
// do stuff
}
else /* the cell is empty */
{
// do stuff
}
}
}
maybe you should check the values returned from tableWidget::item(), because the functions can return 0 if no item is asigned to the provided coordinates, and in that case you're trying to call a method (QTableWidgetItem::text()) on a zero pointer.
Try something like:
QString R1;
QTableWidgetItem *item1(ui->tableWidget->item(row,0));
if (item1) {
R1 = item1->text();
}
// and so on...
Your code looks strange anyway, the 5th line (ui->tableWidget->rowCount()) doesn't make sense, you shouldn't be able to compile that (at least you're missing a semicolon).

How to show icons use QSqlRelationalTableModel and QTableView in every row

Now,I am try to use QSqlRelationalTableModel and QTableView to show my data in database,according to the properity "fileType" such as "doc,txt,exe,sln" to show a icon in the first column.
dirModel=new QSqlRelationalTableModel(this);
dirModel->setTable("ecm_doc");
dirModel->setFilter(QString("creatoruserid='%1' and parentid='%2'").arg(userid).arg(parentid));
dirModel->select();
dirView=new QTableView(this);
dirView->setItemDelegate(new DirDelegate(this));
dirView->setModel(dirModel);
dirView->setSelectionMode(QAbstractItemView::SingleSelection);
showIcon();
void DirTree::showIcon()
{
int rowCount = dirModel->rowCount();
for(int row = 0; row < rowCount; row++)
{
//here is a test.
QModelIndex index = dirModel->index(row, 1);
QIcon folderIcon(style()->standardPixmap(QStyle::SP_DirClosedIcon));
dirModel->setData(index, folderIcon, Qt::DecorationRole);
}
}
Help me,online wait:)
You should subclass from QSqlRelationalTableModel and reimplement the data() function in order to specify the icon:
QVariant MyModel::data(const QModelIndex &index, int role) const
{
// I assume that 1 is the db table column with the filetype property
if (index.column() == 1 && role == Qt::DecorationRole)
{
QSqlRecord r = modelRecord(index.row());
QString fileType = r.field(1).value().toString();
QIcon icon;
if (fileType == "exe")
icon = QIcon(":/PathToIcon/exe.png");
else if (fileType == "sln")
icon = QIcon(":/PathToIcon/sln.png");
...
return QVariant(icon);
}
return QSqlRelationalTableModel::data(index, role);
}

HowTo restore QTreeView last expanded state?

What I have:
QTreeView class with table data
And connected QAbstractTableModel model
Question: how to save expanded state of items? Is some one have finished solutions?
PS: I know, that I can do this code by myself, but I don't have much time, and this is not the major problem of our project, but still we need it, because app contain a lot of such tables, and every time expanding tree items is annoyed process...
First, thanks to Razi for persistentIndexList and isExpanded way.
Second, here is the code which works for me just fine :-)
dialog.h file:
class Dialog : public QDialog
{
Q_OBJECT;
TreeModel *model;
TreeView *view;
public:
Dialog(QWidget *parent = 0);
~Dialog(void);
void reload(void);
protected:
void createGUI(void);
void closeEvent(QCloseEvent *);
void saveState(void);
void restoreState(void);
};
dialog.cpp file:
Dialog::Dialog(QWidget *parent)
{
createGUI();
reload();
}
Dialog::~Dialog(void) {};
void Dialog::reload(void)
{
restoreState();
}
void Dialog::createGUI(void)
{
QFile file(":/Resources/default.txt");
file.open(QIODevice::ReadOnly);
model = new TreeModel(file.readAll());
file.close();
view = new TreeView(this);
view->setModel(model);
QVBoxLayout *mainVLayout = new QVBoxLayout;
mainVLayout->addWidget(view);
setLayout(mainVLayout);
}
void Dialog::closeEvent(QCloseEvent *event_)
{
saveState();
}
void Dialog::saveState(void)
{
QStringList List;
// prepare list
// PS: getPersistentIndexList() function is a simple `return this->persistentIndexList()` from TreeModel model class
foreach (QModelIndex index, model->getPersistentIndexList())
{
if (view->isExpanded(index))
{
List << index.data(Qt::DisplayRole).toString();
}
}
// save list
QSettings settings("settings.ini", QSettings::IniFormat);
settings.beginGroup("MainWindow");
settings.setValue("ExpandedItems", QVariant::fromValue(List));
settings.endGroup();
}
void Dialog::restoreState(void)
{
QStringList List;
// get list
QSettings settings("settings.ini", QSettings::IniFormat);
settings.beginGroup("MainWindow");
List = settings.value("ExpandedItems").toStringList();
settings.endGroup();
foreach (QString item, List)
{
// search `item` text in model
QModelIndexList Items = model->match(model->index(0, 0), Qt::DisplayRole, QVariant::fromValue(item));
if (!Items.isEmpty())
{
// Information: with this code, expands ONLY first level in QTreeView
view->setExpanded(Items.first(), true);
}
}
}
Have a nice day!)
PS: this example based on C:\Qt\4.6.3\examples\itemviews\simpletreemodel code.
Thanks to Razi and mosg I was able to get this working. I made it restore the expanded state recursively so I thought I would share that part.
void applyExpandState_sub(QStringList& expandedItems,
QTreeView* treeView,
QAbstractItemModel* model,
QModelIndex startIndex)
{
foreach (QString item, expandedItems)
{
QModelIndexList matches = model->match( startIndex, Qt::UserRole, item );
foreach (QModelIndex index, matches)
{
treeView->setExpanded( index, true );
applyExpandState_sub(expandedItems,
treeView,
model,
model->index( 0, 0, index ) );
}
}
}
Then use like:
void myclass::applyExpandState()
{
m_treeView->setUpdatesEnabled(false);
applyExpandState_sub( m_expandedItems,
m_treeView,
m_model,
m_model->index( 0, 0, QModelIndex() ) );
m_treeView->setUpdatesEnabled(true);
}
I am using the Qt::UserRole here because multiple items in my model can have the same display name which would mess up the expand state restoration, so the UserRole provides a unique identifier for each item to avoid that problem.
These two function by using a loop should do that for you:
QModelIndexList QAbstractItemModel::persistentIndexList () const
bool isExpanded ( const QModelIndex & index ) const
Here is a general approach that should work with any QTreeView based widget, that uses some sort of ID system to identify elements (I am assuming the ID is an int, which is stored inside the Qt::UserRole):
void MyWidget::saveExpandedState()
{
for(int row = 0; row < tree_view_->model()->rowCount(); ++row)
saveExpandedOnLevel(tree_view_->model()->index(row,0));
}
void Widget::restoreExpandedState()
{
tree_view_->setUpdatesEnabled(false);
for(int row = 0; row < tree_view_->model()->rowCount(); ++row)
restoreExpandedOnLevel(tree_view_->model()->index(row,0));
tree_view_->setUpdatesEnabled(true);
}
void MyWidget::saveExpandedOnLevel(const QModelIndex& index)
{
if(tree_view_->isExpanded(index)) {
if(index.isValid())
expanded_ids_.insert(index.data(Qt::UserRole).toInt());
for(int row = 0; row < tree_view_->model()->rowCount(index); ++row)
saveExpandedOnLevel(index.child(row,0));
}
}
void MyWidget::restoreExpandedOnLevel(const QModelIndex& index)
{
if(expanded_ids_.contains(index.data(Qt::UserRole).toInt())) {
tree_view_->setExpanded(index, true);
for(int row = 0; row < tree_view_->model()->rowCount(index); ++row)
restoreExpandedOnLevel(index.child(row,0));
}
}
Instead of MyWidget::saveExpandedState() and MyWidget::saveExpandedState() one could also directly call MyWidget::saveExpandedOnLevel(tree_view_->rootIndex()) and MyWidget::restoreExpandedOnLevel(tree_view_->rootIndex()). I only used the above implementation because the for loop will be called anyway and MyWidget::saveExpandedState() and MyWidget::saveExpandedState() looked cleaner with my SIGNAL and SLOT design.
I have reworked iforce2d's solution into this:
void ApplyExpandState(QStringList & nodes,
QTreeView * view,
QAbstractItemModel * model,
const QModelIndex startIndex,
QString path)
{
path+=QString::number(startIndex.row()) + QString::number(startIndex.column());
for(int i(0); i < model->rowCount(startIndex); ++i)
{
QModelIndex nextIndex = model->index(i, 0, startIndex);
QString nextPath = path + QString::number(nextIndex.row()) + QString::number(nextIndex.column());
if(!nodes.contains(nextPath))
continue;
ApplyExpandState(nodes, view, model, model->index(i, 0, startIndex), path);
}
if(nodes.contains(path))
view->setExpanded( startIndex.sibling(startIndex.row(), 0), true );
}
void StoreExpandState(QStringList & nodes,
QTreeView * view,
QAbstractItemModel * model,
const QModelIndex startIndex,
QString path)
{
path+=QString::number(startIndex.row()) + QString::number(startIndex.column());
for(int i(0); i < model->rowCount(startIndex); ++i)
{
if(!view->isExpanded(model->index(i, 0, startIndex)))
continue;
StoreExpandState(nodes, view, model, model->index(i, 0, startIndex), path);
}
if(view->isExpanded(startIndex))
nodes << path;
}
This way there is no need to match data. Obviously - for this approach to work, tree needs to stay relatively unchanged. If you somehow change the order of tree items - it will expand wrong nodes.
Here is a version which doesn't rely on nodes having a unique Qt::UserRole or Qt::DisplayRole - it just serialises the entire QModelIndex
header:
#pragma once
#include <QTreeView>
class TreeView : public QTreeView
{
Q_OBJECT
public:
using QTreeView::QTreeView;
QStringList saveExpandedState(const QModelIndexList&) const;
void restoreExpandedState(const QStringList&);
};
source:
#include "tree_view.h"
#include <QAbstractItemModel>
namespace
{
std::string toString(const QModelIndex& index)
{
std::string parent = index.parent().isValid() ? toString(index.parent()) : "X";
char buf[512];
sprintf(buf, "%d:%d[%s]", index.row(), index.column(), parent.c_str());
return buf;
}
QModelIndex fromString(const std::string& string, QAbstractItemModel& model)
{
int row, column;
char parent_str[512];
sscanf(string.c_str(), "%d:%d[%s]", &row, &column, parent_str);
QModelIndex parent = *parent_str == 'X' ? QModelIndex() : fromString(parent_str, model);
return model.index(row, column, parent);
}
}
QStringList TreeView::saveExpandedState(const QModelIndexList& indices) const
{
QStringList list;
for (const QModelIndex& index : indices)
{
if (isExpanded(index))
{
list << QString::fromStdString(toString(index));
}
}
return list;
}
void TreeView::restoreExpandedState(const QStringList& list)
{
setUpdatesEnabled(false);
for (const QString& string : list)
{
QModelIndex index = fromString(string.toStdString(), *model());
setExpanded(index, true);
}
setUpdatesEnabled(true);
};
For a QFileSystemModel, you can't use persistentIndexList().
Here is my work around. It works pretty well, even if I do say so myself. I haven't tested to see what happens if you have a slow loading filesystem, or if you remove the file or path.
// scrolling code connection in constructor
model = new QFileSystemModel();
QObject::connect(ui->treeView, &QTreeView::expanded, [=](const QModelIndex &index)
{
ui->treeView->scrollTo(index, QAbstractItemView::PositionAtTop);//PositionAtCenter);
});
// save state, probably in your closeEvent()
QSettings s;
s.setValue("header_state",ui->treeView->header()->saveState());
s.setValue("header_geometry",ui->treeView->header()->saveGeometry());
if(ui->treeView->currentIndex().isValid())
{
QFileInfo info = model->fileInfo(ui->treeView->currentIndex());
QString filename = info.absoluteFilePath();
s.setValue("last_directory",filename);
}
// restore state, probably in your showEvent()
QSettings s;
ui->treeView->header()->restoreState(s.value("header_state").toByteArray());
ui->treeView->header()->restoreGeometry(s.value("header_geometry").toByteArray());
QTimer::singleShot(1000, [=]() {
QSettings s;
QString filename = s.value("last_directory").toString();
QModelIndex index = model->index(filename);
if(index.isValid())
{
ui->treeView->expand(index);
ui->treeView->setCurrentIndex(index);
ui->treeView->scrollTo(index, QAbstractItemView::PositionAtCenter);
qDebug() << "Expanded" << filename;
}
else
qDebug() << "Invalid index" << filename;
} );
Hope that helps someone.
My approach was to save the list of expanded items (as pointers) and when restoring, only set as expanded only the items in this list.
In order to use the code below, you may need to replace TreeItem * to a constant pointer to your object (that doesn't change after a refresh).
.h
protected slots:
void restoreTreeViewState();
void saveTreeViewState();
protected:
QList<TargetObject*> expandedTreeViewItems;
.cpp
connect(view->model(), SIGNAL(modelAboutToBeReset()), this, SLOT(saveTreeViewState()));
connect(view->model(), SIGNAL(modelReset()), this, SLOT(restoreTreeViewState()));
...
void iterateTreeView(const QModelIndex & index, const QAbstractItemModel * model,
const std::function<void(const QModelIndex&, int)> & fun,
int depth=0)
{
if (index.isValid())
fun(index, depth);
if (!model->hasChildren(index) || (index.flags() & Qt::ItemNeverHasChildren)) return;
auto rows = model->rowCount(index);
auto cols = model->columnCount(index);
for (int i = 0; i < rows; ++i)
for (int j = 0; j < cols; ++j)
iterateTreeView(model->index(i, j, index), model, fun, depth+1);
}
void MainWindow::saveTreeViewState()
{
expandedTreeViewItems.clear();
iterateTreeView(view->rootIndex(), view->model(), [&](const QModelIndex& index, int depth){
if (!view->isExpanded(index))
{
TreeItem *item = static_cast<TreeItem*>(index.internalPointer());
if(item && item->getTarget())
expandedTreeViewItems.append(item->getTarget());
}
});
}
void MainWindow::restoreTreeViewState()
{
iterateTreeView(view->rootIndex(), view->model(), [&](const QModelIndex& index, int depth){
TreeItem *item = static_cast<TreeItem*>(index.internalPointer());
if(item && item->getTarget())
view->setExpanded(index, expandedTreeViewItems.contains(item->getTarget()));
});
}
I think this implementation gives extra flexibility compared to some of the others here. At least, I could not make it work with my custom model.
If you want to keep new items expanded, change the code to save the collapsed items instead.

Resources