Results 1 to 4 of 4

Thread: how to get notified when a new row is added into a QTableView ?

  1. #1
    Join Date
    Oct 2013
    Posts
    6
    Thanks
    1
    Thanked 3 Times in 3 Posts

    Smile how to get notified when a new row is added into a QTableView ?

    hi,
    i need to do some operation to every row of a QTableView, but the number of rows is varying from time to time, thus i need to get notified when row count changes in a QTableView?
    how can i do that ?

    i tried the following, but 'rowCountChanged' is never called.

    class TableView : public QTableView
    {
    Q_OBJECT

    public:

    TableView(QWidget *parent = 0) : QTableView(parent)
    {

    }

    protected Q_SLOTS:

    void rowCountChanged(int oldCount, int newCount)
    {
    /// CP1
    }
    };
    Last edited by aresa; 21st October 2013 at 09:09.

  2. #2
    Join Date
    Feb 2011
    Location
    Bangalore
    Posts
    207
    Thanks
    20
    Thanked 28 Times in 27 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: how to get notified when a new row is added into a QTableView ?

    Are you trying some excel like functionality, which has a star as last element, which you can click to add another row?
    Or you just need a button, clicking on which adds a row to the table?
    In case 1, you need to derive class from QHeaderView, and set the new Qheaderview in the tableview.
    Case2 is simpler. route the signal from button add to the model, and follow the procedure to add row at a position.

  3. #3
    Join Date
    Nov 2010
    Posts
    315
    Thanked 53 Times in 51 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: how to get notified when a new row is added into a QTableView ?

    You doing that wrong. Read about signal slot mechanism since apparently you do not understand that.
    I'm suspecting something like:
    Qt Code:
    1. class MyMainWindow : public QMainWindow {
    2. Q_OBJECT
    3.  
    4. public slots:
    5. void onRowsNuberChanged();
    6. };
    7.  
    8. MyMainWindow::MyMainWindow(QWidget *parent) : QMainWindow(parent) {
    9. ...
    10. connect(ui->tableView->model(), SIGNAL(rowsInserted(QModelIndex,int,int)), this, SLOT(onRowsNuberChanged()));
    11. connect(ui->tableView->model(), SIGNAL(rowsRemoved(QModelIndex,int,int)), this, SLOT(onRowsNuberChanged()));
    12. }
    13.  
    14. void MyMainWindow::onRowsNuberChanged() {
    15. int rowsCount = ui->tableView->model()->rowCount();
    16. // do whatever you need
    17. ...
    18. }
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Oct 2013
    Posts
    6
    Thanks
    1
    Thanked 3 Times in 3 Posts

    Default Re: how to get notified when a new row is added into a QTableView ?

    Ah, i just found QAbstractItemModel provides such a signal as rowadded. that's almost what i want.
    it is the model not the view that emits such a signal. thanks.

Similar Threads

  1. How to be notified on dragEnterEvent?
    By tuxit in forum Qt Quick
    Replies: 6
    Last Post: 12th August 2011, 12:29
  2. Replies: 1
    Last Post: 28th October 2010, 22:36
  3. Replies: 2
    Last Post: 27th October 2010, 11:23
  4. How to be notified when a child changes?
    By mooreaa in forum Qt Programming
    Replies: 1
    Last Post: 3rd July 2008, 13:04
  5. Data not being added to my QTableView?
    By steg90 in forum Qt Programming
    Replies: 1
    Last Post: 20th November 2007, 14:20

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.