Results 1 to 6 of 6

Thread: Detect when the content of a cell of QTableView is changed

  1. #1
    Join Date
    May 2012
    Posts
    99
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Question Detect when the content of a cell of QTableView is changed

    Hi all,

    I need monitoring the changes in the data content of a cell of QTableView.

    I have a QTableView widget into QMainWindow.

    I have tried with the QAbstractItemView signals, the slots are called in my QMainWindow class, but neither of them accomplish the target that I need.

    I have tried with the currentChanged( const QModelIndex & current, const QModelIndex & previous ) signal of QItemSelectionModel, as above the slot is called, but itsn't accomplish the target that I need too.

    I make the connects like this:

    Qt Code:
    1. connect(ui->myTableView->selectionModel(), SIGNAL(currentChanged ( QModelIndex const&, QModelIndex const& )), this, SLOT(onCurrentChanged(QModelIndex con7st&, QModelIndex const&)));
    2. connect(ui->myTableView, SIGNAL(pressed(const QModelIndex &)), this, SLOT(onPressed(const QModelIndex &)));
    To copy to clipboard, switch view to plain text mode 

    There are any signal / slot / method or similar that I could use to accomplish my target?

    Best regards.

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Detect when the content of a cell of QTableView is changed

    You are looking in the wrong direction. To get a signal when the data changes you need to be looking at the data model not the view: QAbstractItemModel::dataChanged() for example.

  3. #3
    Join Date
    Dec 2010
    Location
    South Africa
    Posts
    56
    Thanks
    8
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Maemo/MeeGo

    Default Re: Detect when the content of a cell of QTableView is changed

    When i first started i tried QTableView and had endless problems myself

    I then switched to Qtable Widget. And cell changed events and so forth are much easier to use.

    Hope this helps

  4. #4
    Join Date
    May 2012
    Posts
    99
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Thumbs up Re: Detect when the content of a cell of QTableView is changed

    Quote Originally Posted by ChrisW67 View Post
    You are looking in the wrong direction. To get a signal when the data changes you need to be looking at the data model not the view: QAbstractItemModel::dataChanged() for example.
    Hi ChrisW67,

    I have used this new code:

    Qt Code:
    1. connect(ui->myTableView->model(), SIGNAL(dataChanged(const QModelIndex&, const QModelIndex&)), this, SLOT(onDataChanged(const QModelIndex&, const QModelIndex&)));
    2.  
    3. ...
    4.  
    5. void myClass::onDataChanged(const QModelIndex& topLeft, const QModelIndex& bottomRight)
    6. {
    7. qDebug() << "the cell has changed";
    8. }
    To copy to clipboard, switch view to plain text mode 

    And works fine!

    Thank you.

  5. #5
    Join Date
    Aug 2021
    Posts
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Detect when the content of a cell of QTableView is changed

    Quote Originally Posted by qt_developer View Post
    Hi ChrisW67,

    I have used this new code:

    Qt Code:
    1. connect(ui->myTableView->model(), SIGNAL(dataChanged(const QModelIndex&, const QModelIndex&)), this, SLOT(onDataChanged(const QModelIndex&, const QModelIndex&)));
    2.  
    3. ...
    4.  
    5. void myClass::onDataChanged(const QModelIndex& topLeft, const QModelIndex& bottomRight)
    6. {
    7. qDebug() << "the cell has changed";
    8. }
    To copy to clipboard, switch view to plain text mode 

    And works fine!

    Thank you.

    Hi can you give the whole code ?

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

    Default Re: Detect when the content of a cell of QTableView is changed

    Hi can you give the whole code ?
    What you do in response to a dataChanged() signal is specific to your application. Any "whole code" for this thread would probably have no meaning in your application.

    I suggest you start with reading about Qt Model/View Programming in the documentation, look at the Model/View tutorial, and finally look at some of the 19 examples of Model/View programming that come with Qt.

    You will probably learn enough in that to understand how to use the QAbstractItemModel::dataChanged() signal in your own application.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. How to detect when item is changed in the table view?
    By schmimona in forum Qt Programming
    Replies: 1
    Last Post: 15th August 2011, 10:06
  2. Getting Cell Content of QTableWidget
    By Kclark in forum Qt Programming
    Replies: 1
    Last Post: 27th December 2010, 14:30
  3. Value changed in a QTableView field
    By nittalope in forum Newbie
    Replies: 4
    Last Post: 12th August 2009, 10:21
  4. Replies: 6
    Last Post: 18th January 2008, 00:46
  5. Replies: 9
    Last Post: 23rd November 2006, 12:39

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.