how to display data in interactive list menu
-
wrote on 10 Jan 2020, 06:09 last edited by ahsan737 1 Oct 2020, 06:14
Greetings,
I am building an android application, I need to read from *.csv file and display data in list menu in a particular pattern (sample picture is attached), and if the user clicks an item from that list then I want to switch to a new page and display relevant information in further detail. Please guide.- currently I am using QTreeView approach to display data but I don't know how to detect the particular item is clicked and show information relevant to that item on the new page. (sample code is given below)
//--------------------Tree View Model-------------------------- QStandardItemModel *model =new QStandardItemModel (this); //capture the root item QStandardItem *rootItem=model->invisibleRootItem(); //-------------------------------------------------- readData(); //read data from the data.csv file for (int i = 0; i < Title_line.size(); ++i) { //define Items QStandardItem *TitleItem=new QStandardItem (Title_line.at(i)); TitleItem->setIcon(QIcon(":/img/img/saved_data.jpg.png")); QFont font = TitleItem->font(); font.setPointSize(16); font.setBold(true); TitleItem->setFont(font); QStandardItem *SecondLineItem=new QStandardItem (second_line.at(i)); QStandardItem *ThirdLineItem=new QStandardItem (third_line.at(i)+"\n"); font.setPointSize(14); font.setBold(false); SecondLineItem->setFont(font); ThirdLineItem->setFont(font); //defining tree structure rootItem->appendRow(TitleItem); TitleItem->appendRow(SecondLineItem); TitleItem->appendRow(ThirdLineItem); } ui->treeView->setModel(model ); ui->treeView->expandAll();
-
Greetings,
I am building an android application, I need to read from *.csv file and display data in list menu in a particular pattern (sample picture is attached), and if the user clicks an item from that list then I want to switch to a new page and display relevant information in further detail. Please guide.- currently I am using QTreeView approach to display data but I don't know how to detect the particular item is clicked and show information relevant to that item on the new page. (sample code is given below)
//--------------------Tree View Model-------------------------- QStandardItemModel *model =new QStandardItemModel (this); //capture the root item QStandardItem *rootItem=model->invisibleRootItem(); //-------------------------------------------------- readData(); //read data from the data.csv file for (int i = 0; i < Title_line.size(); ++i) { //define Items QStandardItem *TitleItem=new QStandardItem (Title_line.at(i)); TitleItem->setIcon(QIcon(":/img/img/saved_data.jpg.png")); QFont font = TitleItem->font(); font.setPointSize(16); font.setBold(true); TitleItem->setFont(font); QStandardItem *SecondLineItem=new QStandardItem (second_line.at(i)); QStandardItem *ThirdLineItem=new QStandardItem (third_line.at(i)+"\n"); font.setPointSize(14); font.setBold(false); SecondLineItem->setFont(font); ThirdLineItem->setFont(font); //defining tree structure rootItem->appendRow(TitleItem); TitleItem->appendRow(SecondLineItem); TitleItem->appendRow(ThirdLineItem); } ui->treeView->setModel(model ); ui->treeView->expandAll();
hi @ahsan737
QTreeView inherits QAbstractItemView, therefore all these signals are also available
https://doc.qt.io/qt-5/qabstractitemview.html#signals
You'll be interested in the
void QAbstractItemView::clicked(const QModelIndex &index)
signal. connect to that, you get a QModelIndex that you can use to identify what was clicked :) -
hi @ahsan737
QTreeView inherits QAbstractItemView, therefore all these signals are also available
https://doc.qt.io/qt-5/qabstractitemview.html#signals
You'll be interested in the
void QAbstractItemView::clicked(const QModelIndex &index)
signal. connect to that, you get a QModelIndex that you can use to identify what was clicked :)wrote on 10 Jan 2020, 06:30 last edited by ahsan737 1 Oct 2020, 06:36@J-Hilk thank you, I will look into it. I've got another question, will every root item get a unique index the way I am adding the items to the view? (please check the attached code in the description).
I've tried to compare indexes after clicking at different items but it says that indexes are the same. (test code is given below)QModelIndex index1; QModelIndex index2; refresh++; ui->label->setText(QString::number(refresh)); if (refresh==1) { index1=rootItem->index(); } else if (refresh==2) { index2=rootItem->index(); } else if (refresh==3) { if (index1==index2) { ui->label->setText("Same"); refresh=0; } else { ui->label->setText("Not Same"); refresh=0; } }
*** I've also tried using currentIndex but result is the same.
-
@J-Hilk thank you, I will look into it. I've got another question, will every root item get a unique index the way I am adding the items to the view? (please check the attached code in the description).
I've tried to compare indexes after clicking at different items but it says that indexes are the same. (test code is given below)QModelIndex index1; QModelIndex index2; refresh++; ui->label->setText(QString::number(refresh)); if (refresh==1) { index1=rootItem->index(); } else if (refresh==2) { index2=rootItem->index(); } else if (refresh==3) { if (index1==index2) { ui->label->setText("Same"); refresh=0; } else { ui->label->setText("Not Same"); refresh=0; } }
*** I've also tried using currentIndex but result is the same.
@ahsan737 that's not really enough information to go an,
but for example,
adding this to the constructor of mainwindow
connect(view, &QAbstractItemView::clicked, this, [=](const QModelIndex index)->void{qDebug() << "clicked on"<< index.row();});
from this Qt example:
https://doc.qt.io/qt-5/qtwidgets-itemviews-editabletreemodel-example.htmlworks like a charm
-
@ahsan737 that's not really enough information to go an,
but for example,
adding this to the constructor of mainwindow
connect(view, &QAbstractItemView::clicked, this, [=](const QModelIndex index)->void{qDebug() << "clicked on"<< index.row();});
from this Qt example:
https://doc.qt.io/qt-5/qtwidgets-itemviews-editabletreemodel-example.htmlworks like a charm
wrote on 11 Jan 2020, 02:14 last edited by ahsan737 1 Nov 2020, 03:03@J-Hilk Thanks a lot, the hint
index.row();
has worked for me, and now I can display the relevant data on the second screen. Since I am reading data from android storage and I've noticed thatQPixmap
don't work to display image using a label, but fortunatelyui->label->setText("<img src=/storage/emulated/0/Plots/xyz.jpg align=middle width=600 height=600>");
works.
1/5