here is my code.
factory
->registerEditor
((QVariant::Type)qMetaTypeId<OBISCODE>
(),
&obisCodeEdit);
factory
->registerEditor
((QVariant::Type)qMetaTypeId<QString>
(),
&lineEdit);
factory
->registerEditor
((QVariant::Type)qMetaTypeId<QTime>
(),
&timeEdit);
w.setColumnCount(2);
w.setRowCount(1);
obis.a=1;
obis.b=2;
obis.c=3;
obis.d=4;
obis.e=5;
obis.f=6;
item
->setData
(Qt
::EditRole,
QVariant::fromValue<OBISCODE>
(obis
));
// item->setData(Qt::DisplayRole, "1.2.3.4.5");
w.setItem(0,0,item);
item
->setData
(Qt
::DisplayRole,
QTime(12,
34));
w.setItem(0,1, item);
w.show();
return app.exec();
factory=new QItemEditorFactory;
factory->registerEditor((QVariant::Type)qMetaTypeId<OBISCODE>(),
&obisCodeEdit);
factory->registerEditor((QVariant::Type)qMetaTypeId<QString>(),
&lineEdit);
factory->registerEditor((QVariant::Type)qMetaTypeId<QTime>(),
&timeEdit);
QItemEditorFactory::setDefaultFactory( factory);
w.setColumnCount(2);
w.setRowCount(1);
item= new QTableWidgetItem;
obis.a=1;
obis.b=2;
obis.c=3;
obis.d=4;
obis.e=5;
obis.f=6;
item->setData(Qt::EditRole, QVariant::fromValue<OBISCODE>(obis));
// item->setData(Qt::DisplayRole, "1.2.3.4.5");
w.setItem(0,0,item);
item= new QTableWidgetItem;
item->setData(Qt::DisplayRole, QTime(12,34));
w.setItem(0,1, item);
w.show();
return app.exec();
To copy to clipboard, switch view to plain text mode
Q_DECLARE_METATYPE( OBISCODE)
Q_OBJECT
Q_PROPERTY(OBISCODE obisCode
READ obisCode
WRITE setObisCode
USER true)
public:
void setObisCode(OBISCODE obis);
OBISCODE obisCode();
};
Q_DECLARE_METATYPE( OBISCODE)
class ObisCodeEdit:public QLineEdit{
Q_OBJECT
Q_PROPERTY(OBISCODE obisCode
READ obisCode
WRITE setObisCode
USER true)
public:
ObisCodeEdit(QWidget *parent=0);
void setObisCode(OBISCODE obis);
OBISCODE obisCode();
};
To copy to clipboard, switch view to plain text mode
ObisCodeEdit
::ObisCodeEdit(QWidget *parent
)
//setInputMask("000.000.000.000.000.000");
//setValidator(
// new QRegExpValidator(
// QRegExp("^[0-2]?[0-9]?[0-9]\\.[0-2]?[0-9]?[0-9]\\.[0-2]?[0-9]?[0-9]\\.[0-2]?[0-9]?[0-9]\\.[0-2]?[0-9]?[0-9]\\.[0-2]?[0-9]?[0-9]$"), this) );
}
void ObisCodeEdit::setObisCode( OBISCODE obis){
setText
(QString("%1.%2.%3.%4.%5.%6").
arg(obis.
a) .arg(obis.b)
.arg(obis.c)
.arg(obis.d)
.arg(obis.e)
.arg(obis.f));
}
OBISCODE ObisCodeEdit::obisCode(){
OBISCODE obis;
int index;
list
= text
().
split(".",
QString::SkipEmptyParts);
index=0;
if(list.count()>=index+1){
obis.a=list.at(index).toInt();
}
index++;
if(list.count()>=index+1){
obis.b=list.at(index).toInt();
}
index++;
if(list.count()>=index+1){
obis.c=list.at(index).toInt();
}
index++;
if(list.count()>=index+1){
obis.d=list.at(index).toInt();
}
index++;
if(list.count()>=index+1){
obis.e=list.at(index).toInt();
}
index++;
if(list.count()>=index+1){
obis.f=list.at(index).toInt();
}
return obis;
}
ObisCodeEdit::ObisCodeEdit(QWidget *parent)
:QLineEdit(parent){
//setInputMask("000.000.000.000.000.000");
//setValidator(
// new QRegExpValidator(
// QRegExp("^[0-2]?[0-9]?[0-9]\\.[0-2]?[0-9]?[0-9]\\.[0-2]?[0-9]?[0-9]\\.[0-2]?[0-9]?[0-9]\\.[0-2]?[0-9]?[0-9]\\.[0-2]?[0-9]?[0-9]$"), this) );
}
void ObisCodeEdit::setObisCode( OBISCODE obis){
setText(QString("%1.%2.%3.%4.%5.%6").arg(obis.a)
.arg(obis.b)
.arg(obis.c)
.arg(obis.d)
.arg(obis.e)
.arg(obis.f));
}
OBISCODE ObisCodeEdit::obisCode(){
QStringList list;
OBISCODE obis;
int index;
list= text().split(".", QString::SkipEmptyParts);
index=0;
if(list.count()>=index+1){
obis.a=list.at(index).toInt();
}
index++;
if(list.count()>=index+1){
obis.b=list.at(index).toInt();
}
index++;
if(list.count()>=index+1){
obis.c=list.at(index).toInt();
}
index++;
if(list.count()>=index+1){
obis.d=list.at(index).toInt();
}
index++;
if(list.count()>=index+1){
obis.e=list.at(index).toInt();
}
index++;
if(list.count()>=index+1){
obis.f=list.at(index).toInt();
}
return obis;
}
To copy to clipboard, switch view to plain text mode
when i change
item
->setData
(Qt
::DisplayRole,
QVariant::fromValue<OBISCODE>
(obis
));
item->setData(Qt::DisplayRole, QVariant::fromValue<OBISCODE>(obis));
To copy to clipboard, switch view to plain text mode
it did not display text until i double click it.
but this code is right.
item
->setData
(Qt
::DisplayRole,
QTime(12,
34));
w.setItem(0,1, item);
item= new QTableWidgetItem;
item->setData(Qt::DisplayRole, QTime(12,34));
w.setItem(0,1, item);
To copy to clipboard, switch view to plain text mode
Bookmarks