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 569 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.
  • NevezN Offline
    NevezN Offline
    Nevez
    wrote on last edited by Nevez
    #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?

    JonBJ 1 Reply Last reply
    0
    • NevezN Nevez

      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?

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on 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?
      NevezN 1 Reply Last reply
      0
      • JonBJ JonB

        @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?
        NevezN Offline
        NevezN Offline
        Nevez
        wrote on 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
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on 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
          • NevezN Offline
            NevezN Offline
            Nevez
            wrote on 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();
            
            
            }
            
            JonBJ 1 Reply Last reply
            0
            • NevezN Nevez

              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();
              
              
              }
              
              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by JonB
              #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
              • NevezN Offline
                NevezN Offline
                Nevez
                wrote on 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?

                JonBJ 1 Reply Last reply
                0
                • NevezN Nevez

                  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?

                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on last edited by
                  #8

                  @Nevez

                  QStyledItemDelegate::paint(painter, option, index);
                  
                  1 Reply Last reply
                  0
                  • NevezN Offline
                    NevezN Offline
                    Nevez
                    wrote on 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

                    • Login

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