Results 1 to 6 of 6

Thread: How to refresh table item

  1. #1
    Join Date
    Apr 2007
    Location
    Sunny Darwin, NT Australia
    Posts
    186
    Thanks
    29
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default How to refresh table item

    Inside the setModelData() method of QItemDelegate I need to reformat the edited text and refresh the table. Which methods are available to do this ?

  2. #2
    Join Date
    Jan 2008
    Location
    Poland
    Posts
    687
    Thanks
    4
    Thanked 140 Times in 132 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to refresh table item

    What model do you use? Changes to the model are done by setData() and it should emit dataChanged() signal to notify view about changes. So if you use your own custom model (derived from QAbstractItemModel) make sure you emit dataChanged(). If you use some QStandardItemModel then any change will emit dataChanged() signal so you don't need to do anything, and the view should be refreshed.
    I would like to be a "Guru"

    Useful hints (try them before asking):
    1. Use Qt Assistant
    2. Search the forum

    If you haven't found solution yet then create new topic with smart question.

  3. #3
    Join Date
    Apr 2007
    Location
    Sunny Darwin, NT Australia
    Posts
    186
    Thanks
    29
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to refresh table item

    Model is a QSqlTableModel, maybe the changed signal is being emitted, but I don't know if my method is right.

    Qt Code:
    1. void LineEditDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
    2. const QModelIndex &index) const
    3. {
    4. QLineEdit *edit = qobject_cast<QLineEdit *>(editor);
    5. QString oldVal = index.model()->data(index, Qt::EditRole).toString();
    6. QString newVal = edit->text();
    7. if (oldVal != newVal)
    8. {
    9. model->setData(index, newVal.remove(QRegExp("[()-]")));
    10. /*
    11. // This is wrong...edit has been destroyed by now.
    12.   if ((newVal.left(2) == "04") || (newVal.left(1) == "1"))
    13.   edit->setText(newVal.left(2)+"-"+newVal.mid(2,4)+"-"+newVal.mid(6,4));
    14.   else
    15.   edit->setText("("+newVal.left(2)+")"+newVal.mid(2,4)+"-"+newVal.mid(6,4));
    16. */
    17. }
    18. }
    To copy to clipboard, switch view to plain text mode 

    I'm stripping away the inputMask characters and checking the first 1 or 2 digits to see which format to apply. I save the edited digits in the model but I want the formatted text displayed (e.g. 04-1234-5678)
    Last edited by vieraci; 31st May 2009 at 15:34. Reason: updated contents

  4. #4
    Join Date
    Jan 2008
    Location
    Poland
    Posts
    687
    Thanks
    4
    Thanked 140 Times in 132 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to refresh table item

    hmm I think I don't understand... You are saving 2 digits in the model but want to display 8 digits with dashes it those cells with 2 digits? so maybe store the whole string in model, or modify the painting in paint() method. Also I would rather use QValidator instead of inputMask.
    I would like to be a "Guru"

    Useful hints (try them before asking):
    1. Use Qt Assistant
    2. Search the forum

    If you haven't found solution yet then create new topic with smart question.

  5. #5
    Join Date
    Apr 2007
    Location
    Sunny Darwin, NT Australia
    Posts
    186
    Thanks
    29
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to refresh table item

    No, I'm saving a phone number, can be between 6 to 10 digits. I'm stripping away the inputMask characters so I can see the left most 1 or 2 digits but I'm saving just the digits.

    That was my mistake. I have to save the whole string.

  6. #6
    Join Date
    Mar 2009
    Location
    Italy
    Posts
    7
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to refresh table item

    Quote Originally Posted by faldżip View Post
    hmm I think I don't understand... You are saving 2 digits in the model but want to display 8 digits with dashes it those cells with 2 digits? so maybe store the whole string in model, or modify the painting in paint() method. Also I would rather use QValidator instead of inputMask.

    Hi,


    you have to reimplement the data method for the DisplayRole in order to format "as you want" the saved data. Now, the data method of model is replying with the saved data without the two digits of masks. I think that this is the best way to manage the situation instead reimplementing the paint method.

    Bye.

    Alessandro

Similar Threads

  1. Qt4 Table Item Spanning
    By billyp in forum Qt Programming
    Replies: 1
    Last Post: 20th December 2008, 17:11
  2. Postgresql QSqlRelationalTableModel empty table
    By RolandHughes in forum Qt Programming
    Replies: 0
    Last Post: 12th November 2008, 17:18
  3. Replies: 2
    Last Post: 13th September 2008, 13:55
  4. View, Scene, Item and thread??
    By dungsivn in forum Qt Programming
    Replies: 5
    Last Post: 20th August 2008, 19:21
  5. Item Delegate Painting
    By stevey in forum Qt Programming
    Replies: 3
    Last Post: 9th May 2008, 07:37

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.