Results 1 to 7 of 7

Thread: How to access QTableWidget's default items

  1. #1
    Join Date
    Aug 2010
    Posts
    65
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default How to access QTableWidget's default items

    Whenever QTableWidget is created, it comes with enough default items to fill the number of cells required.

    Is there a way of altering these default items? Or does one have to populate it with one's own QTableWidgetItem's?

    I've tried using QTableWidget::item(), but it returns a NULL pointer, which the documentation states as meaning it hasn't been set. Which indicates to me that unless the Item is inserted manually, it's not accessible. However, that seems a tad bit silly to me, so I'd like to know if there is another way of accessing these default items.

  2. #2
    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: How to access QTableWidget's default items

    Whenever QTableWidget is created, it comes with enough default items to fill the number of cells required.
    I am not aware of any "default" items that QTableWidget creates on its own.
    Could you explain with code what you mean?
    ==========================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.

  3. #3
    Join Date
    Aug 2010
    Posts
    65
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to access QTableWidget's default items

    Simply the cells which are naturally displayed when you create a QTableWidget without populating it with your own QTableWidgetItem's.

    i.e.
    Qt Code:
    1. int main(int argc,char* argv[])
    2. {
    3. QApplication QtApp(argc,argv)
    4. QTableWidget table(2,3);
    5. table.show();
    6. return QtApp.exec();
    7. }
    To copy to clipboard, switch view to plain text mode 
    This presents a 2x3 table of empty cells. The cells are selectable and editable, and are thus (as I call it) populated with default items. My question is: can these items be accessed?

  4. #4
    Join Date
    Aug 2010
    Posts
    65
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to access QTableWidget's default items

    Can someone please advise?

  5. #5
    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: How to access QTableWidget's default items

    he cells are selectable and editable, and are thus (as I call it) populated with default items.
    This is wrong.
    The table size (rows and columns) is given, so the table has that size.
    The cells are NOT populated with items, which is why you get NULL items when you ask for them.
    Once you populate the cells with items you will be able to access these 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.

  6. #6
    Join Date
    Aug 2010
    Posts
    65
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to access QTableWidget's default items

    I understand that the table isn't populated with widgets, but it is clearly populated with... something. As I said, that short, four-line code presents a table with cells that are selectable and whose contents are editable. So something is in each of those cells.

    Its just that the thing I'm going to populate the list with the most is QLineEdits, so cells that are selectable and editable is exactly what I'm going for. If I could simply access the default somethings that populate the table, I wouldn't have to allocate a few dozen additional items.

    However, that firm response does answer my question. Whatever populates the table is clearly out of reach.

  7. #7
    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: How to access QTableWidget's default items

    but it is clearly populated with... something.
    That something is a QTableWidgetItem. (when it is valid)

    Whatever populates the table is clearly out of reach.
    The cells you see are only an empty shell.
    The data model is still empty, and there are NO items in it.
    Read thoroughly the QTableWidget docs, you will see its a convenience widget that uses an item based model.
    Its a specialized (often used) implementation of the Model/View paradigm, which you can also find in the docs, and see how it work in Qt.
    Your main problem is that you mix between concepts.

    Its just that the thing I'm going to populate the list with the most is QLineEdits, so cells that are selectable and editable is exactly what I'm going for. If I could simply access the default somethings that populate the table, I wouldn't have to allocate a few dozen additional items.
    Read about Model/View in Qt, how a model interacts with data aware Widgeta, and what is the use of delegates.
    That will get you on the right track.

    NOTE:
    Since the trolls made abstraction of the way data aware widgets work, with models, and delegates, it got more complected, but at the same time semantically more correct and more flexible.
    For people who need simple item based widget, which is often the case, there are some convenience classes, the as QTableWidget.
    But they are nothing more than a specialized implementation of the more abstract classes and concepts, which you have to understand it you want to work with them correctly.
    ==========================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. QTableWidget Default width of row and column
    By sagirahmed in forum Newbie
    Replies: 4
    Last Post: 11th October 2010, 04:01
  2. How to QTableWidget default values on insertion?
    By RawArkanis in forum Qt Programming
    Replies: 1
    Last Post: 20th April 2010, 07:02
  3. access menubar items by name
    By navid in forum Newbie
    Replies: 2
    Last Post: 7th December 2009, 13:15
  4. Dynamic access of ui items
    By Matze-o in forum Qt Programming
    Replies: 1
    Last Post: 6th November 2008, 23:13
  5. Replies: 3
    Last Post: 13th September 2008, 15:12

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.