Results 1 to 3 of 3

Thread: Get item text from model

Hybrid View

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

    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
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    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,
    _

Similar Threads

  1. Remove item from QListView's model
    By janck in forum Newbie
    Replies: 4
    Last Post: 19th June 2013, 02:37
  2. Replies: 9
    Last Post: 14th February 2013, 19:39
  3. Replies: 5
    Last Post: 4th July 2012, 09:27
  4. Using model indices in complex model item relationships
    By hackerNovitiate in forum Newbie
    Replies: 0
    Last Post: 29th June 2011, 14:30
  5. Checkable item in tree model
    By zlatko in forum Qt Programming
    Replies: 1
    Last Post: 11th May 2007, 11: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.