Here is an example, see if this is what you want. Click the item and it's row number will appear above.
#include <QtWidgets>
// TreeWidget.h
{
Q_OBJECT
public:
explicit TreeWidget
(QWidget * parent
= 0) {
setColumnCount(1);
AddItems();
setSortingEnabled(true);
}
signals:
void selectedRowChanged
(const QString & row
);
private slots:
{
if(m)
row
= QString::number(countRow
(index
));
emit selectedRowChanged(row);
}
private:
{
int count = 0;
if(index.isValid())
{
count = (index.row() + 1) + countRow(index.parent());
if(parent.isValid())
{
for(int r = 0; r < index.row(); ++r)
count += model()->rowCount(parent.child(r, 0));
}
}
return count;
}
void AddItems()
{
root->addChild(sub_1);
root->addChild(sub_2);
root->addChild(sub_3);
sub_1->addChild(sub_1_1);
sub_1->addChild(sub_1_2);
addTopLevelItem(root);
}
};
#include <QtWidgets>
// TreeWidget.h
class TreeWidget : public QTreeWidget
{
Q_OBJECT
public:
explicit TreeWidget(QWidget * parent = 0)
: QTreeWidget(parent)
{
setColumnCount(1);
AddItems();
setSortingEnabled(true);
connect(this, SIGNAL(clicked(QModelIndex)), SLOT(evalRow(QModelIndex)));
}
signals:
void selectedRowChanged(const QString & row);
private slots:
void evalRow(const QModelIndex & index)
{
QString row = "<not selected>";
QAbstractItemModel * m = model();
if(m)
row = QString::number(countRow(index));
emit selectedRowChanged(row);
}
private:
int countRow(const QModelIndex & index)
{
int count = 0;
if(index.isValid())
{
count = (index.row() + 1) + countRow(index.parent());
const QModelIndex parent = index.parent();
if(parent.isValid())
{
for(int r = 0; r < index.row(); ++r)
count += model()->rowCount(parent.child(r, 0));
}
}
return count;
}
void AddItems()
{
QTreeWidgetItem * root = new QTreeWidgetItem(QStringList() << "root");
QTreeWidgetItem * sub_1 = new QTreeWidgetItem(QStringList() << "sub_1");
QTreeWidgetItem * sub_1_1 = new QTreeWidgetItem(QStringList() << "sub_1_1");
QTreeWidgetItem * sub_1_2 = new QTreeWidgetItem(QStringList() << "sub_1_2");
QTreeWidgetItem * sub_2 = new QTreeWidgetItem(QStringList() << "sub2");
QTreeWidgetItem * sub_3 = new QTreeWidgetItem(QStringList() << "sub3");
root->addChild(sub_1);
root->addChild(sub_2);
root->addChild(sub_3);
sub_1->addChild(sub_1_1);
sub_1->addChild(sub_1_2);
addTopLevelItem(root);
}
};
To copy to clipboard, switch view to plain text mode
// main.cpp
#include "TreeWidget.h"
int main(int argc, char *argv[])
{
TreeWidget * treeWidget = new TreeWidget;
layout->addWidget(label);
layout->addWidget(treeWidget);
mainWidget.show();
return app.exec();
}
// main.cpp
#include "TreeWidget.h"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QLabel * label = new QLabel;
TreeWidget * treeWidget = new TreeWidget;
QWidget mainWidget;
QVBoxLayout * layout = new QVBoxLayout(&mainWidget);
layout->addWidget(label);
layout->addWidget(treeWidget);
QObject::connect(treeWidget, SIGNAL(selectedRowChanged(QString)), label, SLOT(setText(QString)));
mainWidget.show();
return app.exec();
}
To copy to clipboard, switch view to plain text mode
Bookmarks