Results 1 to 6 of 6

Thread: qtableview add row on buttonclicked

  1. #1
    Join Date
    Mar 2009
    Posts
    34
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default qtableview add row on buttonclicked

    Hello,

    How can i add a new row with 2 colums to a qtableview when a button was clicked?

    I also plan to remove a single selected row on a button click.

    I will have max. 20 rows aka 2 colums. Does it make sense to use a qtableview here or should i use a qtablewidget?

    Thanks,
    Surf

  2. #2
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: qtableview add row on buttonclicked

    In my opinion, your use case is simple enough that QTableWidget is sufficient. If you will be working with Qt and taking on more and more complex data structures and views, then the model/view paradigm will provide more flexibility and more control, etc.

    So, do you want down and dirty for this one use case or do you want to invest some time to learn Qt's model/view classes which you can leverage for more complex projects down the road?
    I write the best type of code possible, code that I want to write, not code that someone tells me to write!

  3. #3
    Join Date
    Mar 2009
    Posts
    34
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: qtableview add row on buttonclicked

    thanks for the quick reply.

    i want to investigate more time with the model/view approach.

    i currently use a QTableView with a QStandardItemModel.

    once i create a new QStandardItem, will i need to manually delete the memory after having it allocated on the heap?

    that how i do:
    Qt Code:
    1. MainWindow::MainWindow(QWidget *parent) :
    2. QMainWindow(parent),
    3. ui(new Ui::MainWindow)
    4. {
    5. ui->setupUi(this);
    6.  
    7. model = new QStandardItemModel(0,2);
    8.  
    9. ui->tableView->setSelectionBehavior(QAbstractItemView::SelectRows);
    10. ui->tableView->setSelectionMode(QAbstractItemView::SingleSelection);
    11. ui->tableView->setModel(model);
    12. ui->tableView->setVerticalScrollBar(new QScrollBar(ui->tableView));
    13. ui->tableView->show();
    14.  
    15. QObject::connect(ui->pushButton_add, SIGNAL(clicked()), this, SLOT(onAddClicked()));
    16. QObject::connect(ui->pushButton_remove, SIGNAL(clicked()), this, SLOT(onRemoveClicked()));
    17. }
    18.  
    19. void MainWindow::onAddClicked()
    20. {
    21. qDebug() << "onAddClicked...";
    22.  
    23. QList<QStandardItem*> newRow;
    24. QStandardItem *item1 = new QStandardItem(QString("col1"));
    25. QStandardItem *item2 = new QStandardItem(QString("col2"));
    26. newRow.append(item1);
    27. newRow.append(item2);
    28. model->appendRow(newRow);
    29. }
    30.  
    31. void MainWindow::onRemoveClicked()
    32. {
    33. qDebug() << "onRemoveClicked...";
    34.  
    35. QModelIndex currentIndex = ui->tableView->selectionModel()->currentIndex();
    36. model->removeRow(currentIndex.row());
    37. }
    To copy to clipboard, switch view to plain text mode 

    will the QTableView always automatically update when i update the QStandardItemModel?
    Last edited by Surfman19; 7th July 2015 at 10:51.

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: qtableview add row on buttonclicked

    Quote Originally Posted by Surfman19 View Post
    once i create a new QStandardItem, will i need to manually delete the memory after having it allocated on the heap?
    Only if you don't add them to the model. If you do, the model takes ownership.

    Quote Originally Posted by Surfman19 View Post
    will the QTableView always automatically update when i update the QStandardItemModel?
    Yes, the model will signal all views operating on it when it has changed.

    Cheers,
    _

  5. #5
    Join Date
    Mar 2009
    Posts
    34
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: qtableview add row on buttonclicked

    qtableview works fine!

    i currently use the QStandardItemModel. are there any other models available and how the differ?

  6. #6
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: qtableview add row on buttonclicked

    I suggest that you do a little research regarding Qt's model/view classes. If you have specific questions regarding using the model/view classes, then come back here once you have read the docs and attempted to write some code.
    I write the best type of code possible, code that I want to write, not code that someone tells me to write!

Similar Threads

  1. QTableView add row
    By Seth90 in forum Qt Programming
    Replies: 11
    Last Post: 16th March 2017, 09:27
  2. QTableView and QLineEdit OUTSIDE of QTableView, like Excel
    By JoZCaVaLLo in forum Qt Programming
    Replies: 10
    Last Post: 13th May 2011, 13:20
  3. Replies: 2
    Last Post: 26th November 2009, 04:45
  4. QButtonGroup buttonClicked signal/slot problem
    By davidthewatson in forum Qt Programming
    Replies: 3
    Last Post: 28th February 2009, 17:24

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.