Results 1 to 8 of 8

Thread: QTableWidget cellwidget selection

  1. #1
    Join Date
    Jul 2012
    Posts
    244
    Thanks
    27
    Thanked 15 Times in 14 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default QTableWidget cellwidget selection

    I have a qtablewidget, which i populated with widgets (setCellWidget()). So it doesnt contain any QTableWidgetItems, but only widgets.
    I can select the widgets in the table - but how do i get a list of selected cells?

    All the IsSelected() stuff operates on QTableWidgetItems, which i dont have.

    there is a method to return indexes (QModelIndexList), but it's protected and i cant access it.


    how do i do it?

  2. #2
    Join Date
    Nov 2006
    Location
    indonesia
    Posts
    55
    Thanked 11 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTableWidget cellwidget selection

    Hi,
    If you want to get selected item in QTableWidget, you can use :
    Qt Code:
    1. QList<QTableWidgetItem *> QTableWidget::selectedItems ()
    To copy to clipboard, switch view to plain text mode 

    Best regards,

    Toto

  3. #3
    Join Date
    Jul 2012
    Posts
    244
    Thanks
    27
    Thanked 15 Times in 14 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTableWidget cellwidget selection

    yes, but i dont have any items, only widgets. So that call always returns an empty list, because no items exists to begin with.

  4. #4
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QTableWidget cellwidget selection

    Since QTableWidgetItem is not a QObject, you can try and create a widget which subclasses from QTableWidgetItem:
    Qt Code:
    1. class WidgetItem : public QWidget, public QTableWidgetItem
    2. {
    3. ...
    4. };
    To copy to clipboard, switch view to plain text mode 

    Then you can cast your pointer to both QWitdget and QTableWidgetItem allowing you use it both in setCellWidget as well as setItem() allowing you to use the model view part for the selctions and the visible part of cell widget.

    I never tried it so no warranty on this ;-)
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  5. #5
    Join Date
    Jul 2012
    Posts
    244
    Thanks
    27
    Thanked 15 Times in 14 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTableWidget cellwidget selection

    clever & sneaky!


    I'll give it a shot later on, but it doesnt feel very clean. If someone know an "official" way, please do tell.

  6. #6
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QTableWidget cellwidget selection

    What is not clean is the way you are using QTableWidget.
    It is an item based widget, and you are not using it with items which is what my solution actually solves.
    It wont get much cleaner or more "official" than that - except a proper usage of items.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  7. #7
    Join Date
    Jul 2012
    Posts
    244
    Thanks
    27
    Thanked 15 Times in 14 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTableWidget cellwidget selection

    is there a better/more official way to use QWidgets in a "table-like" way?

    Anyways, you solution works just fine. Thanks!

  8. #8
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QTableWidget cellwidget selection

    The question is what exactly you are trying to achieve.
    You said very little about the motivation or requirements of what you are doing, so its hard to say.
    It sounds almost as what you really need is not a table but a layout.
    But I can't really tell based on what you have supplied so far.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

Similar Threads

  1. Align a QPushButton in the cellwidget of QTableWidget
    By rawfool in forum Qt Programming
    Replies: 3
    Last Post: 9th May 2012, 08:55
  2. How to set focus to QTableWidget's cellWidget
    By danczer in forum Qt Programming
    Replies: 1
    Last Post: 21st February 2011, 16:12
  3. QtableWidget moving cellWidget
    By hubipet in forum Qt Programming
    Replies: 0
    Last Post: 22nd October 2010, 07:06
  4. QTableWidget & cellWidget : some pointers
    By prashant in forum Qt Programming
    Replies: 1
    Last Post: 18th October 2009, 10:48
  5. Selection in QTableWidget
    By Nippler in forum Qt Programming
    Replies: 3
    Last Post: 30th April 2008, 08:32

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.