Results 1 to 10 of 10

Thread: QTableView add a remove button to each row

  1. #1
    Join Date
    May 2012
    Posts
    136
    Thanks
    2
    Thanked 27 Times in 24 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default QTableView add a remove button to each row

    What's the best way to add a remove button to each row in a QTableView (without modifying the model)
    Is there anyway to do this in a delegate (the model doesn't have a row for the button)

  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: QTableView add a remove button to each row

    In the delegate you should draw something that looks like a button using QStyle and handle events for clicking such artificial button.
    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
    May 2012
    Posts
    136
    Thanks
    2
    Thanked 27 Times in 24 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QTableView add a remove button to each row

    Thx for the answer, this was not what I was hoping for

    I was thinking of using a proxymodel to add the extra column and add a styleddelegate to it, dunno if this is possible

    just was checing into QProxyModel but it says its obsolete, it looks like i will need to write my own. oh just found that there is a QAbstractProxymodel I can use


    I've just tried it and it seems to work with a QAbstractProxymodel and a delegate.
    The proxymodel just adds an empty column and the delegate creates an pesistent editor in it (a modified QPushbutton)
    It's not clean but it works
    Last edited by StrikeByte; 13th November 2014 at 15:05.

  4. #4
    Join Date
    Jan 2010
    Posts
    73
    Thanks
    6
    Thanked 8 Times in 8 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTableView add a remove button to each row

    Quote Originally Posted by StrikeByte View Post
    I was thinking of using a proxymodel to add the extra column and add a styleddelegate to it, dunno if this is possible

    just was checing into QProxyModel but it says its obsolete, it looks like i will need to write my own. oh just found that there is a QAbstractProxymodel I can use
    Smart thinking.....

    Can you use the QSortFilterProxyModel? It is not marked as deprecated / obsolete.

    http://qt-project.org/doc/qt-5/qsort...roxymodel.html

  5. #5
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QTableView add a remove button to each row

    I've just tried it and it seems to work with a QAbstractProxymodel and a delegate.
    The proxymodel just adds an empty column and the delegate creates an pesistent editor in it (a modified QPushbutton)
    I started to suggest this yesterday, but didn't have the time to complete my reply. Glad you tried it and got it to work.

    Can you use the QSortFilterProxyModel? It is not marked as deprecated / obsolete.
    QSortFilterProxyModel is not appropriate for this use. It is used in cases where you want to 1) remove certain rows or columns from the source model and / or 2) display the source model sorted in a different order without changing the source model. There are several additional methods that must be implemented in QSortFilterProxyModel which have no relevance to the OP's problem. In his case, he wants to pass the source model unchanged, and simply add a new virtual column with a pushbutton in it. Depending on where he adds the virtual column, he won't have to do anything in QAbstractProxyModel's virtual methods except return QModelIndex instances that map directly to or from the same row and column of the source.

  6. #6
    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: QTableView add a remove button to each row

    Quote Originally Posted by StrikeByte View Post
    I've just tried it and it seems to work with a QAbstractProxymodel and a delegate.
    The proxymodel just adds an empty column and the delegate creates an pesistent editor in it (a modified QPushbutton)
    It's not clean but it works
    Did you try it with a large model, say... 1000 rows? What was the performance?
    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
    May 2012
    Posts
    136
    Thanks
    2
    Thanked 27 Times in 24 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QTableView add a remove button to each row

    No sorry my model only contains like 50 rows

    [edit]
    just did a quick test with 5000 items, took 8 seconds (my model does some name checking an makes a unique name for each item, need to check it for a faster algorithm)
    adding items to the model doesnt make a difference when using the proxymodel or not
    scrolling through the list works just fine

    I still do not know what happens on a slow pc

    thx for the question, now I see I need to optimize my model a bit
    [/edit]
    Last edited by StrikeByte; 14th November 2014 at 15:54.

  8. #8
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QTableView add a remove button to each row

    need to check if for a faster algorithm
    You might be able to use qHash() or one of the Qt classes that incorporate it (QMap, QSet, or QHash). You probably don't need to store the keys, just the hash values.

  9. #9
    Join Date
    May 2012
    Posts
    136
    Thanks
    2
    Thanked 27 Times in 24 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QTableView add a remove button to each row

    Thx for the tip, but all the items are stored in a QMap already, with theire internal unique key.

    The algorithm is making a unique name for the users input:
    for example if a user adds an item called "test" if he/she tries to add another item called "test" it will rename it to "test_1", another "test_2",,, etc

  10. #10
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QTableView add a remove button to each row

    If you are doing this in response to user input, then it is unlikely that performance will ever be an issue. Even if you have a large number of names stored, the time it takes to look one up in a QMap (whether it finds it or not) is negligible compared to human response time.

Similar Threads

  1. Remove row button in QTabelview
    By guidupas in forum Qt Programming
    Replies: 2
    Last Post: 10th March 2014, 14:31
  2. Remove max,min button on a QMainWindow
    By Krish_ng in forum Qt Programming
    Replies: 9
    Last Post: 3rd August 2012, 13:36
  3. Replies: 2
    Last Post: 26th April 2011, 11:44
  4. how to remove tab with closable button.
    By ishkabible in forum Newbie
    Replies: 8
    Last Post: 20th September 2010, 02:03
  5. Remove restore button
    By vermarajeev in forum Qt Programming
    Replies: 3
    Last Post: 26th June 2007, 13:29

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.