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
@
QWidget *ColorItemDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &,
const QModelIndex &) const
{QColorDialog* colorDialog = new QColorDialog(); return colorDialog;
}
void ColorItemDelegate::setModelData(QWidget *editor, QAbstractItemModel model,
const QModelIndex &index) const
{
QColorDialog colorDialog = static_cast<QColorDialog>( editor );
QColor color = colorDialog->selectedColor();
if ( color.isValid() )
model->setData(index, color, Qt::EditRole);
}
@But it doesn't work because of model-view system considers editor widget as simple widget not dialog. And color in
@ QColor color = colorDialog->selectedColor(); @
is always not valid.What are there adequate methods to solve this problem?
-
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() ":http://doc.qt.nokia.com/latest/qabstractitemview.html#doubleClicked (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.
-
Hi!
Volker, I implemented this idea.
I created model's class
@
class XPenTableModel : public QAbstractTableModel
{
Q_OBJECT
public:XPenTableModel( QObject* parent = NULL ); QVariant headerData(int section, Qt::Orientation orientation, int role) const; int rowCount(const QModelIndex &parent = QModelIndex()) const; int columnCount(const QModelIndex &parent = QModelIndex()) const; QVariant data(const QModelIndex &index, int role) const; bool setData(const QModelIndex &index, const QVariant &value, int role); Qt::ItemFlags flags(const QModelIndex &index) const;
public slots:
void showDialogIfNeed( QModelIndex index );};
@
In view's class I call showDialogIfNeed@
void Trend::Settings::XPenTableWidget::on_penTableView_doubleClicked(QModelIndex index)
{
model->showDialogIfNeed( index );
}
@and I implemented showDialogIfNeed and overrode function flags to disable editable of color column
@
void XPenTableModel::showDialogIfNeed( QModelIndex index )
{
if ( index.column() == PrivateData::ColorOrder )
{
QColor color = QColorDialog::getColor();
}
}Qt::ItemFlags flags( Qt::ItemIsEnabled )
{
Qt::ItemFlags flags( Qt::ItemIsEnabled );if ( index.column() != PrivateData::ColorOrder ) flags |= Qt::ItemIsSelectable; if ( index.column() == PrivateData::NameOrder ) flags |= Qt::ItemIsEditable; return flags;
}
@It works fine but function showDialogIfNeed places in Model. It is mix gui and logic.
Are there more correct solutions without the mix?