Results 1 to 10 of 10

Thread: Use of same widget for display and edit in model-view

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Feb 2011
    Posts
    4
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Use of same widget for display and edit in model-view

    The Qt View/Model uses different principle to display and edit (DisplayRole and EditRole) items.
    The Delegate classes are easy in providing Editing widgets. But for display it provide "paint()" method to be implemented in subclass, which I feel a real pain to implement.

    How would one achieve both Display and Edit by same kind of Widget in a nicer way.
    For example, how to make QTableView to DISPLAY a QString using QLineEdit and also EDIT it by using same kind of Widget (QLineEdit).
    Similarly, how to make QTableView to DISPLAY a bool using QCheckBox and also EDIT it by using same kind of Widget (QCheckBox).

    Thanks in advance.

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

    Default Re: Use of same widget for display and edit in model-view

    If you displayed using widgets, your view would be awfully slow. Imagine having a million widgets and updating positions of each of them when you scroll through the view. If you want widgets then use QScrollArea.The whole idea of rendering using delegates is to avoid widgets.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Feb 2011
    Posts
    4
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Use of same widget for display and edit in model-view

    Hey, thanks for the reply.
    I understand the problem you described.

    However, I have a problem in hand to solve it where in I thought of using the QTableView.

    (see the attachment)

    Can you provide me some input how this can be achived in Qt?
    Attached Images Attached Images

  4. #4
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Use of same widget for display and edit in model-view

    Qt Code:
    1. <Vertical Layout >
    2. < Horizontal layout >
    3. <line edit>
    4. < check box >
    5. < Horizontal layout >
    6. <line edit>
    7. < check box >
    To copy to clipboard, switch view to plain text mode 
    Get the idea ?
    Use above layout in QScrollArea...
    Last edited by aamer4yu; 12th February 2011 at 07:17.

  5. #5
    Join Date
    Feb 2011
    Posts
    4
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Use of same widget for display and edit in model-view

    However, I feel layout are good if number items are fixed.
    Also I would like to have sorting enabled for columns, having headers, dynamic add and delete of rows etc.

    With above approach I would be end of reimplimenting most of the properties of QTableView.
    Is there any alternative approch and a clean solution?


    Added after 1 40 minutes:


    Quote Originally Posted by wysota View Post
    If you displayed using widgets, your view would be awfully slow. Imagine having a million widgets and updating positions of each of them when you scroll through the view. If you want widgets then use QScrollArea.The whole idea of rendering using delegates is to avoid widgets.
    Is there any readymade painter for LineEdit and CheckBox?
    Last edited by NIRANJAN BAI; 12th February 2011 at 09:04.

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

    Default Re: Use of same widget for display and edit in model-view

    You can mimic a widget by using QStyle API. It has a number of draw* methods for drawing widgets. But it's only drawing.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,321
    Thanks
    316
    Thanked 871 Times in 858 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Use of same widget for display and edit in model-view

    I believe you can use Qt::CheckStateRole and Qt::ItemIsUserCheckable flags to display and edit the check box without using a delegate. For the line edit, you can probably cache a pixmap of a QLineEdit containing the cell text and bitblt that in the paint event. This will take some trickery - the delegate isn't visible until the item is edited, so you will need to make a dummy QLineEdit, paint it, and cache it when you initially fill the table and repaint it when the table is resized or changed.

    But from a usability point of view, wouldn't it be very confusing to a user if every cell in a table looked like it was being edited all the time? You would have to pay very close attention to where the input cursor was in order to know which cell you were editing. This is not a very intuitive interface, in my opinion.

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

    Default Re: Use of same widget for display and edit in model-view

    Quote Originally Posted by d_stranz View Post
    I believe you can use Qt::CheckStateRole and Qt::ItemIsUserCheckable flags to display and edit the check box without using a delegate.
    It's quite the opposite. It is the delegate that draws the box. There is no real checkbox widget there.

    For the line edit, you can probably cache a pixmap of a QLineEdit containing the cell text and bitblt that in the paint event.
    You don't need to cache anything. There is an API for this.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  9. #9
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,321
    Thanks
    316
    Thanked 871 Times in 858 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Use of same widget for display and edit in model-view

    Quote Originally Posted by wysota View Post
    It's quite the opposite. It is the delegate that draws the box. There is no real checkbox widget there.
    But you (the application writer) do not need to create an explicit checkbox delegate in order to have an editable checkbox appear in a table, isn't that correct? It is something the framework takes care of for you when you set the appropriate flags. That's what I was getting at.

    You don't need to cache anything. There is an API for this.
    Through QStyle? Or through QStyledItemDelegate or a subclass? Can you point to an example?

    Thanks.

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

    Default Re: Use of same widget for display and edit in model-view

    Quote Originally Posted by d_stranz View Post
    But you (the application writer) do not need to create an explicit checkbox delegate in order to have an editable checkbox appear in a table, isn't that correct?
    There is already a delegate setup for you when you create a view.


    Through QStyle? Or through QStyledItemDelegate or a subclass? Can you point to an example?
    Qt Code:
    1. style()->drawControl(...); // or similar
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Replies: 8
    Last Post: 6th May 2010, 11:17
  2. QTableView - model / view pattern - layout, edit
    By vlastagf in forum Qt Programming
    Replies: 4
    Last Post: 1st August 2009, 22:42
  3. Problems to display a QFont in a View/Model correctly
    By NoRulez in forum Qt Programming
    Replies: 0
    Last Post: 8th July 2009, 12:26
  4. Replies: 1
    Last Post: 16th January 2008, 11:48
  5. Model-view: Display items in different ways
    By taboom in forum Qt Programming
    Replies: 3
    Last Post: 13th August 2007, 19:05

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
  •  
Qt is a trademark of The Qt Company.