#include <QtGui>
{
Q_OBJECT
public:
{
connect(this, SIGNAL(sectionClicked(int)), this, SLOT(clickedSection(int)));
}
{
if(visualIndexAt(event->pos().x()) == 1) {
setClickable(true);
setSortIndicator(1, sortIndicatorOrder());
setSortIndicatorShown(true);
} else {
setSortIndicatorShown(false);
resizeSection(1, sectionSize(1) - 1);
resizeSection(1, sectionSize(1) + 1);
}
}
{
setClickable(false);
}
public slots:
void clickedSection(int s)
{
qDebug() << "Section " << s << " clicked";
}
};
{
public:
TreeWidget()
{
setColumnCount(3);
list << "a" << "b" << "c";
list2 << "d" << "e" << "f";
addTopLevelItem(item1);
addTopLevelItem(item2);
setHeader(new HeaderView(this));
}
};
#include "main.moc"
int main(int argc, char** argv)
{
TreeWidget tree;
tree.show();
return app.exec();
}
#include <QtGui>
class HeaderView : public QHeaderView
{
Q_OBJECT
public:
HeaderView(QWidget *parent = 0) : QHeaderView(Qt::Horizontal, parent)
{
connect(this, SIGNAL(sectionClicked(int)), this, SLOT(clickedSection(int)));
}
void mousePressEvent(QMouseEvent *event)
{
if(visualIndexAt(event->pos().x()) == 1) {
setClickable(true);
setSortIndicator(1, sortIndicatorOrder());
setSortIndicatorShown(true);
} else {
setSortIndicatorShown(false);
resizeSection(1, sectionSize(1) - 1);
resizeSection(1, sectionSize(1) + 1);
}
QHeaderView::mousePressEvent(event);
}
void mouseReleaseEvent(QMouseEvent *event)
{
QHeaderView::mouseReleaseEvent(event);
setClickable(false);
}
public slots:
void clickedSection(int s)
{
qDebug() << "Section " << s << " clicked";
}
};
class TreeWidget : public QTreeWidget
{
public:
TreeWidget()
{
setColumnCount(3);
QStringList list;
list << "a" << "b" << "c";
QStringList list2;
list2 << "d" << "e" << "f";
QTreeWidgetItem *item1 = new QTreeWidgetItem(this, list);
QTreeWidgetItem *item2 = new QTreeWidgetItem(this, list2);
addTopLevelItem(item1);
addTopLevelItem(item2);
setHeader(new HeaderView(this));
}
};
#include "main.moc"
int main(int argc, char** argv)
{
QApplication app(argc, argv);
TreeWidget tree;
tree.show();
return app.exec();
}
To copy to clipboard, switch view to plain text mode
Bookmarks