Results 1 to 3 of 3

Thread: TableView store contents of particular cell in variable

  1. #1
    Join Date
    Oct 2010
    Posts
    54
    Thanks
    8
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Question TableView store contents of particular cell in variable

    Ok I have a table populated with data from my database.

    What i want is when a user selects a cell and a button is pressed, the data within the cell they selected is used in a function as a variable. I have no idea how to achieve this. Could someone please point me in the right direction.

    Thanks for your time and trouble

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: TableView store contents of particular cell in variable

    In the slot reacting to the button, use the view's QItemSelectionModel to determine the current cell's QModelIndex. Then retrieve the value of the Qt::DisplayRole (or whatever) for that index from the model (not the view).

  3. #3
    Join Date
    Oct 2010
    Posts
    54
    Thanks
    8
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Question Re: TableView store contents of particular cell in variable

    Could you perhaps give me some example code? I'm new at this and really confused.





    Qt Code:
    1. UserWindow::UserWindow(QWidget *parent) :
    2. QMainWindow(parent),
    3. ui(new Ui::UserWindow)
    4. {
    5. //Other code here
    6.  
    7. query.exec(querystring);
    8.  
    9. model->setQuery(query);
    10. model->removeColumn(0);
    11. model->removeColumn(1);
    12. model->setHeaderData(0, Qt::Horizontal, "Name");
    13. model->setHeaderData(1, Qt::Horizontal, "Date");
    14. model->setHeaderData(2, Qt::Horizontal, "Completed");
    15.  
    16. // Populate the table with the relevant information from the database
    17.  
    18. ui->tableView->setModel(model);
    19. ui->tableView->show();
    20. }
    21.  
    22.  
    23.  
    24. void UserWindow::on_NextButton_clicked()
    25. {
    26.  
    27.  
    28. QItemSelectionModel *selectionModel = ui->tableView->selectionModel();
    29. // What do I do here?
    30. // I want the value within the currently selected cell to be used here.
    31. // Could someone give me some example code to demonstrate how to do this.
    32.  
    33. }
    To copy to clipboard, switch view to plain text mode 


    Thanks

Similar Threads

  1. QTableView and changing how a cell displays its contents
    By BitRogue in forum Qt Programming
    Replies: 2
    Last Post: 9th October 2009, 11:18
  2. How to store the lineedit contents in memory
    By grsandeep85 in forum Qt Programming
    Replies: 4
    Last Post: 3rd August 2009, 14:16
  3. Clear contents of a QTableView cell before repainting
    By aspidites in forum Qt Programming
    Replies: 2
    Last Post: 21st April 2009, 17:07
  4. Best way to store and retrieve contents of QTableWidget?
    By codeslicer in forum Qt Programming
    Replies: 3
    Last Post: 6th April 2008, 10:39
  5. Copying contents of QTableView cell to clipboard
    By Conel in forum Qt Programming
    Replies: 2
    Last Post: 18th April 2006, 16:50

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.