Results 1 to 9 of 9

Thread: problem in removing row in QAbstractTableModel

  1. #1
    Join Date
    Sep 2008
    Location
    Bangalore
    Posts
    659
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    116
    Thanked 42 Times in 41 Posts

    Default problem in removing row in QAbstractTableModel

    hi friends,

    Im facing a problem of updating my QAbstractTableModel in QTableView .. when i insert few rows and then i try to remove the rows ...

    this is my insertRow function in TableModel

    Qt Code:
    1. bool TableModel::insertRows(int position, int rows, const QModelIndex &parent)
    2. {
    3. int columns = columnCount();
    4. beginInsertRows(parent, position, position + rows - 1);
    5.  
    6. for (int row = 0; row < rows; ++row) {
    7. QStringList items;
    8. for (int column = 0; column < columns; ++column)
    9. items.append("");
    10. rowList.insert(position, items);
    11. }
    12.  
    13. endInsertRows();
    14. return true;
    15. }
    To copy to clipboard, switch view to plain text mode 


    and my removeRows

    Qt Code:
    1. bool TableModel::removeRows(int position, int rows, const QModelIndex &parent)
    2. {
    3. beginRemoveRows(parent, position, position + rows - 1);
    4.  
    5. for (int row = 0; row < rows; ++row) {
    6. rowList.removeAt(position);
    7. }
    8.  
    9. endRemoveRows();
    10. return true;
    11. }
    To copy to clipboard, switch view to plain text mode 


    and in my main window
    the slot will call minimum 15 times

    Qt Code:
    1. void
    2. MainWindow::receivedData(const dataStruct &data)
    3. {
    4. model-> insertRows(0, 1, QModelIndex());
    5.  
    6. QModelIndex index = model->index(0, 0, QModelIndex());
    7. model->setData(index, data.No, Qt::EditRole);
    8. index = model->index(0, 1, QModelIndex());
    9. model->setData(index, data.x, Qt::EditRole);
    10. index = model->index(0, 2, QModelIndex());
    11. model->setData(index, data.y, Qt::EditRole);
    12. index = model->index(0, 3, QModelIndex());
    13. model->setData(index, data.z, Qt::EditRole);
    14. }
    To copy to clipboard, switch view to plain text mode 


    and one slot called one time after it called 15 times the receiveData

    Qt Code:
    1. void
    2. MainWindow::clearTableSlot()
    3. {
    4.  
    5. qDebug()<<"Row Counting:"<<ui.targetTable->model()->rowCount();
    6. for(int i =0; i < ui.targetTable->model()->rowCount(); i++)
    7. ui.targetTable->model()->removeRow(i);
    8.  
    9. }
    To copy to clipboard, switch view to plain text mode 

    and again i will try to add rows so i can update it ...
    but clearTableSlot() is not not removing the rows ...
    its not removing the rows so that
    the rows are appending one after another ...


    please help and thanks in advance
    "Behind every great fortune lies a crime" - Balzac

  2. #2
    Join Date
    Sep 2011
    Posts
    1,241
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    3
    Thanked 127 Times in 126 Posts

    Default Re: problem in removing row in QAbstractTableModel

    Qt Code:
    1. for(int i =0; i < ui.targetTable->model()->rowCount(); i++)
    2. ui.targetTable->model()->removeRow(i);
    To copy to clipboard, switch view to plain text mode 
    Think about this for a while, or even check what it does in your debugger. It will not be doing what you intend...
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  3. The following user says thank you to amleto for this useful post:

    wagmare (29th November 2012)

  4. #3
    Join Date
    Sep 2008
    Location
    Bangalore
    Posts
    659
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    116
    Thanked 42 Times in 41 Posts

    Default Re: problem in removing row in QAbstractTableModel

    Quote Originally Posted by amleto View Post
    Qt Code:
    1. for(int i =0; i < ui.targetTable->model()->rowCount(); i++)
    2. ui.targetTable->model()->removeRow(i);
    To copy to clipboard, switch view to plain text mode 
    Think about this for a while, or even check what it does in your debugger. It will not be doing what you intend...
    thanks for the reply first ..
    i know i did something wrong .. in the way i try to remove the row . i m new to this Model/View table things ... first time to say. could u please elaborate it more ..

    Did i need QModelIndex to remove the rows ..?
    Last edited by wagmare; 29th November 2012 at 06:10.
    "Behind every great fortune lies a crime" - Balzac

  5. #4
    Join Date
    Sep 2011
    Posts
    1,241
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    3
    Thanked 127 Times in 126 Posts

    Default Re: problem in removing row in QAbstractTableModel

    its not a problem with using Qt. It's a logic problem. Use your debugger! Learning how to use a debugger is not optional!
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  6. #5
    Join Date
    Sep 2008
    Location
    Bangalore
    Posts
    659
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    116
    Thanked 42 Times in 41 Posts

    Default Re: problem in removing row in QAbstractTableModel

    mean like this

    Qt Code:
    1. for(int i = model->rowCount() -2 ; i >= 0; i -=1)
    2. {
    3. model->removeRow(i,QModelIndex());
    4.  
    5.  
    6. }
    To copy to clipboard, switch view to plain text mode 
    "Behind every great fortune lies a crime" - Balzac

  7. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,376
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: problem in removing row in QAbstractTableModel

    That won't delete the last row.
    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.


  8. #7
    Join Date
    Sep 2008
    Location
    Bangalore
    Posts
    659
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    116
    Thanked 42 Times in 41 Posts

    Default Re: problem in removing row in QAbstractTableModel

    Quote Originally Posted by wysota View Post
    That won't delete the last row.
    Sorry , sorry ..
    for(int i = model->rowCount() -1 ; i >= 0; i -=1)
    {
    model->removeRow(i,QModelIndex());


    }
    "Behind every great fortune lies a crime" - Balzac

  9. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,376
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: problem in removing row in QAbstractTableModel

    Why not simply:

    Qt Code:
    1. model->removeRows(0, model->rowCount());
    To copy to clipboard, switch view to plain text mode 

    ?
    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.


  10. The following user says thank you to wysota for this useful post:

    wagmare (29th November 2012)

  11. #9
    Join Date
    Sep 2011
    Posts
    1,241
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    3
    Thanked 127 Times in 126 Posts

    Default Re: problem in removing row in QAbstractTableModel

    and a c++ tidbit:

    i -= 1;
    is normally written
    --i;
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

Similar Threads

  1. Replies: 3
    Last Post: 18th April 2014, 03:37
  2. Container's removing item problem.
    By zgulser in forum Qt Programming
    Replies: 2
    Last Post: 23rd July 2012, 12:36
  3. Replies: 1
    Last Post: 5th May 2011, 09:37
  4. QAbstractTableModel SetRowCount problem
    By gutiory in forum Qt Programming
    Replies: 2
    Last Post: 25th November 2010, 07:48
  5. QAbstractTableModel for QTreeView?
    By Michiel in forum Qt Programming
    Replies: 5
    Last Post: 15th May 2007, 09:09

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.