yea, using pushbutton looks good... but u have to make extra connections for buttons clicked... isnt it ?
With delegates you wont have to both so much...
Just made a simple delegate u can start with...
{
protected:
{
if(index.child(0,0).isValid())
{
painter->setPen(Qt::blue);
//painter->drawRect(option.rect);
opt.rect = option.rect;
qApp
->style
()->drawControl
(QStyle::CE_PushButton,
&opt,painter
);
}
}
};
class TreeDelegate : public QItemDelegate
{
protected:
virtual void paint ( QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index ) const
{
if(index.child(0,0).isValid())
{
painter->setPen(Qt::blue);
//painter->drawRect(option.rect);
QStyleOptionButton opt;
opt.rect = option.rect;
qApp->style()->drawControl(QStyle::CE_PushButton, &opt,painter);
}
QItemDelegate::paint(painter,option,index);
}
virtual void drawCheck ( QPainter * painter, const QStyleOptionViewItem & option, const QRect & rect, Qt::CheckState state ) const {};
};
To copy to clipboard, switch view to plain text mode
You will need to call view->setItemDelegate(new TreeWidgetDelegate());
thats it.
also to achieve expand/collapse on single click like in designer, you can connect itemClicked signal to some function of yours..
Like ,,,
MainWindow::MainWindow()
{
ui.setupUi(this);
ui.treeWidget->expandAll();
ui.treeWidget->setItemDelegate(new TreeDelegate());
ui.treeWidget->setRootIsDecorated(false);
}
{
item->setSelected(false);
item->setExpanded(!item->isExpanded());
}
MainWindow::MainWindow()
{
ui.setupUi(this);
ui.treeWidget->expandAll();
connect(ui.treeWidget,SIGNAL(itemPressed(QTreeWidgetItem*,int)),this,SLOT(itemClicked(QTreeWidgetItem*)));
ui.treeWidget->setItemDelegate(new TreeDelegate());
ui.treeWidget->setRootIsDecorated(false);
}
void MainWindow::itemClicked(QTreeWidgetItem* item)
{
item->setSelected(false);
item->setExpanded(!item->isExpanded());
}
To copy to clipboard, switch view to plain text mode
One thing more to be noted is in Qt 4.5 designer has icon view too... so not sure if they have used tree widget for that... May be they might have used treewidget, with one parent one child... and the child must be QListWidget...
Bookmarks