No the button is not initialized at this time. I thought that just the name of the pointer was sufficient for the connect() function. I gather from your question that I didn't have that right.
this button is instantiated during the lifetime of the application if and only if the user hovers his mouse over a row in a treeview. Here is the code:
{
// if the current column is the 'share' button's column
if ( index.column() == buttonColumn )
{ // create the button that will be located in this view item
}
else
{
return QStyledItemDelegate::createEditor(parent, option, index );
}
}
QWidget * ButtonDelegate::createEditor( QWidget * parent,
const QStyleOptionViewItem & option,
const QModelIndex & index ) const
{
// if the current column is the 'share' button's column
if ( index.column() == buttonColumn )
{ // create the button that will be located in this view item
return new QPushButton("Share", parent);
}
else
{
return QStyledItemDelegate::createEditor(parent, option, index );
}
}
To copy to clipboard, switch view to plain text mode
Now, as I understand it, createEditor() is invoked when openPersistentEditor() is invoked. Another detail, the spec calls for a 'hidden' button to be located in every cell in column zero of a spreadsheet-like view. I handle all of this in a mouseMoveEvent():
{
// 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 on mouse's current row
this
->openPersistentEditor
(m
->index
(m_currentRow,
0,
QModelIndex() ));
}
}
else // model is invalid
{
m_currentRow = -1;
}
}
}
void MyTreeView::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 on mouse's current row
this->openPersistentEditor(m->index(m_currentRow, 0, QModelIndex() ));
}
}
else // model is invalid
{
m_currentRow = -1;
}
}
QTreeView::mouseMoveEvent(event);
}
To copy to clipboard, switch view to plain text mode
One other detail: The buttonDelegate class has a button pointer:
class ButtonDelegate : public QStyledItemDelegate
{
Q_OBJECT
public:
explicit ButtonDelegate
(QObject *parent
=0);
~ButtonDelegate();
// code redacted for bevity
private:
};
class ButtonDelegate : public QStyledItemDelegate
{
Q_OBJECT
public:
explicit ButtonDelegate(QObject *parent=0);
~ButtonDelegate();
// code redacted for bevity
private:
QPushButton *button;
};
To copy to clipboard, switch view to plain text mode
I'm faintly embarrassed to admit that I didn't notice the button pointer has evidently fallen out of use. This code had been through a number of major changes before wysota guided me to a solution that seemed to work. But that solution is rather different than my initial approach. It seems that the framework holds the pointer to the button for the duration of the editor's life.
But, on the other hand, if I comment-out the references to the 'button pointer' the app crashes, and I don't know why.
OK, Timoteo, based on your comment it appears that I need to create the connection that uses the button click as a signal in mouseMoveEvent(), just after the return from openPersistentEditor(). If so, does this mean that disconnect() should be called after closePersistentEditor() is called? I guess it would.
Bookmarks