miraks
17th August 2009, 10: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 ?
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 ?