Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Solved QStyledItemDelegate. Cell isn't selectable

    General and Desktop
    3
    4
    100
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • B
      BrMisha last edited by

      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)))

      1 Reply Last reply Reply Quote 0
      • Christian Ehrlicher
        Christian Ehrlicher Lifetime Qt Champion last edited by

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

        Where is my misstate?

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

        Qt has to stay free or it will die.

        1 Reply Last reply Reply Quote 3
        • eyllanesc
          eyllanesc last edited by eyllanesc

          @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;
          	}
          }
          

          If you want me to help you develop some work then you can write to my email: e.yllanescucho@gmal.com.

          1 Reply Last reply Reply Quote 2
          • B
            BrMisha last edited by

            Thanks, men! You solved my problem)))

            1 Reply Last reply Reply Quote 0
            • First post
              Last post