February 11, 2011

maxomato maxomato
Lab Rat
5 posts

change item’s color in table-view on click

 

Hi

I have table-view and I want to change color of item with help QColorDialog after click on it.

I created subclass of QItemDelegate and overrided functions createEditor and setModelData

  1. QWidget *ColorItemDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &,
  2.                       const QModelIndex &) const
  3. {
  4.  
  5.     QColorDialog* colorDialog = new QColorDialog();
  6.     return colorDialog;
  7. }
  8.  
  9.  
  10. void ColorItemDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
  11.                   const QModelIndex &index) const
  12. {
  13.     QColorDialog* colorDialog = static_cast<QColorDialog>( editor );
  14.     QColor color = colorDialog->selectedColor();
  15.     if ( color.isValid() )
  16.         model->setData(index, color, Qt::EditRole);
  17. }

But it doesn’t work because of model-view system considers editor widget as simple widget not dialog. And color in

  1.  QColor color = colorDialog->selectedColor();

is always not valid.

What are there adequate methods to solve this problem?

4 replies

February 11, 2011

Gerolf Gerolf
Area 51 Engineer
3210 posts

you could try to create a “color edit fields”, which has a color field and a button (like a combo box) and open the dialog from there. It could perhaps work, esopecially, as QComboBox also works.

 Signature 

Nokia Certified Qt Specialist.
Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

February 11, 2011

Volker Volker
Robot Herder
5428 posts

On our application we have similar needs (dialog to edit an item index). We do it this way:

  • in the delegate check in createEditor(), if a dialog is needed for editing. If yes, return a null pointer (that restrains the view from editing the index); if no return the base class’ implementation
  • connect signal QAbstractItemView::doubleClicked() [doc.qt.nokia.com] (or some overload signal from the specialized item views) to a slot
  • in that slot, check if the dialog is needed, if yes create it, show it with exec() and set the value if it returns with “OK”

This way you still have inline editing for text fields, and create a dialog for the complex editing.

February 22, 2011

maxomato maxomato
Lab Rat
5 posts

Hi!

Volker, I implemented this idea.

I created model’s class

  1. class XPenTableModel : public QAbstractTableModel
  2. {
  3.     Q_OBJECT
  4. public:
  5.  
  6.     XPenTableModel( QObject* parent = NULL );
  7.  
  8.     QVariant headerData(int section, Qt::Orientation orientation, int role) const;
  9.  
  10.     int rowCount(const QModelIndex &parent = QModelIndex()) const;
  11.  
  12.     int columnCount(const QModelIndex &parent = QModelIndex()) const;
  13.  
  14.     QVariant data(const QModelIndex &index, int role) const;
  15.  
  16.     bool setData(const QModelIndex &index, const QVariant &value, int role);
  17.  
  18.     Qt::ItemFlags flags(const QModelIndex &index) const;
  19.  
  20. public slots:
  21.      void showDialogIfNeed( QModelIndex index );
  22.  
  23. };

In view’s class I call showDialogIfNeed

  1. void Trend::Settings::XPenTableWidget::on_penTableView_doubleClicked(QModelIndex index)
  2. {
  3.     model->showDialogIfNeed( index );
  4. }

and I implemented showDialogIfNeed and overrode function flags to disable editable of color column

  1. void XPenTableModel::showDialogIfNeed( QModelIndex index )
  2. {
  3.     if ( index.column() == PrivateData::ColorOrder )
  4.     {
  5.         QColor color = QColorDialog::getColor();
  6.     }
  7. }
  8.  
  9.  
  10. Qt::ItemFlags flags( Qt::ItemIsEnabled )
  11. {
  12.     Qt::ItemFlags flags( Qt::ItemIsEnabled );
  13.  
  14.     if ( index.column() != PrivateData::ColorOrder )
  15.         flags |= Qt::ItemIsSelectable;
  16.  
  17.    
  18.     if ( index.column() == PrivateData::NameOrder )
  19.         flags |= Qt::ItemIsEditable;
  20.  
  21.     return flags;
  22. }

It works fine but function showDialogIfNeed places in Model. It is mix gui and logic.

Are there more correct solutions without the mix?

February 22, 2011

Volker Volker
Robot Herder
5428 posts

You can handle everything in the view class or another class that has access to the view. Just show the dialog in the on_penTableView_doubleClicked() slot and set the retrieved data in the model with setData().

 
  ‹‹ In window XP, I failed to open a database file comes from Android phone.      Qt Designer Plugin, Property Editor in initialize() returns null ››

You must log in to post a reply. Not a member yet? Register here!