Results 1 to 5 of 5

Thread: Is there a way to link or share a QTableWidgetItem between different QTableWidget

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,349
    Qt products
    Qt5
    Platforms
    Windows
    Thanks
    318
    Thanked 872 Times in 859 Posts

    Default Re: Is there a way to link or share a QTableWidgetItem between different QTableWidget

    OK, let's see if I understand, I create a model that for example includes the Player Name, that would go in every Table Tab and will be the same so it's reflected in every Tab (view). Now I can add more to that base model right like sub-classes, right? So they all have Player Name but each Tab (view) has it's own Items.
    Not quite. To implement your own model, you need to derive a class from QAbstractTableModel. In that class, you need to re-implement certain methods, like rowCount(), columnCount(), data(), index(), and optionally headerData(). If the model is editable, you need to implement setData() as well.

    Presumably, you have your own data structure that represents your teams, players, or whatever it is. Your table model can contain a pointer to that data structure, or in some way have access to it.

    rowCount() is going to return the number of players, for example. columnCount() is going to return the total number of columns of information for each player. data() can return a bunch of different things, depending on the value of the "role" argument that is passed in. For Qt:: DisplayRole, for example, if the player name goes in column 0, you will return the name of the first player when data() asks for the DisplayRole for the QModelIndex with row = 0 and column = 0. Row = 1, column = 0 is the name of the second player, and so forth.

    You should study this tutorial.

    If your tables each contain slightly different information (and why would you want 28 tables all with the same information?), then you can teach yourself about proxy models. You still have only a single base model that contains all of the information you might want to display anywhere, but you create a proxy model for use by a particular view to select out the subset of the information you want in a particular table. See QSortFilterProxyModelfor one.

    But could you give me an example of how should the QTableWidgetItem pointer go on itemChanged(QTableWidgetItem * item)
    I think maybe you are confused. This is a signal. The view emits the signal after the item is changed, and the signal contains the pointer to the item that was changed. You don't call this method, it gets connected to your slot. Your slot receives the pointer value as an argument, and you use it to determine which row and column was changed (item->row() and item->column()).

    If you ignore the QTableWidgetItem pointer, then your code is assuming that it gets emitted only for column 0, when in fact it gets emitted any time the value in any cell gets changed. The value of the pointer tells you which cell it is.

    Qt Code:
    1. connect( myTableWidget, SIGNAL( itemChanged( QTableWidgetItem * ) ), myClass, SLOT( mySlot( QTableWidgetItem * ) ) );
    2.  
    3. //...
    4.  
    5. void MyClass::mySlot( QTableWidgetItem * item )
    6. {
    7. // do something with item
    8. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by d_stranz; 11th July 2015 at 04:00.

Similar Threads

  1. Replies: 3
    Last Post: 9th January 2012, 01:39
  2. how to share link of an video to my facebook account/twitter account
    By Anshuman in forum Qt for Embedded and Mobile
    Replies: 0
    Last Post: 29th April 2011, 05:39
  3. QTableWidgetItem for a QTableWidget
    By Archa4 in forum Newbie
    Replies: 1
    Last Post: 28th April 2011, 11:11
  4. QTableWidget or QTableWidgetItem CSS
    By ajayajgdeva in forum Newbie
    Replies: 0
    Last Post: 5th February 2010, 13:47
  5. QTableWidget QTableWidgetItem
    By TheKedge in forum Qt Programming
    Replies: 3
    Last Post: 6th September 2006, 15:03

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.