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. TreeView and Signals
Forum Updated to NodeBB v4.3 + New Features

TreeView and Signals

Scheduled Pinned Locked Moved Unsolved General and Desktop
9 Posts 3 Posters 564 Views 1 Watching
  • 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.
  • N Offline
    N Offline
    Nevez
    wrote on 11 Oct 2022, 14:20 last edited by Nevez 10 Nov 2022, 14:22
    #1

    Hello there,
    First question:
    I have a treeview where I export the db table as a model.
    I want to place a button in each cell of this view. How can I do this most conveniently?

    Second question:
    How can I access the text information in that cell when I click on any cell of the view that has a particular tree model?
    Not: I tried:

    connect(view,&QTreeView::clicked,this,this,[=]
                    (QModelIndex mindex)
            {
            
             qDebug()<<treeView->model()->data(mindex,Qt::DisplayRole).toString();
    
    
            });
    

    This code does not give the correct text information of the cell I clicked on.
    because view has a tree structure like that photo.
    19c0ad98-7cac-40f0-bb80-1104645aa753-image.png
    How can I get the correct index?

    J 1 Reply Last reply 11 Oct 2022, 14:51
    0
    • N Nevez
      11 Oct 2022, 14:20

      Hello there,
      First question:
      I have a treeview where I export the db table as a model.
      I want to place a button in each cell of this view. How can I do this most conveniently?

      Second question:
      How can I access the text information in that cell when I click on any cell of the view that has a particular tree model?
      Not: I tried:

      connect(view,&QTreeView::clicked,this,this,[=]
                      (QModelIndex mindex)
              {
              
               qDebug()<<treeView->model()->data(mindex,Qt::DisplayRole).toString();
      
      
              });
      

      This code does not give the correct text information of the cell I clicked on.
      because view has a tree structure like that photo.
      19c0ad98-7cac-40f0-bb80-1104645aa753-image.png
      How can I get the correct index?

      J Offline
      J Offline
      JonB
      wrote on 11 Oct 2022, 14:51 last edited by
      #2

      @Nevez said in TreeView and Signals:

      qDebug()<<treeView->model()->data(mindex,Qt::DisplayRole).toString();

      • You could try the shorter qDebug() << mindex.data(Qt::DisplayRole).toString();, though I think it will give you the same.
      • If mindex is a child it should have a parent set in it --- check that?
      • Are you clicking on the exact cell you want, or only somewhere on the row?
      N 1 Reply Last reply 11 Oct 2022, 14:55
      0
      • J JonB
        11 Oct 2022, 14:51

        @Nevez said in TreeView and Signals:

        qDebug()<<treeView->model()->data(mindex,Qt::DisplayRole).toString();

        • You could try the shorter qDebug() << mindex.data(Qt::DisplayRole).toString();, though I think it will give you the same.
        • If mindex is a child it should have a parent set in it --- check that?
        • Are you clicking on the exact cell you want, or only somewhere on the row?
        N Offline
        N Offline
        Nevez
        wrote on 11 Oct 2022, 14:55 last edited by
        #3

        @JonB said in TreeView and Signals:

        You could try the shorter qDebug() << mindex.data(Qt::DisplayRole).toString();, though I think it will give you the same.

        haha this works very interestingly correctly. Thanks.

        can you look at my second question?

        1 Reply Last reply
        0
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 11 Oct 2022, 18:33 last edited by
          #4

          Hi,

          To customize your cells: QStyledItemDelegate

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • N Offline
            N Offline
            Nevez
            wrote on 12 Oct 2022, 07:10 last edited by
            #5

            I applied a styleditemdelegeta as you said. Buttons were added, but this time the data in the cells were lost. I'm very new to itemdelegate.
            as in the photo;
            aa23887d-ff33-487e-bb1e-8682dbedab12-image.png

            So why ? How can I keep both the button and the data side by side?
            I want it to be like in this photo;
            0e57e774-f019-4a7d-ba11-3f052944556b-image.png

            krnItemDelegate::krnItemDelegate(QObject *parent)
                : QStyledItemDelegate{parent}
            {
            
            }
            
            void krnItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
            {
                    QPushButton button("+");
            
                    button.setGeometry(0,0,20,20);
                    painter->save();
                    painter->translate(option.rect.topLeft());
                    button.render(painter);
                    painter->restore();
            
            
            }
            
            J 1 Reply Last reply 12 Oct 2022, 08:07
            0
            • N Nevez
              12 Oct 2022, 07:10

              I applied a styleditemdelegeta as you said. Buttons were added, but this time the data in the cells were lost. I'm very new to itemdelegate.
              as in the photo;
              aa23887d-ff33-487e-bb1e-8682dbedab12-image.png

              So why ? How can I keep both the button and the data side by side?
              I want it to be like in this photo;
              0e57e774-f019-4a7d-ba11-3f052944556b-image.png

              krnItemDelegate::krnItemDelegate(QObject *parent)
                  : QStyledItemDelegate{parent}
              {
              
              }
              
              void krnItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
              {
                      QPushButton button("+");
              
                      button.setGeometry(0,0,20,20);
                      painter->save();
                      painter->translate(option.rect.topLeft());
                      button.render(painter);
                      painter->restore();
              
              
              }
              
              J Offline
              J Offline
              JonB
              wrote on 12 Oct 2022, 08:07 last edited by JonB 10 Dec 2022, 08:07
              #6

              @Nevez
              Your paint() override paints your QPushButton, but that is all it does. So now you get that but nothing of the original you are wanting to add the button to. You need to call the base implementation either before or after you add your button. Whether that will work or will place one overlapping the other I do not know, but that is where to start from.

              1 Reply Last reply
              0
              • N Offline
                N Offline
                Nevez
                wrote on 12 Oct 2022, 08:26 last edited by
                #7

                I'm not sure I understand what I should do.
                I couldn't find many examples on the net about what I want. Can you share sample code?

                J 1 Reply Last reply 12 Oct 2022, 08:46
                0
                • N Nevez
                  12 Oct 2022, 08:26

                  I'm not sure I understand what I should do.
                  I couldn't find many examples on the net about what I want. Can you share sample code?

                  J Offline
                  J Offline
                  JonB
                  wrote on 12 Oct 2022, 08:46 last edited by
                  #8

                  @Nevez

                  QStyledItemDelegate::paint(painter, option, index);
                  
                  1 Reply Last reply
                  0
                  • N Offline
                    N Offline
                    Nevez
                    wrote on 12 Oct 2022, 13:39 last edited by
                    #9

                    it worked but overlapped each other.
                    I'm thinking of trying a different method (qstyleditemdelegate instead).
                    What are the ways to add a button instead of treeview's openable arrow icons, or to place a (+) icon instead of the arrow icon and assign a signal to the icon?
                    sry for my english

                    1 Reply Last reply
                    0

                    1/9

                    11 Oct 2022, 14:20

                    • Login

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