Results 1 to 3 of 3

Thread: Keep a QStandardItem in QTreeView in edit mode till it is given a unique name

  1. #1
    Join Date
    Dec 2016
    Location
    New England, US
    Posts
    31
    Thanks
    6
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Keep a QStandardItem in QTreeView in edit mode till it is given a unique name

    I have QStandardItem::text() displayed using QTreeView thus:
    QtPost_treeWidget.PNG

    I would like the names of the items under a parent to be unique. In this example, this is the flow of events:
    • First I double click on 'Table3', it goes into edit mode.
    • Then I change the name to 'Table2' - not a unique name, and then click on 'Table1'.
    • In this case it pops up a message box, asking the user to fix the error (not a modal dialog! Just a simple message box).
    • When the user hits 'OK' in the message box, the slot keeps the view of the original item 'Table3' in edit mode.


    But it's too late! My slot exits, and the slot that triggered itemChanged(...) signal now goes on to processing the clicked() action of `Table1'.

    You know my conundrum now: how do I put an infinite loop in my slot that checks for uniqueness of the item names? Without popping up a modal dialog!! I understand how I can do it by populating a text box in a dialog and check it when the user clicks OK. Is there a better way to enforce uniqueness of the item names as they are being created through GUI.

    Thanks!

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Keep a QStandardItem in QTreeView in edit mode till it is given a unique name

    One option you have is a custom item delegate.

    Basically the editing of an item happens through an "editor widget" created by the item delegate.
    For a string that widget is a QLineEdit.

    With a custom delegate you can either provide a widget that does not allow input that you do not want, or you can still provide a QLineEdit but not accept the value in the delegate's setModelData() method.

    Cheers,
    _

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

    NameRakes (5th January 2017)

  4. #3
    Join Date
    Dec 2016
    Location
    New England, US
    Posts
    31
    Thanks
    6
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Keep a QStandardItem in QTreeView in edit mode till it is given a unique name

    Thanks to your pointer, after some more reading, I have implemented your suggestion with a minor variation. The variation came from the fact that I did not want the user to leave the editor without providing a unique name. There can be complications catching this in setModelData() - what to do if the data is invalid? So this is what I have done:

    Still I have the derived (custom) QStyledItemDelegate. This now attaches a custom QValidator to the editor widget. Whenever the editor is created, the custom delegate emits a signal asking for a list of names to check against, which is provided by my custom widget implementing MV. The custom validator performs the check in the validate() method. Now the editor won't budge an inch, there is no way for the user to enter a non-unique name.

    It works great! Thanks to you and others in the forum, I have a much better understanding of the MVD (I am calling it MVD instead of MV) architecture.

Similar Threads

  1. Multiple clicks required to get into edit mode on QTreeView
    By JonInAnnArbor in forum Qt Programming
    Replies: 10
    Last Post: 26th November 2012, 19:59
  2. Hide children of QStandardItem in QTreeView
    By Phlucious in forum Qt Programming
    Replies: 2
    Last Post: 18th June 2012, 21:48
  3. Set Focus on created QStandardItem and edit Text
    By blackbubblegum in forum Newbie
    Replies: 1
    Last Post: 29th February 2012, 11:52
  4. Replies: 1
    Last Post: 16th April 2010, 22:59
  5. Question on checkable QStandardItem in QTreeView
    By ttvo in forum Qt Programming
    Replies: 1
    Last Post: 3rd April 2009, 01:30

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.