Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QStyledItemDelegate. Cell isn't selectable



  • Hi!
    I'm using QStyledItemDelegate as delegate for view for some columns.
    If i process a cell in QStyledItemDelegate::paint then that isn't selectable.
    Code:

    void EditModeWidgetsDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
    {
    	switch (static_cast<EditModeWidgetsModel::Column>(index.column())) {
    		case EditModeWidgetsModel::Column::X: [[fallthrough]];
    		case EditModeWidgetsModel::Column::Y: [[fallthrough]];
    		case EditModeWidgetsModel::Column::Width: [[fallthrough]];
    		case EditModeWidgetsModel::Column::Height: {
    			QStyleOptionMenuItem	mItem;
    			mItem.text = QString::number(index.data().toReal(), 'f', 2);
    			mItem.rect = option.rect;
    			QApplication::style()->drawControl(QStyle::CE_MenuBarItem, &mItem, painter);
    		} break;
    		default: QStyledItemDelegate::paint(painter, option, index);
    	}
    }
    

    Where is my misstate?
    Thanks)))


  • Qt Champions 2019

    @BrMisha said in QStyledItemDelegate. Cell isn't selectable:

    Where is my misstate?

    You're not copying the style options from the given option.



  • @BrMisha sThe paint method invokes initStyleOption which loads configurations as if the item is selected or not, etc, but you don't use it causing that error. If you just want to change the text then just override initStyleOption:

    void EditModeWidgetsDelegate::initStyleOption(QStyleOptionViewItem *option, const QModelIndex &index) const
    {
           QStyledItemDelegate::initStyleOption(option, index);
    	switch (static_cast<EditModeWidgetsModel::Column>(index.column())) {
    		case EditModeWidgetsModel::Column::X: [[fallthrough]];
    		case EditModeWidgetsModel::Column::Y: [[fallthrough]];
    		case EditModeWidgetsModel::Column::Width: [[fallthrough]];
    		case EditModeWidgetsModel::Column::Height: {
    			option->text = QString::number(index.data().toReal(), 'f', 2);
    		} break;
    	}
    }
    


  • Thanks, men! You solved my problem)))


Log in to reply