Alundra
7th January 2015, 04:28
Hi,
I want to add an additional icon in the QTreeItem to have option to set something visible or not when clicking on the icon.
I have tried the delegate but the text of the item go behind the icon and I didn't found a way to have the "..." before the icon.
Here the code of the delegate :
class CItemDelegate : public QStyledItemDelegate
{
Q_OBJECT
public:
CItemDelegate( QObject* Parent = NULL ) :
QStyledItemDelegate( Parent )
{
m_VisibleIcon = QPixmap( QIcon( "Images/Visible.png" ).pixmap( 20, 20 ) );
}
void paint( QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index ) const
{
QStyledItemDelegate::paint( painter, option, index );
painter->drawPixmap( ComputeVisibleIconPos( option ), m_VisibleIcon );
}
bool editorEvent( QEvent* event, QAbstractItemModel* model, const QStyleOptionViewItem& option, const QModelIndex& index )
{
if( event->type() == QEvent::MouseButtonRelease )
{
QMouseEvent* MouseEvent = static_cast< QMouseEvent* >( event );
const QRect VisibleButtonRect = m_VisibleIcon.rect().translated( ComputeVisibleIconPos( option ) );
if( VisibleButtonRect.contains( MouseEvent->pos() ) )
emit VisibleIconClicked( index );
}
return false;
}
signals:
void VisibleIconClicked( const QModelIndex& );
private:
QPoint ComputeVisibleIconPos( const QStyleOptionViewItem& option ) const
{
return QPoint( option.rect.right() - m_VisibleIcon.width() - 2,
option.rect.center().y() - m_VisibleIcon.height() / 2 );
}
private:
QPixmap m_VisibleIcon;
};
The second option is to have 2 columns but I didn't found a way to have the second column fixed width and the first column stretch correctly.
The second column could have a QToolButton with a stylesheet on it to only show the image.
I tried to have the icon on the first column and text on the second but decoration didn't worked good because I didn't found a way to have it on the second column.
The delegate version is more clean because a no name column is not needed.
How can I have the "..." before the icon ?
If the solution is to have icon before the icon already on the item, i like the solution but I don't know how do it.
Thanks for the help
I want to add an additional icon in the QTreeItem to have option to set something visible or not when clicking on the icon.
I have tried the delegate but the text of the item go behind the icon and I didn't found a way to have the "..." before the icon.
Here the code of the delegate :
class CItemDelegate : public QStyledItemDelegate
{
Q_OBJECT
public:
CItemDelegate( QObject* Parent = NULL ) :
QStyledItemDelegate( Parent )
{
m_VisibleIcon = QPixmap( QIcon( "Images/Visible.png" ).pixmap( 20, 20 ) );
}
void paint( QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index ) const
{
QStyledItemDelegate::paint( painter, option, index );
painter->drawPixmap( ComputeVisibleIconPos( option ), m_VisibleIcon );
}
bool editorEvent( QEvent* event, QAbstractItemModel* model, const QStyleOptionViewItem& option, const QModelIndex& index )
{
if( event->type() == QEvent::MouseButtonRelease )
{
QMouseEvent* MouseEvent = static_cast< QMouseEvent* >( event );
const QRect VisibleButtonRect = m_VisibleIcon.rect().translated( ComputeVisibleIconPos( option ) );
if( VisibleButtonRect.contains( MouseEvent->pos() ) )
emit VisibleIconClicked( index );
}
return false;
}
signals:
void VisibleIconClicked( const QModelIndex& );
private:
QPoint ComputeVisibleIconPos( const QStyleOptionViewItem& option ) const
{
return QPoint( option.rect.right() - m_VisibleIcon.width() - 2,
option.rect.center().y() - m_VisibleIcon.height() / 2 );
}
private:
QPixmap m_VisibleIcon;
};
The second option is to have 2 columns but I didn't found a way to have the second column fixed width and the first column stretch correctly.
The second column could have a QToolButton with a stylesheet on it to only show the image.
I tried to have the icon on the first column and text on the second but decoration didn't worked good because I didn't found a way to have it on the second column.
The delegate version is more clean because a no name column is not needed.
How can I have the "..." before the icon ?
If the solution is to have icon before the icon already on the item, i like the solution but I don't know how do it.
Thanks for the help