Results 1 to 3 of 3

Thread: Get item text from model

  1. #1
    Join Date
    May 2013
    Location
    Georgia,Tbilisi
    Posts
    32
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    7

    Question Get item text from model

    I'm writing phone book software(for educational purposes) I have 1 tableView,one QStandardItemModel,4 buttons(add,edit,delete,clear all)
    here is a picture:
    Capture.JPG

    add button works correctly. But Edit button doesn't work correctly,as I want.
    I add one person's number with Add button and then when I want to edit contact,I press edit Button but suddenly it crashes.
    Capture.JPG

    But,when I add new contact directly in tableView without add button, and then press Edit. That works. I can edit contact without any problems.
    Capture.JPG
    So,problem is when new contact is added via Add button. I can't see what is the problem.
    source code of add and edit buttons

    Qt Code:
    1. void MainWindow::on_pushButtonAdd_clicked()
    2. {
    3. ChangeDialog dg;
    4. dg.setWindowTitle(tr("Add new contact"));
    5. if(dg.exec() == QDialog::Accepted){
    6. list << dg.GetList();
    7. for(int i = 0; i < 4;i++){
    8. QStandardItem *item = new QStandardItem(QString("%1").arg(list.at(i)));
    9. item->setEditable(false);
    10. model->setItem(rowCount,i,item);
    11. }
    12. }
    13. rowCount++;
    14. }
    15.  
    16. void MainWindow::on_pushButtonEdit_clicked()
    17. {
    18. ChangeDialog dg;
    19. dg.setWindowTitle(tr("Edit contact"));
    20. QStringList Slist;
    21.  
    22.  
    23. for(int i = 0; i < 4; i++){
    24. Slist.append(model->item(rowCount,i)->text());
    25. //QMessageBox::information(this,"title","asd");
    26. }
    27.  
    28. dg.SetList(Slist);
    29. QStringList list2;
    30. if(dg.exec() == QDialog::Accepted){
    31. list2 << dg.GetList();
    32. for(int i = 0; i < 4;i++){
    33. QStandardItem *item2 = new QStandardItem(QString("%1").arg(list2.at(i)));
    34. item2->setEditable(false);
    35. model->setItem(rowCount,i,item2);
    36. }
    37. }
    38. }
    To copy to clipboard, switch view to plain text mode 

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

    Default Re: Get item text from model

    Where do you get "rowCount" from?

    Maybe it is outside the valid range of 0 <= rowCount < mode->rowCount()

    If it is then model->item() will return 0 and accessing text() on a null pointer will crash.

    Cheers,
    _

  3. #3
    Join Date
    May 2013
    Location
    Georgia,Tbilisi
    Posts
    32
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    7

    Default Re: Get item text from model

    you are right I noticed that now. What a stupid mistake
    rowCount variable comes from here:
    Qt Code:
    1. class MainWindow : public QMainWindow
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. explicit MainWindow(QWidget *parent = 0);
    7. int rowCount;
    8. ~MainWindow();
    To copy to clipboard, switch view to plain text mode 
    when I add new contact, "rowCount" increased by one and then item() returns 0 and can't call text(). So it crashes. I understand.

    ----
    In last one hour I wrote completely new code. And when I saw your reply, I now have two different code.
    Code 2:
    Qt Code:
    1. ChangeDialog dg;
    2. QStringList listOne;
    3. QModelIndexList indexes = ui->tableView->selectionModel()->selection().indexes();
    4. for(int i = 0; i < indexes.count(); ++i){
    5. QModelIndex index = indexes.at(i);
    6. QVariant v = index.data();
    7. listOne.append(v.toString());
    8. }
    9. dg.SetList(listOne);
    10. if(dg.exec() == QDialog::Accepted){
    11. list = dg.GetList();
    12. for(int i = 0; i < 4;i++){
    13. QStandardItem *item = new QStandardItem(QString("%1").arg(list.at(i)));
    14. item->setEditable(false);
    15. model->setItem(ui->tableView->currentIndex().row(),i,item);
    16. }
    17. }
    To copy to clipboard, switch view to plain text mode 

    And old code
    Code 1:
    Qt Code:
    1. ChangeDialog dg;
    2. dg.setWindowTitle(tr("Edit contact"));
    3. QStringList Slist;
    4.  
    5.  
    6. for(int i = 0; i < 4; i++){
    7. Slist.append(model->item(ui->tableView->currentIndex().row(),i)->text());
    8. //QMessageBox::information(this,"title","asd");
    9. }
    10.  
    11. dg.SetList(Slist);
    12. QStringList list2;
    13. if(dg.exec() == QDialog::Accepted){
    14. list2 << dg.GetList();
    15. for(int i = 0; i < 4;i++){
    16. QStandardItem *item2 = new QStandardItem(QString("%1").arg(list2.at(i)));
    17. item2->setEditable(false);
    18. model->setItem(ui->tableView->currentIndex().row(),i,item2);
    19. }
    20. }
    To copy to clipboard, switch view to plain text mode 
    But I changed Code 1's rowCount to this: "ui->tableView->currentIndex().row()"
    ------

    Both code do the same job but both have one problem. When I add new contact and then I edit contact. Edited contact is not fully edited.
    Here is a picture:
    1.JPG

    2.JPG

    3.JPG

    BUT,when I change Code 1's: "ui->tableView->currentIndex().row()" to This: "rowCount",Here is a result
    5.JPG

    And the same happens if I change Code 2's ""ui->tableView->currentIndex().row()" to "rowCount".

    I dont want to add new contact. I want fully edit contact,not just NAME field


    Added after 19 minutes:


    I solved Code 2's problem. I changed this line:

    Qt Code:
    1. model->setItem(indexes.at(i).row(),i,item);
    To copy to clipboard, switch view to plain text mode 
    And this works

    In Code 1. I'm still thinking to find solution
    Last edited by Higgs; 19th February 2014 at 20:51.

Similar Threads

  1. Remove item from QListView's model
    By janck in forum Newbie
    Replies: 4
    Last Post: 19th June 2013, 03:37
  2. Replies: 9
    Last Post: 14th February 2013, 20:39
  3. Replies: 5
    Last Post: 4th July 2012, 10:27
  4. Using model indices in complex model item relationships
    By hackerNovitiate in forum Newbie
    Replies: 0
    Last Post: 29th June 2011, 15:30
  5. Checkable item in tree model
    By zlatko in forum Qt Programming
    Replies: 1
    Last Post: 11th May 2007, 12:35

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.