Hi again.
The problem is not in creating the delegate. I've one and it works. The problem is that that in the QTableView I use items, sometimes QLineEdit and sometimes QComboBox items that are created in the delegate. The issue is really that I can't seem to getI the QLineEdit's and QComboBox's specific signals that are emitted when the user is doing things in them (selecting item, etc).
Code example: Sorry if I'm writing this, maybe it's better to include files?
UIQtCookBook::setupItemViews()
{
/* Other code here... setting the Model for the view etc */
// Setting the delegate for the view
IngredientTableDelegate *ingredientTableDelegate = new IngredientTableDelegate();
ingredientsTableView->setItemDelegate( ingredientTableDelegate);
}
// Other file.
// IngredientTableDelegate is subclass of QItemDelegate
//
QWidget* IngredientTableDelegate
::createEditor( QWidget* parent,
const QStyleOptionViewItem
&,
const QModelIndex
& index
) const {
if( checkIfCombo() )
{
QVariant value
= model
->data
( index, Qt
::DisplayRole );
// Add the list of measurements to the QComboBox
QString measurement
= value.
toString();
box->addItems( m_measurements );
editor = box;
// The connection below does not work
connect(box, SIGNAL(activated(int)), this, SLOT(setTestPrintout(int)));
}
else
{
QVariant value
= model
->data
( index, Qt
::DisplayRole );
editor = editLine;
}
editor->installEventFilter(const_cast<IngredientTableDelegate*>(this));
return editor;
}
UIQtCookBook::setupItemViews()
{
/* Other code here... setting the Model for the view etc */
// Setting the delegate for the view
IngredientTableDelegate *ingredientTableDelegate = new IngredientTableDelegate();
ingredientsTableView->setItemDelegate( ingredientTableDelegate);
}
// Other file.
// IngredientTableDelegate is subclass of QItemDelegate
//
QWidget* IngredientTableDelegate::createEditor( QWidget* parent, const QStyleOptionViewItem&, const QModelIndex& index ) const
{
QWidget* editor = 0;
if( checkIfCombo() )
{
editor = new QComboBox (parent);
const QAbstractItemModel* model = index.model();
QVariant value = model->data( index, Qt::DisplayRole );
// Add the list of measurements to the QComboBox
QString measurement= value.toString();
QComboBox* box = new QComboBox( parent );
box->addItems( m_measurements );
editor = box;
// The connection below does not work
connect(box, SIGNAL(activated(int)), this, SLOT(setTestPrintout(int)));
}
else
{
editor = new QLineEdit( "-na-", parent);
const QAbstractItemModel* model = index.model();
QVariant value = model->data( index, Qt::DisplayRole );
QLineEdit* editLine = new QLineEdit(parent);
editor = editLine;
}
editor->installEventFilter(const_cast<IngredientTableDelegate*>(this));
return editor;
}
To copy to clipboard, switch view to plain text mode
So far so good. But what I really what to do is to catch the activated signal from QComboBox items, basically I wan't to call a specific function when a certain value in the QComboBox is chosen. I do not seem to be able to do that.
I.e. the bottom line is. How can I connect the signals from the dynamically created QWidgets in createEditor above to another function in this class. I've tried (naive approach?) to do it in the createEditor function, but I do not get the expected result (see the comment in the code --- the printout function is never called)
Bookmarks