PDA

View Full Version : Delegate is not validated automatically with my own widget



miraks
17th August 2009, 11:05
Hi,

In an editable QTableView, I added the following delegate:

SKGQueryDelegate::SKGQueryDelegate(QObject* parent, SKGDocument* iDoc, bool iModeUpdate):
QItemDelegate(parent), document(iDoc), updateMode(iModeUpdate)
{
}

SKGQueryDelegate::~SKGQueryDelegate()
{
document=NULL;
}

QWidget* SKGQueryDelegate::createEditor(QWidget *parent,
const QStyleOptionViewItem & option ,
const QModelIndex & index ) const
{
SKGTRACEIN(1, "SKGQueryDelegate::createEditor");

QTableWidgetItem* it_h=((QTableWidget*) this->parent())->horizontalHeaderItem(index.column());
QString attname=it_h->data(Qt::UserRole).toString();

SKGPredicatCreator* editor=new SKGPredicatCreator(parent, document, attname, updateMode);

return editor;
}

void SKGQueryDelegate::setEditorData ( QWidget * editor, const QModelIndex & index ) const
{
SKGTRACEIN(1, "SKGQueryDelegate::setEditorData");
SKGPredicatCreator *pred = dynamic_cast<SKGPredicatCreator*>(editor);
if (pred) {
pred->setXmlDescription(index.model()->data(index, Qt::UserRole).toString());
} else QItemDelegate::setEditorData (editor, index);
}

void SKGQueryDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
const QModelIndex &index) const
{
SKGTRACEIN(1, "SKGQueryDelegate::setModelData");
SKGPredicatCreator *pred = dynamic_cast<SKGPredicatCreator*>(editor);
if (pred && model) {
QString xml=pred->xmlDescription();
model->setData(index, pred->text(), Qt::DisplayRole);
model->setData(index, xml, Qt::UserRole);
} else QItemDelegate::setModelData (editor, model, index);
}

using my own widget SKGPredicatCreator:

class SKGBANKGUI_EXPORT SKGPredicatCreator : public QWidget
{
Q_OBJECT
Q_PROPERTY( QString test READ text USER true)
Q_PROPERTY( QString xmlDescription READ xmlDescription WRITE setXmlDescription)

public:
/**
* Default Constructor
* @param parent the parent
* @param document the document
* @param attribute name of the attribute
*/
explicit SKGPredicatCreator(QWidget *parent = 0, SKGDocument* document=NULL, const QString& attribute="", bool iSQL=false);

/**
* Default Destructor
*/
virtual ~SKGPredicatCreator();

/**
* Get text
* @return text
*/
virtual QString text();

/**
* Get Text from XML description
* @param iXML the description
*/
static QString getTextFromXml(const QString& iXML);

/**
* Get XML description
* @return description
*/
virtual QString xmlDescription();

/**
* Set XML description
* @param iText the description
*/
virtual void setXmlDescription(const QString& iXML);
signals:


public slots:


private slots:
void onOperatorChanged();

private:
Q_DISABLE_COPY(SKGPredicatCreator);
bool updateMode;

SKGComboBox* kOperator;
QWidget* kValue1;
QWidget* kValue2;
};

It works fine, except for the following scenario:
1-double click on a cell ==> cell becomes editable (SKGPredicatCreator is created).
2-Enter values in custom widget SKGPredicatCreator (do not click enter)
3-Click on external button "Add"

Problem: Values entered in step 2 are not automatically validate when I do step 3 (focus on SKGPredicatCreator is lost). :(
It's not the case, if I use a std widget like QComboBox. :confused:

Why ? What I have to do to activate automatic validation ?

miraks
20th August 2009, 08:52
No idea ! :(

miraks
21st August 2009, 13:26
Please. I didn't find the solution !

miraks
24th August 2009, 12:18
Nobody knows ! :crying:

nightghost
24th August 2009, 21:39
I'm not sure if I have understand your scenario.

The "external" button is not part of the editor widget? Why not make the button part of the editor? Clicking on "add" would do a validation and close the widget if the data is valid? Afair "setModelData()" is called if the editor loses his focus.

or ask a method like "isValid()" in "setModelData()" and only add the data to the model if it returns true.

miraks
19th September 2009, 12:45
To be clearer:

I have a QTreeWidget with a delegate for edition of cells.

Case 1- If the component used for edition is a QComboBox, then the edition mode is closed and the value of the QComboBox is kept when the focus is lost.
This is a standard behaviour with code needed.

Case 2- If the component used for edition is a my widget (A frame containing 3 QComboBox), then the edition mode is closed and the value of the QComboBox is NOT kept (cell is empty) when the focus is lost.
So, end user needs to validate the cell by clicking "Return".

How to get same behaviour in Case 2 than in Case 1 ?