PDA

View Full Version : QColorDialog always return 0 as a result



NoRulez
16th October 2009, 20:36
Hey @all,

within my sublacc of QItemDelegate, the QColorDialog returns always 0 when i want to get the result by using:


void MyDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
const QModelIndex &index) const {
QColorDialog *pDialog = qobject_cast<QColorDialog*>(editor);
if(pDialog) {
int resultCode = pDialog->result();
qDebug() << "Result Code : " << resultCode;
}
}


It doesn't matter if i press "OK" or "Cancel", the result is ever 0.

Hope anybody could help

Best Regards
NoRulez

wysota
16th October 2009, 23:18
What exactly do you need result() for?

NoRulez
16th October 2009, 23:31
In "createEditor()" I return a QColorDialog object, and set the inital data in "setEditorData()".
Now in the "setModelData()" I want to write the new color, so i need the "result()" to check if the user pressed the "OK" or the "Cancel" button,
because only if the user has pressed the "OK" button I want to write the "currentColor()" back to the model.
"selectedColor()" always returns "InvalidColor".


Best Regards
NoRulez

wysota
16th October 2009, 23:44
In "createEditor()" I return a QColorDialog object
Can you show a screenshot of how does that look? I somehow cannot imagine a QColorDialog inside a cell of a table...

NoRulez
16th October 2009, 23:54
It's also an normal QColorDialog...I've attached a screenshot.
So when i double click on a cell, the QColorDialog appears. Know i want to save the data/color when the user press the "OK" button, but the "result()" function always returns 0 which is QDialog::rejected, but i need QDialog::Accepted. "result()" returns ever 0, no matter which button was pressed.

Could you help me?
Best Regards

Lesiok
17th October 2009, 09:24
From QDialog::result reference : Returns the modal dialog's result code, Accepted or Rejected.
Show us how QColorDialog is invoked.

wysota
17th October 2009, 11:07
It's also an normal QColorDialog...I've attached a screenshot.
So how is that related to the delegate?


So when i double click on a cell, the QColorDialog appears.
That's not what QAbstractItemDelegate::createEditor() is for.

NoRulez
17th October 2009, 12:44
@wysota: Why not?

Returns the editor to be used for editing the data item with the given index. Note that the index contains information about the model being used. The editor's parent widget is specified by parent, and the item options by option.
The base implementation returns 0. If you want custom editing you will need to reimplement this function.
The returned editor widget should have Qt::StrongFocus; otherwise, QMouseEvents received by the widget will propagate to the view. The view's background will shine through unless the editor paints its own background (e.g., with setAutoFillBackground()).
So the QColorDialog inherits QDialog, and this inherits QWidget... It's a QWidget, so why I can't use them?
I view color bars in the tableview, on double click, the QColorDialog appears and I can change the color...
The only problem I have is to not get the Accepted result from the "result()" function.

@Lesiok: Here is the code of the delegate:


QWidget* MyDelegate::createEditor(QWidget* parent, const QStyleOptionViewItem &option,
const QModelIndex &index) const {
if (index.column() == 4) {
return new QColorDialog(parent);
}
return QItemDelegate::createEditor(parent, option, index);
}

void MyDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const {
QColor baseColor = QColor(index.sibling(index.row(), 5).data().toInt(),
index.sibling(index.row(), 6).data().toInt(),
index.sibling(index.row(), 7).data().toInt(),
index.sibling(index.row(), 8).data().toInt());
QColorDialog *pDialog = qobject_cast<QColorDialog*>(editor);
if(pDialog)
pDialog->setCurrentColor(baseColor);
}

void MyDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
const QModelIndex &index) const {
QColorDialog *pDialog = qobject_cast<QColorDialog*>(editor);
if(pDialog) {
int resultCode = pDialog->result();
qDebug() << "Result Code : " << resultCode;
// Here i only get QDialog::Rejected, no matter if the "OK" or the "Cancel" button was pressed
if(resultCode == QDialog::Accepted) {
QColor currentColor = pDialog->currentColor();
model->setData(index.model()->index(index.row(), 5), currentColor.red(), Qt::EditRole);
model->setData(index.model()->index(index.row(), 6), currentColor.green(), Qt::EditRole);
model->setData(index.model()->index(index.row(), 7), currentColor.blue(), Qt::EditRole);
model->setData(index.model()->index(index.row(), 8), currentColor.alpha(), Qt::EditRole);
}
}
}

void MyDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
const QModelIndex &index) const {
if(option.state & QStyle::State_Selected)
painter->fillRect(option.rect, option.palette.highlight());

QColor baseColor(index.sibling(index.row(), 5).data().toInt(),
index.sibling(index.row(), 6).data().toInt(),
index.sibling(index.row(), 7).data().toInt(),
index.sibling(index.row(), 8).data().toInt());

painter->save();
painter->setBrush(baseColor);
painter->setPen(Qt::black);
painter->drawRect(option.rect.x()+2, option.rect.y()+2,option.rect.width()-5, option.rect.height()-5);
painter->restore();
}

wysota
17th October 2009, 13:06
@wysota: Why not?
Because createEditor() is to return a persistent widget that takes place of the item for the time of editing. This is not the case here thus you are using the mechanism incorrectly.

Instead connect to the view's doubleClicked() signal and upon that signal call QColorDialog::getColor() and afterwards set the model's data using QAbstractItemModel::setData().