Results 1 to 8 of 8

Thread: How to get all column data on row clicked

  1. #1
    Join Date
    May 2009
    Location
    Gorontalo
    Posts
    200
    Thanks
    20
    Thanked 5 Times in 5 Posts
    Qt products
    Platforms
    Unix/X11 Windows

    Question How to get all column data on row clicked

    Example, I have a QTreeView data like this

    Qt Code:
    1. 1 Deb
    2. 2 Debian Ubuntu Mepis
    3. 3 Rpm
    4. 4 RedHat Fedora Mandriva
    5. 5 Tar.gz
    6. 6 Linux Unix BSD
    To copy to clipboard, switch view to plain text mode 

    I don't know how to get all column data if one row clicked. Sample if i click row number 6. I get result

    Linux
    Unix
    BSD

    Need your help...

  2. #2
    Join Date
    Sep 2009
    Posts
    20
    Thanked 2 Times in 2 Posts

    Default Re: How to get all column data on row clicked

    Hi,

    Try something like this

    Qt Code:
    1. switch(keyEvent->key())
    2. {
    3. case Qt::Key_C:
    4. if(keyEvent->modifiers() & Qt::ControlModifier)
    5. {
    6. QList<QTreeWidgetItem *> selItems = this->selectedItems();
    7. QApplication::setOverrideCursor(Qt::WaitCursor);
    8. QString temp;
    9. qApp->processEvents();
    10. // Add header
    11. for(int i = 0; i < this->headerItem()->columnCount(); i++)
    12. temp += this->headerItem()->data(i, 0).toString() + '\t';
    13. temp.remove(temp.size() - 1, 1); // remove last '\t'
    14. temp += '\n';
    15. for(int i = 0; i < selItems.size(); i++)
    16. {
    17. for(int j = 0; j < selItems.at(0)->columnCount(); j++)
    18. temp += selItems.at(i)->data(j, 0).toString() + '\t';
    19. temp.remove(temp.size() - 1, 1); // remove last '\t'
    20. temp += '\n';
    21. }
    22. temp.remove(temp.size() - 1, 1); // remove last '\n'
    23. QApplication::clipboard()->setText(temp);
    24. QApplication::restoreOverrideCursor();
    25. }
    26. else
    27. QTreeWidget::keyPressEvent(keyEvent);
    28. break;
    To copy to clipboard, switch view to plain text mode 

    It's part of my keyPressEvent function, I use QTreeWidget but for QTreeView will be quite similar.

    Regards

  3. #3
    Join Date
    May 2009
    Location
    Gorontalo
    Posts
    200
    Thanks
    20
    Thanked 5 Times in 5 Posts
    Qt products
    Platforms
    Unix/X11 Windows

    Question Re: How to get all column data on row clicked

    Thank's for your reply.

    But, I confused with your code. Btw, I created QTreeView with designer. I want get data with doubleclicked signal. I see, the parameters just QModelIndex. Can I get data with that ? Or, what signal must I use ?

    So far I just retrieve data when column is clicked. Whereas I want to take data from all columns.

    Qt Code:
    1. void Dialog::on_treeView_doubleClicked(QModelIndex index)
    2. {
    3. qDebug() << "Data : " << index.model()->data(index).toString();
    4. }
    To copy to clipboard, switch view to plain text mode 

    I Hope, you can understand my english

  4. #4
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to get all column data on row clicked

    Well, you could create a model index for each column and pass those to the model to retrieve the data for each column, but a far better way would be to design and code your own model so you have the data stored how you want it, then you don't need the model at all - you can reference your data structure directly using index.row()

  5. #5
    Join Date
    May 2009
    Location
    Gorontalo
    Posts
    200
    Thanks
    20
    Thanked 5 Times in 5 Posts
    Qt products
    Platforms
    Unix/X11 Windows

    Question Re: How to get all column data on row clicked

    Can you give me a sample code ? I tried search use google, but not lucky

  6. #6
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to get all column data on row clicked

    \qt\examples\itemviews\addressbook\

    You'll notice the data is stored in a QList<QPair<QString,QString>>, so the data can be referenced directly without using the data method of the model.

    For example, to receive both columns for any appropriate row you would do the following:
    Qt Code:
    1. QPair<QString, QString> pair = model.listOfPairs.at(index.row());
    To copy to clipboard, switch view to plain text mode 
    Assuming listOfParis was public of course (you would probably prefer to have a method for accessing it that returned a type such as QPair<QString, QString> type)

    And of course, you would have a custom structured data storage object, not QPair, but probably still in a QList<>

  7. #7
    Join Date
    Sep 2009
    Posts
    20
    Thanked 2 Times in 2 Posts

    Default Re: How to get all column data on row clicked

    Hello,

    This should work for single row selection

    Qt Code:
    1. void Dialog::on_treeView_clicked(QModelIndex index)
    2. {
    3. Q_UNUSED(index);
    4. QModelIndexList mySelection = ui->treeView->selectionModel()->selectedIndexes();
    5. QString data;
    6. foreach(QModelIndex selectedIndex, mySelection)
    7. data += selectedIndex.data(0).toString() + '\t';
    8. data.remove(data.size() - 1, 1);
    9. qDebug() << "Data : " << data;
    10. }
    To copy to clipboard, switch view to plain text mode 

  8. #8
    Join Date
    Oct 2009
    Posts
    66
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: How to get all column data on row clicked

    Perhaps this might also work

    [code]
    name =(model->item(temp.row(),column))->text();
    [\code]
    name -> QString
    model -> QStandardItemModel
    temp -> QModelIndex

Similar Threads

  1. Replies: 2
    Last Post: 21st October 2009, 08:13
  2. Replies: 6
    Last Post: 6th August 2009, 17:18
  3. Replies: 1
    Last Post: 23rd November 2008, 14:11
  4. How to find which column clicked from a treeview.
    By mekos in forum Qt Programming
    Replies: 2
    Last Post: 4th August 2008, 16:44
  5. setting data of buffer to QTreeWidget column
    By thomasjoy in forum Qt Programming
    Replies: 1
    Last Post: 15th August 2007, 17:12

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.