Results 1 to 2 of 2

Thread: add and delete row dynamically in QTableWidget

  1. #1
    Join Date
    Feb 2011
    Posts
    64
    Thanks
    16
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default add and delete row dynamically in QTableWidget

    I have this piece of code:
    Qt Code:
    1. Store::Store(QWidget *parent) : QMainWindow(parent),
    2. ui(new Ui::Store)
    3. {
    4. // ..
    5. connect(id, SIGNAL(currentIndexChanged(int)), name, SLOT(setCurrentIndex(int)));
    6. connect(name, SIGNAL(currentIndexChanged(int)), id, SLOT(setCurrentIndex(int )));
    7. connect(id, SIGNAL(currentIndexChanged(int)), this, SLOT(changeItem(int )));
    8.  
    9. connect(ui->billTableWidget, SIGNAL(cellChanged(int,int)), this, SLOT(addRow(int, int)));
    10. }
    11.  
    12. void Store::addRow(int row, int col)
    13. {
    14. if (ui->billTableWidget->rowCount() == row + 1)
    15. {
    16. item = ui->billTableWidget->item(row, col);
    17. if ((item->text().length() > 0) || (ui->billTableWidget->currentRow() != 0))
    18. {
    19. QComboBox *name = new QComboBox;
    20. QComboBox *id = new QComboBox;
    21. ui->billTableWidget->insertRow(row + 1);
    22. ui->billTableWidget->setCellWidget(row + 1, 0, id);
    23. ui->billTableWidget->setCellWidget(row + 1, 1, name);
    24.  
    25. connect(id, SIGNAL(currentIndexChanged(int)), name, SLOT(setCurrentIndex(int)));
    26. connect(name, SIGNAL(currentIndexChanged(int)), id, SLOT(setCurrentIndex(int )));
    27. connect(id, SIGNAL(currentIndexChanged(int)), this, SLOT(changeItem(int )));
    28.  
    29. PrintBill bill(ui);
    30. bill.getItems(id, name);
    31. }
    32. else
    33. {
    34. ui->billTableWidget->removeCellWidget(row + 1, 0);
    35. ui->billTableWidget->removeCellWidget(row + 1, 1);
    36. ui->billTableWidget->removeRow(ui->billTableWidget->rowCount());
    37. }
    38. }
    39.  
    40. // msg.exec();
    41.  
    42. sd.setText(QString::number(row) + QString::number(col));
    43. //sd.exec();
    44. }
    To copy to clipboard, switch view to plain text mode 
    Now, if the user select an item from QComboBox widget (id or name), a new row inserted, but if the user select the first item from QComboBox, witch is an empty item, the last row removed. of course this happen only with the row before the last now. Now, I am facing two problems:
    1) I can't delete row.
    2) I can only add one more row, the new row one doesn't respond to signal correctly. When I chose new item in the second row, it interpreted it like I am still in the first row.

  2. #2
    Join Date
    Feb 2011
    Posts
    64
    Thanks
    16
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: add and delete row dynamically in QTableWidget

    Screenshot.jpg
    I add an image to make my problem more clear.

Similar Threads

  1. Replies: 9
    Last Post: 2nd November 2011, 09:12
  2. Replies: 8
    Last Post: 21st March 2010, 11:31
  3. How to delete header on QTableWidget
    By jlbrd in forum Qt Programming
    Replies: 2
    Last Post: 18th July 2006, 21:00
  4. Dynamically sized QTableWidget
    By therealjag in forum Qt Programming
    Replies: 3
    Last Post: 31st March 2006, 21:06
  5. Replies: 2
    Last Post: 22nd February 2006, 14:58

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.