PDA

View Full Version : subclass qStyledItemDelegate signal itemActivated not triggered



jcr
20th October 2010, 01:56
Hello,

I subclassed QStyledItemDelegate and set that to my QListWidget. To my surprise, the connect between signal itemActivated(QListWidgetItem*) and the slot event_refresh(QListWidgetItem*) is broken.
I suspect the issue is with the signal because the connect was working before I added the subclass of QStyledItemDelegate.
Any idea of what the problem might be?

Thanks!

I have

events_widget = new QListWidget;
events_widget->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOf f);
events_widget->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff) ;
events_widget->setItemDelegate(new event_delegate);
connect(events_widget, SIGNAL(itemActivated(QListWidgetItem*)),
this, SLOT(event_refresh(QListWidgetItem*)))
but it looks like the itemActivated signal is not triggered any more but it was working before I set the ItemDelegate.

My event_delegate implementation looks like that:


event_delegate::event_delegate(QObject *parent) :
QStyledItemDelegate(parent)
{
}
void event_delegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
if (qVariantCanConvert<EventOne>(index.data()))
{
EventOne eventOne = qVariantValue<EventOne>(index.data());
if (option.state & QStyle::State_Selected)
{
painter->fillRect(option.rect, option.palette.highlight());
}
eventOne.paint(painter, option.rect, option.palette);
}
else
{
QStyledItemDelegate::paint(painter, option, index);
}
}
QSize event_delegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
{
if (qVariantCanConvert<EventOne>(index.data()))
{
EventOne eventOne = qVariantValue<EventOne>(index.data());
return eventOne.sizeHint();
}
else
{
return QStyledItemDelegate::sizeHint(option, index);
}
}

the class EventOne is like this:


EventOne::EventOne(QString description_, QString date_, QString capacity_)
: description(description_), date(date_), capacity(capacity_)
{
}
void EventOne::paint(QPainter *painter, const QRect &rect, const QPalette &palette) const
{
//qDebug() << "|" << rect << "|";
painter->save();
painter->setRenderHint(QPainter::Antialiasing, true);
painter->drawText(10,50+30*rect.y(), date);
painter->restore();
}
QSize EventOne::sizeHint() const
{
return QSize(1,1);
}

Could it be that the events are stopped by the new delegate. Is there something I should add somewhere?

Thanks!

jcr
20th October 2010, 04:06
Hi,

The problem is in the sizeHint() function of my EventOne class.
By returning QSize(1), I think I am making the area able to capture the itemActivated way too small; and that is why the signal is not triggered. Now, the issue is how to write that function.

jcr
20th October 2010, 17:16
Hello,
Maybe someone could help me clarifying what the sizeHint function does and how it is related to the view's signals.
My implementation of EventOne now looks like this:


EventOne::EventOne(QString description_, QString date_, QString capacity_)
: description(description_), date(date_), capacity(capacity_)
{
}
void EventOne::paint(QPainter *painter, const QRect &rect, const QPalette &palette, const QModelIndex& index) const
{
painter->save();
painter->setRenderHint(QPainter::Antialiasing, true);
painter->drawText(0,70 + rect.y()* 35,date);
//painter->translate(1.0, 0.0);
painter->restore();
}
QSize EventOne::sizeHint(const QRect& rect, const QModelIndex &index) const
{
return QSize(1,1);
//return QSize(rect.width(),30);
//return QSize(300,30);
}

like this I can see the rows of data. But, those rows cannot be activated. The "activated rows are actually stacked at the top of the widget. if I change the sizeHint function to increase the area, I don't see the rows properly but I can activate a row and click it and get to the next screen.
How can I have it work?

Thanks!