// ButtonDelegate.h
class ButtonDelegate : public QStyledItemDelegate
{
Q_OBJECT
public:
explicit ButtonDelegate
(QObject *parent
=0);
~ButtonDelegate();
protected:
private:
};
// ButtonDelegate.cpp
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);
label->setTextFormat(Qt::RichText);
label->setWordWrap(false);
}
//HistoryTreeView.h
{
public:
HistoryTreeView
(QWidget *parent
= 0);
protected:
void enterEvent
( QEvent * event
);
void leaveEvent
( QEvent * event
);
private:
int m_currentRow;
};
//HistoryTreeView.cpp
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();
// TODO: clear all rows
QModelIndex itemIndex
= m
->sibling
(m_currentRow,
0, index
);
QWidget * item
= indexWidget
(itemIndex
);
Q_ASSERT(item != NULL );
}
}
else // model is invalid
{
m_currentRow = -1;
}
}
}
//HistoryWindow.h
{
Q_OBJECT
public:
explicit HistoryWindow
(QWidget *parent
= 0);
~HistoryWindow();
void setupModel();
void SetMouseTransitSignals
(QWidget *parent
);
protected:
private:
Ui::HistoryWindow *ui;
HistoryItemModel *model;
HistoryTreeView *treeView;
void setupData();
void createModelAndView();
};
//HistoryWindow.cpp
void HistoryWindow::createModelAndView()
{
setupModel();
ui->treeView->setItemDelegateForColumn(0, new ButtonDelegate);
ui->treeView->setAllColumnsShowFocus(true);
ui->treeView->setModel(model);
SetMouseTransitSignals(this);
}
void HistoryWindow
::SetMouseTransitSignals(QWidget *parent
) {
// enable mouse tracking for the treeview.
ui->treeView->setMouseTracking(true);
}
void HistoryWindow
::enterEvent(QEvent*e
) {
}
// ButtonDelegate.h
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.cpp
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);
label = new QLabel;
label->setTextFormat(Qt::RichText);
label->setWordWrap(false);
}
//HistoryTreeView.h
class HistoryTreeView : public QTreeView
{
public:
HistoryTreeView(QWidget *parent = 0);
protected:
virtual void mouseMoveEvent(QMouseEvent *event);
void enterEvent( QEvent * event );
void leaveEvent( QEvent * event );
private:
int m_currentRow;
};
//HistoryTreeView.cpp
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();
// TODO: clear all rows
QModelIndex itemIndex = m->sibling(m_currentRow, 0, index );
QWidget * item = indexWidget(itemIndex);
Q_ASSERT(item != NULL );
}
}
else // model is invalid
{
m_currentRow = -1;
}
}
QTreeView::mouseMoveEvent(event);
}
//HistoryWindow.h
class HistoryWindow : public QMainWindow
{
Q_OBJECT
public:
explicit HistoryWindow(QWidget *parent = 0);
~HistoryWindow();
void setupModel();
void addFile( QStandardItemModel *model,
const QString &anAttribute,
const QString &anotherAttrib,
const QString &path,
const QString &datestamp,
const QString &text );
void SetMouseTransitSignals(QWidget *parent);
protected:
void enterEvent(QEvent*);
void leaveEvent(QEvent*);
private:
Ui::HistoryWindow *ui;
HistoryItemModel *model;
HistoryTreeView *treeView;
void setupData();
void createModelAndView();
};
//HistoryWindow.cpp
void HistoryWindow::createModelAndView()
{
setupModel();
ui->treeView->setItemDelegateForColumn(0, new ButtonDelegate);
ui->treeView->setAllColumnsShowFocus(true);
ui->treeView->setModel(model);
SetMouseTransitSignals(this);
}
void HistoryWindow::SetMouseTransitSignals(QWidget *parent)
{
// enable mouse tracking for the treeview.
ui->treeView->setMouseTracking(true);
}
void HistoryWindow::enterEvent(QEvent*e)
{
QWidget::enterEvent(e);
}
To copy to clipboard, switch view to plain text mode
Bookmarks