Modify row in QModelIndex in a slot
-
Hello.
I have a QTreeView with two columns. I want to output the content of the second column if a row is double clicked.
It works if the row is clicked in the second column. But it also should output the second column if the first column is clicked but I don't know how to do this.
This is what I've tried:
@
connect(ui->treeView, SIGNAL(doubleClicked(QModelIndex)),
this, SLOT(onDoubleClicked(QModelIndex)));void MainWindow::onDoubleClicked(QModelIndex modelIndex)
{
QModelIndex *lmodelIndex;if(modelIndex.column() == 0)
lmodelIndex = new QModelIndex( //????????
else
lmodelIndex = new QModelIndex(modelIndex);qDebug() << modelIndex << m_treeModel->data(*lmodelIndex, Qt::DisplayRole);
}
@ -
I think you should be having something like:-
@void MainWindow::onDoubleClicked(QModelIndex modelIndex)
{
if (modelIndex.column() == 0 || modelIndex.column() == 1) //as per ur requirement
{
QModelIndex nIndex = m_treeModel->index(modelIndex.row(),1);
qDebug() << "Data" << m_treeModel->data(nIndex, Qt::DisplayRole);
}
}@-Note: written brain to terminal. not tested- works now :)
-
Thank you for your reply.
Unfortunately this will not compile and I have no idea what this void point could be used for:
bq.
mainwindow.cpp:101: error: no matching function for call to 'QModelIndex::QModelIndex(int, int, QModelIndex)'
c:\QtSDK\Desktop\Qt\4.8.1\mingw\include\QtCore/qabstractitemmodel.h:388: note: candidates are: QModelIndex::QModelIndex(int, int, void*, const QAbstractItemModel*)
c:\QtSDK\Desktop\Qt\4.8.1\mingw\include\QtCore/qabstractitemmodel.h:64: note:QModelIndex::QModelIndex(const QModelIndex&)
c:\QtSDK\Desktop\Qt\4.8.1\mingw\include\QtCore/qabstractitemmodel.h:63: note:QModelIndex::QModelIndex()bq. -
Sorry my bad , I have updated the code check again :)
-
Now it compiles but an empty string will be returned always :-)
I think it can't work this way anyway because the index function gets the parameter of the row but no parameter of which parent.
So how could the function know which row from which parent?!?
I think there must be another way ;)
-
Now that depends on how you are populating the treeView. For that above code I have taken the QModelIndex of the current row that is double clicked and column 1, from
@QModelIndex nIndex = m_treeModel->index(modelIndex.row(),1);@
and from the nIndex we can get the data at column 1 at that particular row. It will be highly appreciated if you can share your code.
Regards
Soumitra -
This is my code:
treemodel.h
@
#ifndef TREEMODEL_H
#define TREEMODEL_H#include <QAbstractItemModel>
#include <QModelIndex>
#include <QVariant>class TreeItem;
class TreeModel : public QAbstractItemModel
{
Q_OBJECTpublic:
TreeModel(TreeItem *treeItem, QObject *parent = 0);
~TreeModel();
QVariant data(const QModelIndex &index, int role) const;
Qt::ItemFlags flags(const QModelIndex &index) const;
QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const;
QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const;
QModelIndex parent(const QModelIndex &index) const;
int rowCount(const QModelIndex &parent = QModelIndex()) const;
int columnCount(const QModelIndex &parent = QModelIndex()) const;private:
TreeItem *m_rootItem;
};#endif@
treemodel.cpp
@#include <QtGui>#include "treeitem.h"
#include "treemodel.h"TreeModel::TreeModel(TreeItem *treeItem, QObject *parent)
: QAbstractItemModel(parent)
{
m_rootItem = treeItem;
}TreeModel::~TreeModel()
{
delete m_rootItem;
}int TreeModel::columnCount(const QModelIndex &parent) const
{
if (parent.isValid())
return static_cast<TreeItem*>(parent.internalPointer())->columnCount();
else
return m_rootItem->columnCount();
}QVariant TreeModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid())
return QVariant();if (role == Qt::DisplayRole)
{
TreeItem item = static_cast<TreeItem>(index.internalPointer());
return item->data(index.column());
}return QVariant();
}Qt::ItemFlags TreeModel::flags(const QModelIndex &index) const
{
if (!index.isValid())
return 0;return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
}
QVariant TreeModel::headerData(int section, Qt::Orientation orientation, int role) const
{
if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
return m_rootItem->data(section);return QVariant();
}
QModelIndex TreeModel::index(int row, int column, const QModelIndex &parent)
const
{
if (!hasIndex(row, column, parent))
return QModelIndex();TreeItem *parentItem; if (!parent.isValid())
parentItem = m_rootItem;
else
parentItem = static_cast<TreeItem*>(parent.internalPointer());TreeItem *childItem = parentItem->child(row); if (childItem) return createIndex(row, column, childItem); else return QModelIndex();
}
QModelIndex TreeModel::parent(const QModelIndex &index) const
{
if (!index.isValid())
return QModelIndex();TreeItem *childItem = static_cast<TreeItem*>(index.internalPointer()); TreeItem *parentItem = childItem->parent();
if (parentItem == m_rootItem)
return QModelIndex();return createIndex(parentItem->row(), 0, parentItem);
}
int TreeModel::rowCount(const QModelIndex &parent) const
{
TreeItem *parentItem;
if (parent.column() > 0)
return 0;if (!parent.isValid())
parentItem = m_rootItem;
else
parentItem = static_cast<TreeItem*>(parent.internalPointer());return parentItem->childCount();
}
@treeitem.h
@#ifndef TREEITEM_H
#define TREEITEM_H#include <QList>
#include <QVariant>class TreeItem
{
public:
TreeItem(const QList<QVariant> &data, TreeItem *parent = 0);
~TreeItem();void appendChild(TreeItem *child);
TreeItem *child(int row);
int childCount() const;
int columnCount() const;
QVariant data(int column) const;
int row() const;
TreeItem *parent();QString log(int tabLevel = -1);
private:
QList<TreeItem*> childItems;
QList<QVariant> itemData;
TreeItem *parentItem;
};#endif
@treeitem.cpp
@#include <QStringList>#include "treeitem.h"
TreeItem::TreeItem(const QList<QVariant> &data, TreeItem *parent)
{
parentItem = parent;
itemData = data;
}TreeItem::~TreeItem()
{
qDeleteAll(childItems);
}void TreeItem::appendChild(TreeItem *item)
{
childItems.append(item);
}TreeItem *TreeItem::child(int row)
{
return childItems.value(row);
}int TreeItem::childCount() const
{
return childItems.count();
}int TreeItem::columnCount() const
{
return itemData.count();
}QVariant TreeItem::data(int column) const
{
return itemData.value(column);
}TreeItem *TreeItem::parent()
{
return parentItem;
}QString TreeItem::log(int tabLevel)
{
QString output = "";tabLevel++;
for(int i = 0; i < tabLevel; i++)
output = output + "\t";output = output + itemData[0].toString() + "\n";
foreach(TreeItem *child, this->childItems)
output = output + child->log(tabLevel);tabLevel--;
return output;
}int TreeItem::row() const
{
if (parentItem)
return parentItem->childItems.indexOf(const_cast<TreeItem*>(this));return 0;
}
@Part of mainwindow.cpp
@
QList<QVariant> header;
header << "01" << "02";
TreeItem *rootTreeItem = new TreeItem(header);while(!file.atEnd())
{
TreeItem *folderTreeItem;
QByteArray line = file.readLine();if(line == baFinderFolder)
{
QList<QVariant> folder;line = file.readLine();
line = line.trimmed();
line.remove(0,5);folder << QString(line) << "";
folderTreeItem = new TreeItem(folder, rootTreeItem);
rootTreeItem->appendChild(folderTreeItem);
}if(line == baFinderURL)
{
TreeItem *urlTreeItem;
QList<QVariant> url;line = file.readLine();
line = line.trimmed();
line.remove(0,5);url << QString(line);
line = file.readLine();
line = line.trimmed();
line.remove(0,4);url << QString(line);
urlTreeItem = new TreeItem(url, folderTreeItem);
folderTreeItem->appendChild(urlTreeItem);
}
}qDebug() << rootTreeItem->log();
m_treeModel = new TreeModel(rootTreeItem, this);
ui->treeView->setModel(m_treeModel);
@ -
---push---