Results 1 to 7 of 7

Thread: Fetching data from table view

  1. #1
    Join Date
    Jan 2010
    Location
    Ankara, Turkey
    Posts
    7
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60

    Default Fetching data from table view

    I've a QSqlQueryModel as a table view. I want users to delete some of entries by simply selecting them from the table and pressing delete button. I delete the entry by first column's value so I'm successful if user clicks to first column. I can't force user to click first column especially on S60 so I want to get first columns value if user clicks to other columns. How can I do it?

    Ask if you want more information and thanks in advance.
    A fan of Qt since November 2009.

  2. #2
    Join Date
    May 2009
    Location
    USA
    Posts
    300
    Thanks
    82
    Thanked 11 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Fetching data from table view

    1. Set up a menu item or a context menu with signal/slot to deleteEntry()

    Example:
    Qt Code:
    1. connect(ui->actionDelete_Log_Entry, SIGNAL(triggered(bool)),
    2. this, SLOT(deleteEntry()));
    To copy to clipboard, switch view to plain text mode 

    2. All user has to do is click anywhere in a record (any column) then select your new menu item 'Delete'
    or new context menu entry 'Delete'.


    Qt Code:
    1. void MainWindow::deleteEntry() {
    2. QModelIndex index = view->currentIndex();
    3. if (index.isValid()) {
    4. QSqlRecord record = model->record(index.row());
    5. model->removeRow(index.row());
    6. model->submitAll();
    7. }
    8. }
    To copy to clipboard, switch view to plain text mode 

    Works fine for me.

  3. #3
    Join Date
    Jan 2010
    Location
    Ankara, Turkey
    Posts
    7
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60

    Default Re: Fetching data from table view

    Strangely, it returns "QSqlQuery::value: not positioned on a valid record" error when I press to the delete key.

    My delete function:

    Qt Code:
    1. void lessons::deleteLesson(QModelIndex index)
    2. {
    3. QSqlTableModel *model = this->lessonsTable();
    4. model->removeRow(index.row());
    5. model->submitAll();
    6. }
    To copy to clipboard, switch view to plain text mode 

    lessonsTable function:

    Qt Code:
    1. QSqlTableModel* lessons::lessonsTable()
    2. {
    3. model->setTable("lessons");
    4. model->setEditStrategy(QSqlTableModel::OnManualSubmit);
    5. model->select();
    6. model->removeColumn(0); // Don't show the ID
    7. model->setHeaderData(0, Qt::Horizontal, "Name");
    8. model->setHeaderData(1, Qt::Horizontal, "Teacher");
    9. return model;
    10. }
    To copy to clipboard, switch view to plain text mode 

    And where I call the function:

    Qt Code:
    1. void osman::on_pushButton_del_lesson_clicked()
    2. {
    3. lsn.deleteLesson(ui->tableView_lessons->currentIndex());
    4. }
    To copy to clipboard, switch view to plain text mode 
    A fan of Qt since November 2009.

  4. #4
    Join Date
    May 2009
    Location
    USA
    Posts
    300
    Thanks
    82
    Thanked 11 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Fetching data from table view

    Looks like you are creating a NEW model in your delete function, and trying to delete from that one, which would be empty, instead of deleting from the existing model.

  5. #5
    Join Date
    Jan 2010
    Location
    Ankara, Turkey
    Posts
    7
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60

    Default Re: Fetching data from table view

    I've set my existing model's edit strategy to QSqlTableModel::OnFieldChange and when user edits the table directly it returns the same error too. Is it a problem with model or my database connection?
    A fan of Qt since November 2009.

  6. #6
    Join Date
    Oct 2009
    Posts
    151
    Thanks
    6
    Thanked 13 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Fetching data from table view

    How does
    Qt Code:
    1. lsn.deleteLesson(ui->tableView_lessons->currentIndex())
    To copy to clipboard, switch view to plain text mode 
    In your button clicked calling function relate to
    Qt Code:
    1. void lessons::deleteLesson(QModelIndex index)
    2. {
    3. QSqlTableModel *model = this->lessonsTable()
    4. .....
    5. }
    To copy to clipboard, switch view to plain text mode 

    You are passing a tableview index in the calling function to who knows what in the deleteLesson slot.

    Also have you connected the signal and slot properly?

  7. #7
    Join Date
    Jul 2009
    Posts
    139
    Thanks
    13
    Thanked 59 Times in 52 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Fetching data from table view


  8. The following user says thank you to numbat for this useful post:

    utkuaydin (19th January 2010)

Similar Threads

  1. Set height for table view
    By sasi in forum Qt Programming
    Replies: 1
    Last Post: 29th July 2009, 08:07
  2. Table view
    By tulsi in forum Qt Programming
    Replies: 1
    Last Post: 22nd April 2009, 08:07
  3. Table view->model->set data
    By tulsi in forum Qt Programming
    Replies: 3
    Last Post: 21st April 2009, 08:36
  4. Table Widget Vs. Table View
    By winston2020 in forum Qt Programming
    Replies: 2
    Last Post: 19th October 2008, 09:56
  5. Table Model / View Problem -- Data Not Displaying
    By jhendersen in forum Qt Programming
    Replies: 1
    Last Post: 22nd April 2007, 06:45

Tags for this Thread

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.