Results 1 to 5 of 5

Thread: how to show text and custom editor in QTableWidgetItem

  1. #1
    Join Date
    Apr 2010
    Posts
    4
    Thanks
    1
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default how to show text and custom editor in QTableWidgetItem

    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 .
    Qt Code:
    1. item->setData(Qt::EditRole, QVariant::fromValue<OBISCODE>(obis));
    2. // item->setData(Qt::DisplayRole, "1.2.3.4.5");
    To copy to clipboard, switch view to plain text mode 


    itemeditor..zip
    screen..jpg

  2. #2
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: how to show text and custom editor in QTableWidgetItem

    What is your question?

  3. The following user says thank you to tbscope for this useful post:

    brook2lost (7th June 2010)

  4. #3
    Join Date
    Apr 2010
    Posts
    4
    Thanks
    1
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: how to show text and custom editor in QTableWidgetItem

    here is my code.
    Qt Code:
    1. factory=new QItemEditorFactory;
    2. factory->registerEditor((QVariant::Type)qMetaTypeId<OBISCODE>(),
    3. &obisCodeEdit);
    4. factory->registerEditor((QVariant::Type)qMetaTypeId<QString>(),
    5. &lineEdit);
    6. factory->registerEditor((QVariant::Type)qMetaTypeId<QTime>(),
    7. &timeEdit);
    8. QItemEditorFactory::setDefaultFactory( factory);
    9.  
    10. w.setColumnCount(2);
    11. w.setRowCount(1);
    12.  
    13. item= new QTableWidgetItem;
    14.  
    15. obis.a=1;
    16. obis.b=2;
    17. obis.c=3;
    18. obis.d=4;
    19. obis.e=5;
    20. obis.f=6;
    21. item->setData(Qt::EditRole, QVariant::fromValue<OBISCODE>(obis));
    22. // item->setData(Qt::DisplayRole, "1.2.3.4.5");
    23. w.setItem(0,0,item);
    24.  
    25. item= new QTableWidgetItem;
    26. item->setData(Qt::DisplayRole, QTime(12,34));
    27. w.setItem(0,1, item);
    28. w.show();
    29.  
    30. return app.exec();
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. Q_DECLARE_METATYPE( OBISCODE)
    2. class ObisCodeEdit:public QLineEdit{
    3.  
    4. Q_OBJECT
    5. Q_PROPERTY(OBISCODE obisCode
    6. READ obisCode
    7. WRITE setObisCode
    8. USER true)
    9. public:
    10. ObisCodeEdit(QWidget *parent=0);
    11. void setObisCode(OBISCODE obis);
    12. OBISCODE obisCode();
    13. };
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. ObisCodeEdit::ObisCodeEdit(QWidget *parent)
    2. :QLineEdit(parent){
    3.  
    4. //setInputMask("000.000.000.000.000.000");
    5. //setValidator(
    6. // new QRegExpValidator(
    7. // 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) );
    8. }
    9.  
    10. void ObisCodeEdit::setObisCode( OBISCODE obis){
    11.  
    12. setText(QString("%1.%2.%3.%4.%5.%6").arg(obis.a)
    13. .arg(obis.b)
    14. .arg(obis.c)
    15. .arg(obis.d)
    16. .arg(obis.e)
    17. .arg(obis.f));
    18. }
    19. OBISCODE ObisCodeEdit::obisCode(){
    20.  
    21. OBISCODE obis;
    22. int index;
    23.  
    24.  
    25.  
    26. list= text().split(".", QString::SkipEmptyParts);
    27.  
    28. index=0;
    29. if(list.count()>=index+1){
    30.  
    31. obis.a=list.at(index).toInt();
    32. }
    33.  
    34. index++;
    35. if(list.count()>=index+1){
    36.  
    37. obis.b=list.at(index).toInt();
    38. }
    39.  
    40. index++;
    41. if(list.count()>=index+1){
    42.  
    43. obis.c=list.at(index).toInt();
    44. }
    45.  
    46. index++;
    47. if(list.count()>=index+1){
    48.  
    49. obis.d=list.at(index).toInt();
    50. }
    51.  
    52.  
    53. index++;
    54. if(list.count()>=index+1){
    55.  
    56. obis.e=list.at(index).toInt();
    57. }
    58.  
    59.  
    60. index++;
    61. if(list.count()>=index+1){
    62.  
    63. obis.f=list.at(index).toInt();
    64. }
    65. return obis;
    66. }
    To copy to clipboard, switch view to plain text mode 

    when i change
    Qt Code:
    1. 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.
    Qt Code:
    1. item= new QTableWidgetItem;
    2. item->setData(Qt::DisplayRole, QTime(12,34));
    3. w.setItem(0,1, item);
    To copy to clipboard, switch view to plain text mode 

  5. #4
    Join Date
    Apr 2010
    Posts
    4
    Thanks
    1
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: how to show text and custom editor in QTableWidgetItem

    my requirement is it behaves like QLineEdit when display, and behaves like IP address when editing

  6. #5
    Join Date
    Apr 2010
    Posts
    4
    Thanks
    1
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: how to show text and custom editor in QTableWidgetItem

    i figure it out at last. just add w.openPersistentEditor(item);
    Qt Code:
    1. item= new QTableWidgetItem;
    2.  
    3. obis.a=1;
    4. obis.b=2;
    5. obis.c=3;
    6. obis.d=4;
    7. obis.e=5;
    8. obis.f=6;
    9. item->setData(Qt::DisplayRole, QVariant::fromValue<OBISCODE>(obis));
    10. // item->setData(Qt::DisplayRole, "1.2.3.4.5");
    11. w.setItem(0,0,item);
    12. w.openPersistentEditor(item);
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Extracting text from QTableWidgetItem
    By bizmopeen in forum Newbie
    Replies: 3
    Last Post: 1st September 2009, 17:28
  2. QTableWidgetItem Text Editing
    By ToddAtWSU in forum Qt Programming
    Replies: 2
    Last Post: 17th March 2009, 02:23
  3. Rich Text in QTableWidgetItem
    By joshuajcarson in forum Qt Programming
    Replies: 9
    Last Post: 2nd September 2008, 15:49
  4. QTableWidgetItem Text
    By pytro in forum Qt Programming
    Replies: 2
    Last Post: 6th August 2007, 21:44
  5. Replies: 12
    Last Post: 15th February 2006, 10:46

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.