Results 1 to 5 of 5

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

  1. #1
    Join Date
    Jul 2015
    Posts
    2
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

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

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

    Hi, in Qt I have a QMainWindow -> centralWidget (QWidget) -> QtabWidget -> then 10 Tabs (QWidgets) -> each with up to 26 QtableWidgets:





    Is there is a way to link some of the Items of each that are actually the same Item but it's repeated in each tableWidget, like for example the Player Name is the same per row in each tableWidget, so if the user edits the name in one, it should change it in the same row in every tableWidget.

    I could use the signal

    Qt Code:
    1. void QTableWidget::cellChanged(int row, int column)
    To copy to clipboard, switch view to plain text mode 

    But then if the user had sorted on one of the Tabs, the Row and Column numbers won't match anymore on every Tab.


    I would also like that if I sort by column in one of the Tabs(tableWidget), the new arrangement of the rows should be the same in every Tab.

    Can anyone point me in the correct direction?


    By the way, whats the difference between
    Qt Code:
    1. void QTableWidget::cellChanged(int row, int column)
    To copy to clipboard, switch view to plain text mode 
    and
    Qt Code:
    1. void QTableWidget::itemChanged(QTableWidgetItem * item)
    To copy to clipboard, switch view to plain text mode 
    . Because I've been able to use QTableWidget::cellChanged correctly but I've no idea how to use QTableWidget::itemChanged, how do you use it, an example please.

    Thanks a lot for your time.

  2. #2
    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: Is there a way to link or share a QTableWidgetItem between different QTableWidget

    You can't share the same QTableWidgetItem among tables, because the QTableWidget takes ownership of the item.

    If you convert to QTableView and implement a QAbstractItemModel to hold the information you want to display, then you can use the same model instance in as many tables as you want. Each view can have a completely independent sort of the model. When you edit, you are editing the model, not the view of the model, so changes to the model are reflected in every view.

    Because I've been able to use QTableWidget::cellChanged correctly but I've no idea how to use QTableWidget::itemChanged, how do you use it, an example please.
    There's essentially no difference. Both signals are emitted when the data in the cell changes. In one case, the widget is telling you the cell indexes, which you can use to retrieve the QTableWidgetItem for that cell; in the other case, the widget is telling you the QTableWidgetItem pointer, which you can use to retrieve the cell indexes where it lives.

  3. #3
    Join Date
    Jul 2015
    Posts
    2
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

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

    Quote Originally Posted by d_stranz View Post
    You can't share the same QTableWidgetItem among tables, because the QTableWidget takes ownership of the item.

    If you convert to QTableView and implement a QAbstractItemModel to hold the information you want to display, then you can use the same model instance in as many tables as you want. Each view can have a completely independent sort of the model. When you edit, you are editing the model, not the view of the model, so changes to the model are reflected in every view.
    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.


    Quote Originally Posted by d_stranz View Post
    There's essentially no difference. Both signals are emitted when the data in the cell changes. In one case, the widget is telling you the cell indexes, which you can use to retrieve the QTableWidgetItem for that cell; in the other case, the widget is telling you the QTableWidgetItem pointer, which you can use to retrieve the cell indexes where it lives.
    Yeah, I used itemChanged without arguments and it worked the same as cellChanged:

    Qt Code:
    1. void MainWindow::on_tableWidget_Players_itemChanged()
    2. {
    3. ui->tableWidget_Players->item(0,1)->setData(Qt::DisplayRole, QVariant(QString::number((((ui->tableWidget_Players->item(0,0))->text().toFloat()) / 3),'f',1)));
    4. }
    To copy to clipboard, switch view to plain text mode 

    But could you give me an example of how should the QTableWidgetItem pointer go on itemChanged(QTableWidgetItem * item), I don't know how and can't find examples in Google.


    Thanks for your time.

  4. #4
    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: 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 05:00.

  5. #5
    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: Is there a way to link or share a QTableWidgetItem between different QTableWidget

    Quote Originally Posted by Imnus View Post
    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.
    You should have one model with columns you want to show in all the tabs and simply hide the columns you don't want to show in each of the views or alternatively use a filtering model which will hide the columns.
    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: 3
    Last Post: 9th January 2012, 02: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, 06:39
  3. QTableWidgetItem for a QTableWidget
    By Archa4 in forum Newbie
    Replies: 1
    Last Post: 28th April 2011, 12:11
  4. QTableWidget or QTableWidgetItem CSS
    By ajayajgdeva in forum Newbie
    Replies: 0
    Last Post: 5th February 2010, 14:47
  5. QTableWidget QTableWidgetItem
    By TheKedge in forum Qt Programming
    Replies: 3
    Last Post: 6th September 2006, 16: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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.