PDA

View Full Version : qt and custom delegate



giugio
11th November 2012, 17:05
hello.
I have implemented a custom delegate for the extracted widgetbox of the qt designer.
The widgets in the widgetbox are buttons with an icon
My problem is that i must implement a special behaviour for the widgets.
in short when i press a widget i must first disable all the widget in the widgetbox without the selected widget then select the selected widget .
that is the problem:
the "paint" function that draw all the widgets is called only if a widged is dirty or has need to be drawed.
But I need to redraw all the widgets for each time i press a widget(that becomes the selected widget).
thanks.

this is the code


CustomItemDelegate::CustomItemDelegate(QObject *parent) :
QItemDelegate(parent)
{
_state = QStyle::State_Enabled;

}


void CustomItemDelegate::paint(QPainter *painter,
const QStyleOptionViewItem &option,
const QModelIndex &index) const
{
const QSortFilterProxyModel* model =
static_cast<const QSortFilterProxyModel*>(index.model());

QDesignerWidgetBoxInterface::Widget widget =((WidgetBoxCategoryModel*)model->sourceModel())->widgetAt(index);

QString strIcon = widget.iconName();
QString text = widget.name();
QRect rect = option.rect;
QRect buttonRect( rect);
buttonRect.setWidth( 30);

QStyleOptionButton button;
QIcon pq(strIcon);

button.icon = pq;
button.iconSize = QSize(22, 22);
button.palette = option.palette;

button.rect = buttonRect;
//button.text = text;
button.state = _state | QStyle::State_Enabled;

QApplication::style()->drawControl
(QStyle::CE_PushButton, &button, painter);
}

QSize CustomItemDelegate::sizeHint(const QStyleOptionViewItem &/*option*/,
const QModelIndex &/*index*/) const
{
//hard coding size for test purpose,
//actual size hint can be calculated from option param
return QSize(800,30);
}

bool CustomItemDelegate::editorEvent(QEvent *event,
QAbstractItemModel *model,
const QStyleOptionViewItem &option,
const QModelIndex &index)
{
const QSortFilterProxyModel* modelx =
static_cast<const QSortFilterProxyModel*>(index.model());

QDesignerWidgetBoxInterface::Widget widget =((WidgetBoxCategoryModel*)modelx->sourceModel())->widgetAt(index);

m_strName = widget.name();

if( event->type() == QEvent::MouseButtonPress ||
event->type() == QEvent::MouseButtonRelease ) {
} else {
//ignoring other mouse event and reseting button's state
_state = QStyle::State_Raised;
return true;
}

QRect buttonRect( option.rect);
//buttonRect.setY(option.rect.y()+ 35);
//buttonRect.setHeight( 30);
buttonRect.setWidth( 30);
QMouseEvent* mouseEvent = static_cast<QMouseEvent*>(event);
if( !buttonRect.contains( mouseEvent->pos()) ) {
_state = QStyle::State_Raised;
return true;
}


if(widget.getFunction() == QDesignerWidgetBoxInterface::Widget::Creator)
{
if(m_nOldName != widget.name() && m_nOldName != ""){
_state = QStyle::State_Raised;
return true;
}

if( event->type() == QEvent::MouseButtonPress){

//if(_state == QStyle::State_Sunken)
// _state = QStyle::State_Raised;
//else
// _state = QStyle::State_Sunken;
}
else if( event->type() == QEvent::MouseButtonRelease) {
if(_state == QStyle::State_Sunken){
_state = QStyle::State_Raised;
}else{
_state = QStyle::State_Sunken;
m_nOldName = widget.name();
}

}

}


else if( event->type() == QEvent::MouseButtonPress) {
_state = QStyle::State_Sunken;
} else if( event->type() == QEvent::MouseButtonRelease) {
_state = QStyle::State_Raised;
emit buttonClicked( index);
emit pressDel(m_strName);
}
return true;
}



thanks.