Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. how to display data in interactive list menu

how to display data in interactive list menu

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 2 Posters 503 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    ahsan737
    wrote on 10 Jan 2020, 06:09 last edited by ahsan737 1 Oct 2020, 06:14
    #1

    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();
    
    

    sample_1.png

    J 1 Reply Last reply 10 Jan 2020, 06:22
    0
    • A ahsan737
      10 Jan 2020, 06:09

      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();
      
      

      sample_1.png

      J Online
      J Online
      J.Hilk
      Moderators
      wrote on 10 Jan 2020, 06:22 last edited by
      #2

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


      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


      Q: What's that?
      A: It's blue light.
      Q: What does it do?
      A: It turns blue.

      A 1 Reply Last reply 10 Jan 2020, 06:30
      1
      • J J.Hilk
        10 Jan 2020, 06:22

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

        A Offline
        A Offline
        ahsan737
        wrote on 10 Jan 2020, 06:30 last edited by ahsan737 1 Oct 2020, 06:36
        #3

        @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 1 Reply Last reply 10 Jan 2020, 07:45
        0
        • A ahsan737
          10 Jan 2020, 06:30

          @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 Online
          J Online
          J.Hilk
          Moderators
          wrote on 10 Jan 2020, 07:45 last edited by
          #4

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

          works like a charm


          Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


          Q: What's that?
          A: It's blue light.
          Q: What does it do?
          A: It turns blue.

          A 1 Reply Last reply 11 Jan 2020, 02:14
          1
          • J J.Hilk
            10 Jan 2020, 07:45

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

            works like a charm

            A Offline
            A Offline
            ahsan737
            wrote on 11 Jan 2020, 02:14 last edited by ahsan737 1 Nov 2020, 03:03
            #5

            @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 that QPixmap don't work to display image using a label, but fortunately ui->label->setText("<img src=/storage/emulated/0/Plots/xyz.jpg align=middle width=600 height=600>"); works.

            1 Reply Last reply
            0

            1/5

            10 Jan 2020, 06:09

            • Login

            • Login or register to search.
            1 out of 5
            • First post
              1/5
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved