OK minorpan! Here we go - the files mytreewidget.hpp and cpp are below.
Launch the application, and then
- click all tree nodes open
- resize the window from the right-bottom corner so that the first leaf node
(containing the text "aa" just above the long line) is on the right-bottom corner.
The horizontal scroll bar "underlines" this line (so to speak).
- then start resizing the window to the right. You know the long line of aaa's
is just below the horizontal scrollbar.
Do this a couple of times. Experiment with very small changes in the height of the
window. I am sure you will get the flicker-effect and your CPU load rises up, right?
- Jorma
// the file mytreewidget.hpp:
#include <QHeaderView>
#include <QTreeWidget>
#include <QApplication>
{
Q_OBJECT
public:
protected slots:
void updateColumnWidth();
};
// the file mytreewidget.hpp:
#include <QHeaderView>
#include <QTreeWidget>
#include <QApplication>
class MyTreeWidget: public QTreeWidget
{
Q_OBJECT
public:
MyTreeWidget(QWidget* parent = 0);
QIcon nodeIcon;
QIcon leafIcon;
virtual void resizeEvent( QResizeEvent* event);
protected slots:
void updateColumnWidth();
};
To copy to clipboard, switch view to plain text mode
// the file mytreewidget.cpp:
#include "mytreewidget.hpp"
{
// setSelectionMode( QAbstractItemView::SingleSelection);
setDragEnabled(true); // inherited from QAbstractItemView
setAcceptDrops(true);
setDropIndicatorShown(true);
setObjectName
(QString::fromUtf8("objectView"));
//setGeometry(QRect(10, 20, 120, 150));
setColumnCount(2);
header()->resizeSection(0, 0);
setItemHidden(headerItem(), true);
nodeIcon.
addPixmap(style
()->standardPixmap
(QStyle::SP_DirClosedIcon),
nodeIcon.
addPixmap(style
()->standardPixmap
(QStyle::SP_DirOpenIcon),
leafIcon.
addPixmap(style
()->standardPixmap
(QStyle::SP_FileIcon));
}
void MyTreeWidget::updateColumnWidth()
{
header()->resizeSection(0, 0);
resizeColumnToContents(0);
}
{
// in addition to plain old resizing update the column widths:
updateColumnWidth(); // minorpan's function
};
// the file mytreewidget.cpp:
#include "mytreewidget.hpp"
MyTreeWidget::MyTreeWidget(QWidget* parent): QTreeWidget(parent)
{
// setSelectionMode( QAbstractItemView::SingleSelection);
setDragEnabled(true); // inherited from QAbstractItemView
setAcceptDrops(true);
setDropIndicatorShown(true);
setObjectName(QString::fromUtf8("objectView"));
//setGeometry(QRect(10, 20, 120, 150));
setColumnCount(2);
header()->resizeSection(0, 0);
setItemHidden(headerItem(), true);
QObject::connect(this, SIGNAL(itemChanged(QTreeWidgetItem*, int)), this, SLOT(updateColumnWidth()));
QObject::connect(this, SIGNAL(itemExpanded(QTreeWidgetItem*)), this, SLOT(updateColumnWidth()));
QObject::connect(this, SIGNAL(itemCollapsed(QTreeWidgetItem*)), this, SLOT(updateColumnWidth()));
nodeIcon.addPixmap(style()->standardPixmap(QStyle::SP_DirClosedIcon),
QIcon::Normal, QIcon::Off);
nodeIcon.addPixmap(style()->standardPixmap(QStyle::SP_DirOpenIcon),
QIcon::Normal, QIcon::On);
leafIcon.addPixmap(style()->standardPixmap(QStyle::SP_FileIcon));
}
void MyTreeWidget::updateColumnWidth()
{
header()->resizeSection(0, 0);
resizeColumnToContents(0);
}
void MyTreeWidget::resizeEvent( QResizeEvent* event)
{
QTreeWidget::resizeEvent( event);
// in addition to plain old resizing update the column widths:
updateColumnWidth(); // minorpan's function
};
To copy to clipboard, switch view to plain text mode
int main(int argc, char* argv[])
{
MyTreeWidget widget;
Qt::ItemFlags flags = pItem->flags() & (~ Qt::ItemIsEditable)| Qt::ItemIsSelectable
| Qt::ItemIsDropEnabled | Qt::ItemIsDragEnabled | Qt::ItemIsEnabled | Qt::ItemIsUserCheckable;
pItem->setFlags(flags);
pItem->setIcon(0, widget.nodeIcon);
pItem->setFlags(flags);pItem->setIcon(0, widget.nodeIcon);
pItem->setFlags(flags);pItem->setIcon(0, widget.nodeIcon);
pItem->setFlags(flags);pItem->setIcon(0, widget.nodeIcon);
pItem->setFlags(flags);pItem->setIcon(0, widget.nodeIcon);
pItem->setFlags(flags);pItem->setIcon(0, widget.nodeIcon);
pItem->setFlags(flags);pItem->setIcon(0, widget.leafIcon);
pItem->setFlags(flags);pItem->setIcon(0, widget.nodeIcon);
pItem->setFlags(flags);pItem->setIcon(0, widget.nodeIcon);
pItem->setFlags(flags);pItem->setIcon(0, widget.nodeIcon);
widget.show();
return app.exec();
}
int main(int argc, char* argv[])
{
QApplication app(argc, argv);
MyTreeWidget widget;
QTreeWidgetItem* pItem;
pItem = new QTreeWidgetItem(&widget, QStringList("a"));
Qt::ItemFlags flags = pItem->flags() & (~ Qt::ItemIsEditable)| Qt::ItemIsSelectable
| Qt::ItemIsDropEnabled | Qt::ItemIsDragEnabled | Qt::ItemIsEnabled | Qt::ItemIsUserCheckable;
pItem->setFlags(flags);
pItem->setIcon(0, widget.nodeIcon);
pItem = new QTreeWidgetItem(pItem, QStringList("aa"));
pItem->setFlags(flags);pItem->setIcon(0, widget.nodeIcon);
pItem = new QTreeWidgetItem(pItem, QStringList("aa"));
pItem->setFlags(flags);pItem->setIcon(0, widget.nodeIcon);
pItem = new QTreeWidgetItem(pItem, QStringList("aa"));
pItem->setFlags(flags);pItem->setIcon(0, widget.nodeIcon);
pItem = new QTreeWidgetItem(pItem, QStringList("aa"));
pItem->setFlags(flags);pItem->setIcon(0, widget.nodeIcon);
pItem = new QTreeWidgetItem(pItem, QStringList("aa"));
pItem->setFlags(flags);pItem->setIcon(0, widget.nodeIcon);
pItem = new QTreeWidgetItem(pItem, QStringList("aa"));
pItem->setFlags(flags);pItem->setIcon(0, widget.leafIcon);
pItem = new QTreeWidgetItem(&widget, QStringList("aaaaaaaaaaaaaaaaaaaaaaaaaaaaa"));
pItem->setFlags(flags);pItem->setIcon(0, widget.nodeIcon);
pItem = new QTreeWidgetItem(pItem, QStringList("aa"));
pItem->setFlags(flags);pItem->setIcon(0, widget.nodeIcon);
pItem = new QTreeWidgetItem(pItem, QStringList("aa"));
pItem->setFlags(flags);pItem->setIcon(0, widget.nodeIcon);
widget.show();
return app.exec();
}
To copy to clipboard, switch view to plain text mode
Bookmarks