PDA

View Full Version : Does QDataWidgetMapper use flags?



Netwiz
22nd September 2009, 01:29
I have derived my own model from QAbstractTableModel, including implementing Qt::ItemFlags flags ( const QModelIndex & index ).

When I display the model using QTableView, it responds as expected to the flags that I set for a given column:
eg. Qt::ItemIsEnabled allows mouse interaction with the column
Qt::ItemIsEditable allows the cell to be edited

When I connect the model to a QDataWidgetMapper, I expected the flags to similarly interact with the widgets in the mapper. eg. I would expect that the widget would be disabled if Qt::ItemIsEnabled is not set. In my testing that doesn't seem to happen.

Am I missing something here?

Or does QDataWidgetMapper completely ignore the model flags?

Example given below is a very basic model, in which flags() returns Qt::ItemIsSelectable only. In my thinking this should disable the mapped widget and not permit editing. In fact it is enabled and editable.

Any help here would be very much appreciated. Thanks in advance.



class myModel : public QAbstractTableModel
{
Q_OBJECT

public:
myModel();
int rowCount ( const QModelIndex & parent = QModelIndex() ) const;
int columnCount ( const QModelIndex & parent = QModelIndex() ) const;
QVariant data ( const QModelIndex & index, int role = Qt::DisplayRole ) const;
bool setData ( const QModelIndex & index, const QVariant & value, int role = Qt::EditRole );
QVariant headerData ( int section, Qt::Orientation orientation, int role = Qt::DisplayRole ) const;
Qt::ItemFlags flags ( const QModelIndex & index ) const;
protected:
QList<QString> c1;
QList<QString> c2;
};

int myModel::rowCount ( const QModelIndex & parent ) const
{
return c1.count();
}



int myModel::columnCount ( const QModelIndex & parent ) const
{
return 2;
}



QVariant myModel::data ( const QModelIndex & index, int role ) const
{
if ( (role != Qt::DisplayRole) && (role != Qt::EditRole) )
return QVariant();
return (index.column() == 0) ? QVariant(c1.at(index.row())) : QVariant(c2.at(index.row()));
}



bool myModel::setData ( const QModelIndex & index, const QVariant & value, int role )
{
if (index.column() == 0)
c1[index.row()]= value.toString();
else
c2[index.row()]= value.toString();
emit dataChanged( this->index(0,0), this->index(c1.count(),1));
return true;
}

QVariant myModel::headerData ( int section, Qt::Orientation orientation, int role ) const
{
return QVariant(QString(""));
}

Qt::ItemFlags myModel::flags( const QModelIndex & index ) const
{
return Qt::ItemIsSelectable;
}

NameRakes
29th October 2021, 01:56
Or does QDataWidgetMapper completely ignore the model flags?


Wow, no one has responded in 12 years! The answer is NO. Qt ignores item flags - I have looked into QDataWidgetMapper code (for a completely different reason).

Why did I stumble upon this after 12 years? I was looking for an answer to this question: a widget which has some data, became disabled/invisible due to some choice in GUI, does it continue to participate in mapping to model? For example: let's say the choices are rectangle or triangle for a 2D figure, having 4 and 3 pairs of coordinates respectively through QLineEdit. And someone converted a rectangle to a triangle: I can easily disable/hide the last row. But the widget is still there - does the data get transferred, say under, manual submit mode? If the data transferred is empty QVariant(), that's fine. But I believe that's not the case.