class ButtonDelegate : public QStyledItemDelegate
{
Q_OBJECT
public:
explicit ButtonDelegate
(QObject *parent
=0);
~ButtonDelegate();
protected:
private:
};
ButtonDelegate
::ButtonDelegate(QObject *parent
) : QStyledItemDelegate
(parent
){
button->setFixedWidth( 50); // I'm setting this in pixels. There has to be a better way.
button->setPalette(pal);
#if 1
label->setTextFormat(Qt::RichText);
label->setWordWrap(false);
#endif
}
ButtonDelegate::~ButtonDelegate()
{
delete button;
delete label;
}
void ButtonDelegate
::paint( QPainter *painter,
{
bool selected
= option.
state & QStyle::State_Selected;
int yOffset = button->height() < option.rect.height()
? (option.rect.height() - button->height()) / 2 : 0;
QRect labelRect
(option.
rect.
x(),
option.rect.y() + yOffset,
option.rect.width() - button->width(),
option.rect.height());
QRect buttonRect
( option.
rect.
x() + labelRect.
width(),
option.rect.y(),
option.rect.width() - labelRect.width(),
option.rect.height());
label->setFixedSize(qMax(0, labelRect.width()),
labelRect.height());
QString html
= index.
model()->data
(index, Qt
::DisplayRole).
toString();
label->setText(html);
paintWidget(painter, buttonRect, buttonKey, button);
.arg(html)
.arg(labelRect.width())
.arg(labelRect.height());
paintWidget(painter, labelRect, labelKey, label);
}
void ButtonDelegate
::paintWidget( QPainter *painter,
{
{
widget->render(&pixmap);
}
painter->drawPixmap(rect, pixmap);
}
void HistoryWindow::createModelAndView()
{
setupModel();
ui->treeView->setItemDelegateForColumn(0, new ButtonDelegate);
ui->treeView->setAllColumnsShowFocus(true);
ui->treeView->setModel(model);
SetMouseTransitSignals(this);
for ( int i = 0; i < model->rowCount(); ++i )
{
ui
->treeView
->openPersistentEditor
( model
->index
(i,
0,
QModelIndex()) );
}
}
class ButtonDelegate : public QStyledItemDelegate
{
Q_OBJECT
public:
explicit ButtonDelegate(QObject *parent=0);
~ButtonDelegate();
void paint( QPainter *painter,
const QStyleOptionViewItem &option,
const QModelIndex &index) const;
QSize ButtonDelegate::sizeHint( const QStyleOptionViewItem &option,
const QModelIndex &index) const;
void unpolish(QWidget *widget);
void polish(QWidget *widget);
void paintWidget( QPainter *painter,
const QRect &rect,
const QString &cacheKey,
QWidget *widget ) const;
protected:
void enterEvent ( QEvent * );
void leaveEvent ( QEvent * );
private:
mutable QTextDocument document;
QPushButton *button;
QLabel *label;
};
ButtonDelegate::ButtonDelegate(QObject *parent) : QStyledItemDelegate(parent)
{
button = new QPushButton( "Wild!");
button->setFixedWidth( 50); // I'm setting this in pixels. There has to be a better way.
QPalette pal = button->palette();
pal.setColor(QPalette::ButtonText, QColor(255, 0, 0));
pal.setColor(QPalette::Button, QColor(255, 255, 0));
button->setPalette(pal);
#if 1
label = new QLabel;
label->setTextFormat(Qt::RichText);
label->setWordWrap(false);
#endif
}
ButtonDelegate::~ButtonDelegate()
{
delete button;
delete label;
}
void ButtonDelegate::paint( QPainter *painter,
const QStyleOptionViewItem &option,
const QModelIndex &index) const
{
bool selected = option.state & QStyle::State_Selected;
int yOffset = button->height() < option.rect.height()
? (option.rect.height() - button->height()) / 2 : 0;
QRect labelRect(option.rect.x(),
option.rect.y() + yOffset,
option.rect.width() - button->width(),
option.rect.height());
QRect buttonRect( option.rect.x() + labelRect.width(),
option.rect.y(),
option.rect.width() - labelRect.width(),
option.rect.height());
label->setFixedSize(qMax(0, labelRect.width()),
labelRect.height());
QString html = index.model()->data(index, Qt::DisplayRole).toString();
label->setText(html);
QString buttonKey("PUSHBUTTON");
paintWidget(painter, buttonRect, buttonKey, button);
QString labelKey = QString("LABEL:%1.%2.%3x%4").arg(selected)
.arg(html)
.arg(labelRect.width())
.arg(labelRect.height());
paintWidget(painter, labelRect, labelKey, label);
}
void ButtonDelegate::paintWidget( QPainter *painter,
const QRect &rect,
const QString &cacheKey,
QWidget *widget ) const
{
QPixmap pixmap(widget->size());
if (!QPixmapCache::find(cacheKey, &pixmap))
{
widget->render(&pixmap);
QPixmapCache::insert(cacheKey, pixmap);
}
painter->drawPixmap(rect, pixmap);
}
void HistoryWindow::createModelAndView()
{
setupModel();
ui->treeView->setItemDelegateForColumn(0, new ButtonDelegate);
ui->treeView->setAllColumnsShowFocus(true);
ui->treeView->setModel(model);
SetMouseTransitSignals(this);
for ( int i = 0; i < model->rowCount(); ++i )
{
ui->treeView->openPersistentEditor( model->index(i, 0, QModelIndex()) );
}
}
To copy to clipboard, switch view to plain text mode
void HistoryWindow::createModelAndView()
{
setupModel();
ui->treeView->setItemDelegateForColumn(0, new ButtonDelegate);
ui->treeView->setAllColumnsShowFocus(true);
ui->treeView->setModel(model);
SetMouseTransitSignals(this);
for ( int i = 0; i < model->rowCount(); ++i )
{
ui
->treeView
->openPersistentEditor
( model
->index
(i,
0,
QModelIndex()) );
}
}
void HistoryWindow::createModelAndView()
{
setupModel();
ui->treeView->setItemDelegateForColumn(0, new ButtonDelegate);
ui->treeView->setAllColumnsShowFocus(true);
ui->treeView->setModel(model);
SetMouseTransitSignals(this);
for ( int i = 0; i < model->rowCount(); ++i )
{
ui->treeView->openPersistentEditor( model->index(i, 0, QModelIndex()) );
}
}
To copy to clipboard, switch view to plain text mode
I would have thought the following code would have caused a button to appear in on only the row where the mouse is hovering. It doesn't work as I expected. closePersistentEditor(), which is called from within a 'for loop', does a fine job of making all buttons disappear but, for some reason the following call to openPersistentEditor() unexpectedly fails to cause any buttons to reappear. Why would the call to openPersistentEditor() not have an effect?
void HistoryTreeView
::mouseMoveEvent(QMouseEvent *event
) {
// Only do something when a model is set.
if (m)
{
if (index.isValid())
{
// if the mouse has moved to another row
if (index.row() != m_currentRow)
{
m_currentRow = index.row();
// clear buttons from all rows
for ( int i = 0; i < m->rowCount(); ++i )
{
this
->closePersistentEditor
( m
->index
(i,
0,
QModelIndex()) );
}
// create button in mouse's current row
this->openPersistentEditor(m->index(m_currentRow, 0, index ));
}
}
else // model is invalid
{
m_currentRow = -1;
}
}
}
void HistoryTreeView::mouseMoveEvent(QMouseEvent *event)
{
QAbstractItemModel *m(model());
// Only do something when a model is set.
if (m)
{
QModelIndex index = this->indexAt(event->pos());
if (index.isValid())
{
// if the mouse has moved to another row
if (index.row() != m_currentRow)
{
m_currentRow = index.row();
// clear buttons from all rows
for ( int i = 0; i < m->rowCount(); ++i )
{
this->closePersistentEditor( m->index(i, 0, QModelIndex()) );
}
// create button in mouse's current row
this->openPersistentEditor(m->index(m_currentRow, 0, index ));
}
}
else // model is invalid
{
m_currentRow = -1;
}
}
QTreeView::mouseMoveEvent(event);
}
To copy to clipboard, switch view to plain text mode
Bookmarks