#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QtGui/QMainWindow>
{
Q_OBJECT
public:
~MainWindow();
};
#include <QAbstractItemDelegate>
#include <QFontMetrics>
#include <QModelIndex>
#include <QSize>
#include <QTreeView>
#include <QStyledItemDelegate>
#include <QResizeEvent>
{
Q_OBJECT
public:
public slots:
private:
};
class TableDelegate : public QStyledItemDelegate
{
Q_OBJECT
public:
private:
};
#endif // MAINWINDOW_H
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QtGui/QMainWindow>
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = 0);
~MainWindow();
};
#include <QAbstractItemDelegate>
#include <QFontMetrics>
#include <QModelIndex>
#include <QSize>
#include <QTreeView>
#include <QStyledItemDelegate>
#include <QResizeEvent>
class QAbstractItemModel;
class QObject;
class QPainter;
class HGrid : public QTreeView
{
Q_OBJECT
public:
HGrid(QWidget * parent = 0);
public slots:
void expanding(const QModelIndex & index);
void collapsing(const QModelIndex & index);
private:
QTreeView * tv;
void collapseOthers(QAbstractItemModel * m, QModelIndex sp);
};
class TableDelegate : public QStyledItemDelegate
{
Q_OBJECT
public:
TableDelegate(QObject *parent, QTreeView* child);
void paint(QPainter *painter, const QStyleOptionViewItem &option,
const QModelIndex &index) const;
QSize sizeHint(const QStyleOptionViewItem &option,
const QModelIndex &index ) const;
private:
QTreeView * tv;
};
#endif // MAINWINDOW_H
To copy to clipboard, switch view to plain text mode
and mainwindow.cpp
#include "mainwindow.h"
#include <QTreeView>
#include <QStandardItemModel>
#include <QString>
#include <QPainter>
#include <QDebug>
#include <QScrollBar>
#include <QtGui/QApplication>
const int margin = 12;
const int sMargin = 3;
const int ChildModelRole = Qt::UserRole + 1;
{
/* Register QAbstractItemModel* so we can store it in a QVariant. */
qRegisterMetaType<QAbstractItemModel*>("QAbstractItemModel*");
tv->hide();
TableDelegate * td = new TableDelegate(this, tv);
setItemDelegate(td);
setIndentation(12);
setHorizontalScrollMode(ScrollPerPixel);
setVerticalScrollMode(ScrollPerPixel);
}
{
for (int i = 0; i < m->rowCount(); i++)
if (isExpanded(m->index(i, 0)))
if (m->index(i, 0) != sp)
setExpanded(m->index(i, 0), false);
}
{
/* Hide all the other expanded sections. This way we only need one child tree-view. */
collapseOthers(model(), index);
if (index.model()->data(index.child(0,0), ChildModelRole).isValid())
{
/* Retrieve our child model. */
tv->setModel(qVariantValue<QAbstractItemModel*>(index.model()->data(index.child(0,0), ChildModelRole)));
setFirstColumnSpanned(0, index, true);
viewport()->stackUnder(tv);
tv->show();
}
}
{
tv->hide();
}
: QStyledItemDelegate(parent), tv(child)
{ }
{
if (index.model()->data(index, ChildModelRole).isValid())
{
painter->save();
painter->setBrush(Qt::gray);
painter->setPen(Qt::NoPen);
painter->drawRoundedRect(rc.left() + sMargin, rc.top() + sMargin,
rc.width() - (sMargin * 2), rc.height() - (sMargin * 2),
3.5, 3.5);
painter->restore();
tv->resize(rc.width() - (margin * 2), rc.height() - (margin * 2));
tv->move(rc.left() + margin, rc.top() + margin);
}
else
{
QStyledItemDelegate::paint(painter, option, index);
}
}
{
if (index.model()->data(index, ChildModelRole).isValid())
return QSize(option.
rect.
width(),
200);
else
return QStyledItemDelegate::sizeHint(option, index);
}
MainWindow
::MainWindow(QWidget *parent
){
HGrid * hgrid = new HGrid(this);
/* Our master can have any number of columns. */
for (int i = 0; i < 10; ++i) {
parentItem->appendRow(item);
item->appendRow(ph);
ph->setFlags(Qt::NoItemFlags);
/* Now make a child model to fill our child tree view. */
for (int j = 0; j < 10; j++)
{
QList<QStandardItem*> items;
for (int column = 0; column < 6; ++column)
{
}
m2->invisibleRootItem()->appendRow(items);
}
/* And store a pointer to our child model in the master model. */
m
->setData
(ph
->index
(),
QVariant(QMetaType::type("QAbstractItemModel*"),
&m2
), ChildModelRole
);
}
hgrid->setModel(m);
setCentralWidget(hgrid);
}
MainWindow::~MainWindow()
{ }
int main(int argc, char *argv[])
{
MainWindow w;
w.show();
return a.exec();
}
#include "mainwindow.h"
#include <QTreeView>
#include <QStandardItemModel>
#include <QString>
#include <QPainter>
#include <QDebug>
#include <QScrollBar>
#include <QtGui/QApplication>
const int margin = 12;
const int sMargin = 3;
const int ChildModelRole = Qt::UserRole + 1;
Q_DECLARE_METATYPE( QAbstractItemModel * );
HGrid::HGrid(QWidget * parent) :
QTreeView(parent), tv(0)
{
/* Register QAbstractItemModel* so we can store it in a QVariant. */
qRegisterMetaType<QAbstractItemModel*>("QAbstractItemModel*");
tv = new QTreeView(this->viewport());
tv->hide();
TableDelegate * td = new TableDelegate(this, tv);
connect(this, SIGNAL(expanded(QModelIndex)), SLOT(expanding(const QModelIndex)));
connect(this, SIGNAL(collapsed(QModelIndex)), SLOT(collapsing(const QModelIndex)));
setItemDelegate(td);
setIndentation(12);
setHorizontalScrollMode(ScrollPerPixel);
setVerticalScrollMode(ScrollPerPixel);
}
void HGrid::collapseOthers(QAbstractItemModel * m, QModelIndex sp)
{
for (int i = 0; i < m->rowCount(); i++)
if (isExpanded(m->index(i, 0)))
if (m->index(i, 0) != sp)
setExpanded(m->index(i, 0), false);
}
void HGrid::expanding(const QModelIndex &index)
{
/* Hide all the other expanded sections. This way we only need one child tree-view. */
collapseOthers(model(), index);
if (index.model()->data(index.child(0,0), ChildModelRole).isValid())
{
/* Retrieve our child model. */
tv->setModel(qVariantValue<QAbstractItemModel*>(index.model()->data(index.child(0,0), ChildModelRole)));
setFirstColumnSpanned(0, index, true);
viewport()->stackUnder(tv);
tv->show();
}
}
void HGrid::collapsing(const QModelIndex &index)
{
tv->hide();
}
TableDelegate::TableDelegate(QObject *parent, QTreeView *child)
: QStyledItemDelegate(parent), tv(child)
{ }
void TableDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
const QModelIndex &index) const
{
if (index.model()->data(index, ChildModelRole).isValid())
{
QRect rc = option.rect;
painter->save();
painter->setBrush(Qt::gray);
painter->setPen(Qt::NoPen);
painter->drawRoundedRect(rc.left() + sMargin, rc.top() + sMargin,
rc.width() - (sMargin * 2), rc.height() - (sMargin * 2),
3.5, 3.5);
painter->restore();
tv->resize(rc.width() - (margin * 2), rc.height() - (margin * 2));
tv->move(rc.left() + margin, rc.top() + margin);
}
else
{
QStyledItemDelegate::paint(painter, option, index);
}
}
QSize TableDelegate::sizeHint(const QStyleOptionViewItem & option,
const QModelIndex & index) const
{
if (index.model()->data(index, ChildModelRole).isValid())
return QSize(option.rect.width(), 200);
else
return QStyledItemDelegate::sizeHint(option, index);
}
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
HGrid * hgrid = new HGrid(this);
/* Our master can have any number of columns. */
QStandardItemModel * m = new QStandardItemModel(0, 1);
QStandardItem *parentItem = m->invisibleRootItem();
for (int i = 0; i < 10; ++i) {
QStandardItem *item = new QStandardItem(QString("Parent %0").arg(i));
parentItem->appendRow(item);
QStandardItem * ph = new QStandardItem("Placeholder");
item->appendRow(ph);
ph->setFlags(Qt::NoItemFlags);
/* Now make a child model to fill our child tree view. */
QStandardItemModel * m2 = new QStandardItemModel;
for (int j = 0; j < 10; j++)
{
QList<QStandardItem*> items;
for (int column = 0; column < 6; ++column)
{
items.push_back(new QStandardItem(QString("Item %0").arg(column)));
}
m2->invisibleRootItem()->appendRow(items);
}
/* And store a pointer to our child model in the master model. */
m->setData(ph->index(), QVariant(QMetaType::type("QAbstractItemModel*"), &m2), ChildModelRole);
}
hgrid->setModel(m);
setCentralWidget(hgrid);
}
MainWindow::~MainWindow()
{ }
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
To copy to clipboard, switch view to plain text mode
Bookmarks