PDA

View Full Version : how to show text and custom editor in QTableWidgetItem



brook2lost
7th June 2010, 07:28
my requirement is showing data using text, and editing data with custom format?
The code below display none when not editing, but show text when being clicked .


item->setData(Qt::EditRole, QVariant::fromValue<OBISCODE>(obis));
// item->setData(Qt::DisplayRole, "1.2.3.4.5");



4736
4737

tbscope
7th June 2010, 07:51
What is your question?

brook2lost
7th June 2010, 07:59
here is my code.


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();



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();
};





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;
}


when i change



item->setData(Qt::DisplayRole, QVariant::fromValue<OBISCODE>(obis));

it did not display text until i double click it.
but this code is right.



item= new QTableWidgetItem;
item->setData(Qt::DisplayRole, QTime(12,34));
w.setItem(0,1, item);

brook2lost
7th June 2010, 08:24
my requirement is it behaves like QLineEdit when display, and behaves like IP address when editing

brook2lost
7th June 2010, 09:15
i figure it out at last. just add w.openPersistentEditor(item);


item= new QTableWidgetItem;

obis.a=1;
obis.b=2;
obis.c=3;
obis.d=4;
obis.e=5;
obis.f=6;
item->setData(Qt::DisplayRole, QVariant::fromValue<OBISCODE>(obis));
// item->setData(Qt::DisplayRole, "1.2.3.4.5");
w.setItem(0,0,item);
w.openPersistentEditor(item);