Results 1 to 13 of 13

Thread: QTreeWidget item editing: want column specificity

  1. #1
    Join Date
    Oct 2006
    Location
    Massachusetts, USA
    Posts
    19
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default QTreeWidget item editing: want column specificity

    I am using QTreeWidget, such a nice convenience class for us porters from Qt 3 -> 4, but I am unable to find a way to make a particular column of a QTreeWidgetItem editable while the other columns are not. Currently, I make an item editable upon creation like this:

    Qt Code:
    1. item->setFlags(item->flags() | Qt::ItemIsEditable);
    To copy to clipboard, switch view to plain text mode 

    But this makes all the columns editable, whereas I just want to be able to edit, say, column 2. I tried not setting the item flags as above, and instead calling

    Qt Code:
    1. tree_widget->editItem(item,2);
    To copy to clipboard, switch view to plain text mode 

    in my slot for itemDoubleClicked(), after checking that the double-clicked column is 2. But that fails because the item must have its editable flag set to true for editItem() to succeed.

    What I'd like to see is a column-specific QTreeWidgetItem::setFlags() function, i.e. QTreeWidgetItem::setFlags(Qt::ItemFlags flags, int col = -1). How to get the desired effect without resorting to subclassing? Ask TrollTech for an enhancement?

    I'm at Qt 4.2.1.

    Advice, anyone?

    thanks,
    McKee

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QTreeWidget item editing: want column specificity

    Use the model-view approach. You can operate on each column separately there.

  3. #3
    Join Date
    Oct 2006
    Location
    Massachusetts, USA
    Posts
    19
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTreeWidget item editing: want column specificity

    I found a better solution: leave the QTreeWidgetItem objects un-editable, and call the following convenience functions provided by QTreeWidget:

    Qt Code:
    1. tree_widget->openPersistentEditor(item,col);
    2. ...
    3. tree_widget->closePersistentEditor(item,col);
    To copy to clipboard, switch view to plain text mode 

    The item and column are provided by the signals I capture with slots in my parent form. I make the calls only for the target column, thus achieving the column-specificity I was after. For my app I am using signal
    Qt Code:
    1. QTreeWidget::itemDoubleClicked(QTreeWidgetItem*,int)
    To copy to clipboard, switch view to plain text mode 
    to trigger editing.

    So no fooling with QTreeWidgetItem flags, just starting/stopping the editor explicitly, and using the persistent one.

    McKee

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QTreeWidget item editing: want column specificity

    I don't know if that solution is better

  5. #5
    Join Date
    Oct 2006
    Location
    Massachusetts, USA
    Posts
    19
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTreeWidget item editing: want column specificity

    That's the difference between an expert (you) and a beginner (me). The latter is ebullient when he spots a "solution".

    Thanks for the replies!

  6. #6
    Join Date
    Mar 2006
    Location
    Mountain View, California
    Posts
    489
    Thanks
    3
    Thanked 74 Times in 54 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QTreeWidget item editing: want column specificity

    In the long run, the model/view approach is best. Then you can have your model's flag() function not make the one column editable.

    The convenience widgets should not be treated as the Qt4 version of the Qt3 item views. They were meant for small simple views. That you're trying to make some columns editable and others not, tells me that you're trying to make QTreeWidget do too much.

  7. #7
    Join Date
    Oct 2006
    Location
    Massachusetts, USA
    Posts
    19
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTreeWidget item editing: want column specificity

    Point taken; thanks.

    But I succeeded in getting the functionality I needed with just +5 lines of code, using a function provided by QTreeWidget itself, not some hack:

    Qt Code:
    1. void QTreeWidget::openPersistentEditor(QTreeWidgetItem*item,int column)
    To copy to clipboard, switch view to plain text mode 

    I capture the user's editing changes with the slot

    Qt Code:
    1. void QTreeWidget::itemChanged(QTreeWidgetItem * item, int column)
    To copy to clipboard, switch view to plain text mode 

    So I feel I was not trying to do too much with the convenience class. I was just slow to find the solution. This feature of my app is a small, simple sidelight, not a central, expandible effort, so I'm done and it's stable. Movin' on ...

    But thanks for the advice.

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QTreeWidget item editing: want column specificity

    What if you just click or tab to another cell of the widget without changing the contents? Does the editor get closed? I still think you are "hacking" the widget a little...

  9. #9
    Join Date
    Oct 2006
    Location
    Massachusetts, USA
    Posts
    19
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTreeWidget item editing: want column specificity

    Good catch. Handling that case was in my solution, though I didn't say so before.

    Qt Code:
    1. void NotQuiteHackingWindow::on_TreeWidgetInQuestion_currentItemChanged( QTreeWidgetItem *current, QTreeWidgetItem *previous)
    2. {
    3. if (previous)
    4. Ui.TreeWidgetInQuestion->closePersistentEditor(previous,2); // does nothing if none open
    5. }
    To copy to clipboard, switch view to plain text mode 

    Line 3 isn't really necessary. And the slot is connected automatically by the UI setup function -- no code needed for that.

    Ok, I concede your point. Thanks.

  10. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QTreeWidget item editing: want column specificity

    I also wonder what happens when the widget looses keyboard focus in favour of other widget... The editor should get closed then.

  11. The following user says thank you to wysota for this useful post:

    McKee (9th December 2006)

  12. #11
    Join Date
    Oct 2006
    Location
    Massachusetts, USA
    Posts
    19
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTreeWidget item editing: want column specificity

    Good thought. I checked that case, and the behaviour seems ideal. If the item is open but no changes have been made, the editor persists in that when the QTableWidget gets focus back, the item is still in edit mode. But if there has been a change to the item when QTableWidget loses focus, it emits itemChanged() and closes the persistent editor. I'm with that.

  13. #12
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QTreeWidget item editing: want column specificity

    This is a strange behaviour. If a widget loses focus, the editor should be closed. Imagine you have 10 such tables. If you change focus between them, you'll soon have 10 editors open. Is that really what you want?

  14. #13
    Join Date
    Oct 2006
    Location
    Massachusetts, USA
    Posts
    19
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTreeWidget item editing: want column specificity

    For the library, that may be an issue. But for my app, it's not. I have many tables, yes, but only 2 are editable, and the "keep-editor-open-if no-change-after-focus-loss" behaviour, though strange, is in my case preferable. It provides more of a "where was I" context when the user returns focus to the widget. If I did need to rectify the strangeness, I suppose there's a way to capture an event for the table widget losing focus and close the editor explcitly. But I need not discover it at this point.

    Have we wrung this thread dry yet?

Similar Threads

  1. Removing items properly from qtreewidget item
    By Djony in forum Qt Programming
    Replies: 6
    Last Post: 21st November 2006, 13:20
  2. Replies: 1
    Last Post: 21st September 2006, 11:37
  3. Get a certain item from a QTreeWidget
    By mace in forum Newbie
    Replies: 2
    Last Post: 28th August 2006, 11:35
  4. Change column width in a QTreeWidget
    By mace in forum Qt Programming
    Replies: 2
    Last Post: 27th May 2006, 13:19
  5. keypress while editing an item in QListWidget
    By Beluvius in forum Qt Programming
    Replies: 3
    Last Post: 4th April 2006, 10:56

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.