Trolltech Home | Qt-interest Home | Recent Threads | All Threads | Author | Date
All threads index page 3

Qt-interest Archive, December 2006
Qt::EditRole =(=) Qt::DisplayRole ???!! [QT 4.2.2]


Message 1 in thread

Hello list,

i want a QTableWidget to display a double Value in a special format, like "3.200 [µs]".
Thats not a problem by setting data with
	setData(Qt::DisplayRole, tr("%1 [µs]").arg(dValue,0,'f',3) );
and everything is fine until the user want to edit this value.
Editor brings up a QLineEdit. This sounds reasonable cause i set a text to be displayed.
But i want a QDoubleSpinBox. So I've searching the Assistant to edit the value with
a QDoubleSpinBox and found the Qt::EditRole where the Assistant says:

"Qt::EditRole - The data in a form suitable for editing in an editor."

Great, _BUT_, after debugging i found this:

QVariant QTableWidgetItem::data(int role) const
{
    role = (role == Qt::EditRole ? Qt::DisplayRole : role); // <--- WTF ?!
    for (int i = 0; i < values.count(); ++i)
        if (values.at(i).role == role)
            return values.at(i).value;
    return QVariant();
}

why do you return DisplayRole instead of EditRole ????
I _need_ EditRole to be returned !!
Is there an other way to achieve this behavior ?

Hope someone could help me.

Kind regards,
Michael


--
 [ signature omitted ] 

Message 2 in thread

Hi,

Most often, the "data" edited are the "data" displayed => the data
associated with the DisplayRole are equal to the data associated with the
EditRole.

Indeed, apparently, the trolls decided a QTableWidgetItem provide the same
data for both Display and EditRole.

That may nnot be a problem in your case.

As far as I understand your needs, what you want is a specificic editor
widget (ie a specific GUI) for the given item. But the editor QWidget, when
opened, should display the same data (the same number) as the data
displayed while the item is not edited.

If that's correct, there should be no problem for you : Edit and Display
role are associated with the same data.

You 'd **just** have to specify that the GUI QWidget to be used while
editing is no more a QLineEdit, but a QDoubleSpinBox.
To that aim, you may define and use your own delegate. 
See QItemDelegate, and probably some of the itemviews examples, especially
spinboxdelegate.

Hope I understood correctly your post, and my answer helps somehow !

Cheers
Nicolas

Michael Sprauer wrote:

> Hello list,
> 
> i want a QTableWidget to display a double Value in a special format, like
> "3.200 [Âs]". Thats not a problem by setting data with
> setData(Qt::DisplayRole, tr("%1 [Âs]").arg(dValue,0,'f',3) );
> and everything is fine until the user want to edit this value.
> Editor brings up a QLineEdit. This sounds reasonable cause i set a text to
> be displayed. But i want a QDoubleSpinBox. So I've searching the Assistant
> to edit the value with a QDoubleSpinBox and found the Qt::EditRole where
> the Assistant says:
> 
> "Qt::EditRole - The data in a form suitable for editing in an editor."
> 
> Great, _BUT_, after debugging i found this:
> 
> QVariant QTableWidgetItem::data(int role) const
> {
>     role = (role == Qt::EditRole ? Qt::DisplayRole : role); // <--- WTF ?!
>     for (int i = 0; i < values.count(); ++i)
>         if (values.at(i).role == role)
>             return values.at(i).value;
>     return QVariant();
> }
> 
> why do you return DisplayRole instead of EditRole ????
> I _need_ EditRole to be returned !!
> Is there an other way to achieve this behavior ?
> 
> Hope someone could help me.
> 
> Kind regards,
> Michael
> 
> 
> --
> To unsubscribe - send a mail to qt-interest-request@xxxxxxxxxxxxx with
> "unsubscribe" in the subject or the body. List archive and information:
> http://lists.trolltech.com/qt-interest/

--
 [ signature omitted ] 

Message 3 in thread

Nicolas Castagne wrote:

> Hi,
> 
> Most often, the "data" edited are the "data" displayed => the data
> associated with the DisplayRole are equal to the data associated with the
> EditRole.
> 

Maybe the following code snippet from a TableDelegate might help..
Check if the value's original type is numerical en return the right
editor...

QVariant qvValue = index.model()->data( index, Qt::DisplayRole );
        if( qvValue.type() == QVariant::Double )
        {
            QDoubleSpinBox *pEditor = new QDoubleSpinBox( parent );
            if( pEditor )
            {
                pEditor->setDecimals( 13 );
                pEditor->installEventFilter(
                const_cast<CTableEditDelegate*>(this) );

                pResult = pEditor;
            }
        }

-- 
 [ signature omitted ] 

Message 4 in thread

On 14.12.06 14:34:09, Michael Sprauer wrote:
> why do you return DisplayRole instead of EditRole ????
> I _need_ EditRole to be returned !!

Then don't use the convenience classes, they are meant to be used for
the "simple" cases.

> Is there an other way to achieve this behavior ?

Use a real model and a QTableView, either implement the model yourself
or use QStandardItemModel.

Andreas

-- 
 [ signature omitted ]