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

Qt-interest Archive, March 2007
QVariant::UserType


Message 1 in thread

I want create QTreeView for program settings where each node can be our 
UserType like stuctures:

struct UserData{
int userId;
QString name;
};

struct ProfileSettings{
int countryId;
QString countryname;
};

And i want delegate each type items. For UserData type - QLineEdit, for 
ProfileSettings type - QComboBox.

Can i define delegate type like this ?:

factory->registerEditor(QVariant::UserData, LineEditListCreator);
factory->registerEditor(QVariant::ProfileSettings, ComboBoxListCreator);

or

QWidget *VariantDelegate::createEditor(QWidget *parent,const 
QStyleOptionViewItem &,const QModelIndex &index) const
{
     QVariant originalValue = index.model()->data(index, Qt::UserRole);
     if (!isSupportedType(originalValue.type()))
         return 0;

     switch (originalValue.type())
     {
     case QVariant::UserData:
     QLineEdit *lineEdit = new QLineEdit(parent);
     lineEdit->setFrame(false);
     return lineEdit;
     break;
     case QVariant::ProfileSettings:
     QComboBox *combo = new QComboBox(parent);
     return combo;
     break;
     default:
     QLineEdit *lineEdit = new QLineEdit(parent);
     lineEdit->setFrame(false);
     return lineEdit;
     }
}

--
 [ signature omitted ] 

Message 2 in thread

On 23.03.07 12:38:22, SABROG wrote:
> I want create QTreeView for program settings where each node can be our 
> UserType like stuctures:
> 
> struct UserData{
> int userId;
> QString name;
> };
> 
> struct ProfileSettings{
> int countryId;
> QString countryname;
> };
> 
> And i want delegate each type items. For UserData type - QLineEdit, for 
> ProfileSettings type - QComboBox.
> 
> Can i define delegate type like this ?:
> 
> factory->registerEditor(QVariant::UserData, LineEditListCreator);
> factory->registerEditor(QVariant::ProfileSettings, ComboBoxListCreator);
> 
> or
> 
> QWidget *VariantDelegate::createEditor(QWidget *parent,const 
> QStyleOptionViewItem &,const QModelIndex &index) const
> {
>     QVariant originalValue = index.model()->data(index, Qt::UserRole);
>     if (!isSupportedType(originalValue.type()))
>         return 0;
> 
>     switch (originalValue.type())
>     {
>     case QVariant::UserData:

The answer to both is basically: no. At least not unless you extend the
enum from QVariant with your own types. However thats not really needed
I think, you can just register your custom types with
Q_DECLARE_METATYPE and qRegisterMetaType (see docs for QMetaType class)
and then use something like

if( variant.canConvert<UserData>() )
	<line-edit-creation>
else if ( variant.canConvert<ProfileSettings>() )
	<combobox>

Andreas

-- 
 [ signature omitted ]