Results 1 to 7 of 7

Thread: QAbstractItemModel / QStyledItemDelegate howto update data on value changed?

  1. #1
    Join Date
    Jun 2010
    Location
    Germany, Munich
    Posts
    16
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QAbstractItemModel / QStyledItemDelegate howto update data on value changed?

    Hi there,
    I have QAbstractItemModel and displaying its data in a QTableView.
    A derived QStyledItemDelegate class creates an QSpinBox to edit values in
    column 1.
    A QSpinbox on some other place on the widget where the QTableView is nested,
    shows the same data eg. (2/1), the QSpinbox is connected through QWidgetMapper.
    Actually it works fine: I can change both value and they update each other if
    I change the focus.
    But thats the point! The changed values have to immediately update the model
    data. How can I force that the valueChange() signal update the model data.

    Therefore if I insert a number directly in the spinbox and press enter,
    the number jumps back to the old value. Only the up/down button works to edit
    the value.

    Thanks for help

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: QAbstractItemModel / QStyledItemDelegate howto update data on value changed?

    You should be connecting the outer / other QSpinBox to QAbstractItemModel, and not to the QTableView's or QStyledItemDelegate's QSpinBox, are you doing it this way?

  3. #3
    Join Date
    Jun 2010
    Location
    Germany, Munich
    Posts
    16
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QAbstractItemModel / QStyledItemDelegate howto update data on value changed?

    Yes:
    Qt Code:
    1. otpTableView = QTableView(this)
    2. otpTableView.setModel(otpmodel)
    3. otpTableView.setItemDelegate( OtpDelegate_C(this) )
    4.  
    5.  
    6. mapper_r1 = QtGui.QDataWidgetMapper(this)
    7. mapper_r1.setModel(otpmodel)
    8. mapper_r1.addMapping(label_row1, 0, "text")
    9. mapper_r1.addMapping(spinbox_row1, 1)
    10. mapper_r1.setCurrentIndex(1)
    To copy to clipboard, switch view to plain text mode 

    Anyway the problem is that the data from the editor will be send to the model only after the editor lost the focus. I think that is normal behavior for models indeed.
    But if you connect eg. a slider with spinbox, you can see intstandly the changing values. That`s what I have to achieve.
    Thanks

  4. #4
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: QAbstractItemModel / QStyledItemDelegate howto update data on value changed?

    In that case use QSpinBox::valueChanged() signal

  5. #5
    Join Date
    Jun 2010
    Location
    Germany, Munich
    Posts
    16
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QAbstractItemModel / QStyledItemDelegate howto update data on value changed?

    Ups, some post get lost somewhere ???
    Thanks Santosh, but I dosn`t connect anything it goes autmaticaly by
    Qt Code:
    1. mapper_r1.addMapping
    To copy to clipboard, switch view to plain text mode 
    Maybe I have to derive QTableView and QDataWidgetMapper to force some signals

  6. #6
    Join Date
    Jun 2010
    Location
    Germany, Munich
    Posts
    16
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QAbstractItemModel / QStyledItemDelegate howto update data on value changed?

    I improved the code to

    Qt Code:
    1. mapper_r2 = QtGui.QDataWidgetMapper(this)
    2. mapper_r2.setModel(otpmodel)
    3. mapper_r2.addMapping(label_row2, 0, "text")
    4. mapper_r2.addMapping(spinbox_row2, 1)
    5. mapper_r2.setCurrentIndex(1)
    6. connect(
    7. spinbox_row2, SIGNAL(valueChanged(), otpmodel, SLOT(dataChanged()))
    8. )
    9. mapper_r2.setSubmitPolicy(QtGui.QDataWidgetMapper.AutoSubmit)
    To copy to clipboard, switch view to plain text mode 

    But as expected, the value isn`t set to the model already , so an dataChanged() isn`t enough

  7. #7
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: QAbstractItemModel / QStyledItemDelegate howto update data on value changed?

    There is no such SLOT in QAbstractItemModel

Similar Threads

  1. Replies: 2
    Last Post: 9th August 2010, 18:53
  2. Replies: 12
    Last Post: 24th July 2010, 09:12
  3. Update to 4.4.2: cornerWidget functionality changed
    By mclark in forum Qt Programming
    Replies: 5
    Last Post: 21st November 2008, 02:14
  4. update custom QStyledItemDelegate after drag
    By dano in forum Qt Programming
    Replies: 1
    Last Post: 17th November 2008, 19:29
  5. Informing a model its data has changed
    By saknopper in forum Newbie
    Replies: 3
    Last Post: 17th January 2007, 20:58

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.