PDA

View Full Version : How to get all column data on row clicked



wirasto
7th November 2009, 20:50
Example, I have a QTreeView data like this



1 Deb
2 Debian Ubuntu Mepis
3 Rpm
4 RedHat Fedora Mandriva
5 Tar.gz
6 Linux Unix BSD


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...

lasher
7th November 2009, 21:42
Hi,

Try something like this



switch(keyEvent->key())
{
case Qt::Key_C:
if(keyEvent->modifiers() & Qt::ControlModifier)
{
QList<QTreeWidgetItem *> selItems = this->selectedItems();
QApplication::setOverrideCursor(Qt::WaitCursor);
QString temp;
qApp->processEvents();
// Add header
for(int i = 0; i < this->headerItem()->columnCount(); i++)
temp += this->headerItem()->data(i, 0).toString() + '\t';
temp.remove(temp.size() - 1, 1); // remove last '\t'
temp += '\n';
for(int i = 0; i < selItems.size(); i++)
{
for(int j = 0; j < selItems.at(0)->columnCount(); j++)
temp += selItems.at(i)->data(j, 0).toString() + '\t';
temp.remove(temp.size() - 1, 1); // remove last '\t'
temp += '\n';
}
temp.remove(temp.size() - 1, 1); // remove last '\n'
QApplication::clipboard()->setText(temp);
QApplication::restoreOverrideCursor();
}
else
QTreeWidget::keyPressEvent(keyEvent);
break;


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

Regards

wirasto
7th November 2009, 22:25
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.



void Dialog::on_treeView_doubleClicked(QModelIndex index)
{
qDebug() << "Data : " << index.model()->data(index).toString();
}


I Hope, you can understand my english :)

squidge
7th November 2009, 22:40
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()

wirasto
7th November 2009, 22:57
Can you give me a sample code ? I tried search use google, but not lucky :(

squidge
7th November 2009, 23:42
\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:


QPair<QString, QString> pair = model.listOfPairs.at(index.row());

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

lasher
8th November 2009, 10:54
Hello,

This should work for single row selection



void Dialog::on_treeView_clicked(QModelIndex index)
{
Q_UNUSED(index);
QModelIndexList mySelection = ui->treeView->selectionModel()->selectedIndexes();
QString data;
foreach(QModelIndex selectedIndex, mySelection)
data += selectedIndex.data(0).toString() + '\t';
data.remove(data.size() - 1, 1);
qDebug() << "Data : " << data;
}

kapoorsudhish
9th November 2009, 05:21
Perhaps this might also work

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