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. Qtableview verticalheader show icon instead of numbers

Qtableview verticalheader show icon instead of numbers

Scheduled Pinned Locked Moved General and Desktop
48 Posts 4 Posters 23.2k 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.
  • A Offline
    A Offline
    advseo32
    wrote on last edited by
    #35

    i tried many solutions, i'm completly lost

    i have created a slots in my proxyModel
    @void emitHeaderChangedData(QModelIndex &current,QModelIndex &prev) ;@

    her code is
    @void MytableModel::emitHeaderChangedData(QModelIndex &current, QModelIndex &prev)
    {
    int currentRow = current.row();
    int previousRow = prev.row() ;
    emit headerDataChanged(Qt::Vertical,currentRow,previousRow);
    }

    @

    and i connect the signal currentRowChanged to that slots in my constructor , but it's still not working

    @MytableModel::MytableModel(QObject *parent) : QSortFilterProxyModel(parent)

    {
    m_selectionModel = 0;
    connect(m_selectionModel,SIGNAL(currentRowChanged(QModelIndex,QModelIndex)),
    this,SLOT(emitHeaderChangedData(QModelIndex&,QModelIndex&)));

    }

    @

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #36

      Just a quick question, why didn't you follow Andre's suggestion and use a QIdentityProxyModel ?

      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
      • A Offline
        A Offline
        advseo32
        wrote on last edited by
        #37

        I'm using Andre's method her is the complete code

        header file

        i create a function to emit headerDatachanged but not works,

        @void MytableModel::emitHeaderChangedData(QModelIndex &current, QModelIndex &prev)@

        and i try to use connect in my constrector, also not working

        i don't know exactly what's the best way to emit that signal

        @#ifndef MYTABLEMODEL_H
        #define MYTABLEMODEL_H

        #include <QSortFilterProxyModel>
        #include <QItemSelectionModel>
        class MytableModel : public QSortFilterProxyModel
        {
        public:
        explicit MytableModel(QObject parent = 0);
        QVariant headerData(int section, Qt::Orientation orientation, int role) const;
        QVariant data(const QModelIndex &proxyIndex, int role) const;
        void SetSelectionModel(QItemSelectionModel
        selection) ;
        public slots:
        void emitHeaderChangedData(QModelIndex &current,QModelIndex &prev) ;
        private:
        QItemSelectionModel *m_selectionModel;

        };

        #endif // MYTABLEMODEL_H
        @

        her is mytablemodel.cpp

        @#include "mytablemodel.h"
        #include <QPixmap>
        MytableModel::MytableModel(QObject *parent) : QSortFilterProxyModel(parent)

        {
        m_selectionModel = 0;
        connect(m_selectionModel,SIGNAL(currentRowChanged(QModelIndex,QModelIndex)),m_selectionModel,SLOT(emitHeaderChangedData(QModelIndex&,QModelIndex&)));

        }

        QVariant MytableModel::headerData(int section, Qt::Orientation orientation, int role) const
        {
        switch (orientation) {
        case Qt::Horizontal :
        switch (role) {
        case Qt::DisplayRole :
        switch (section) {
        case 0: return "Id";break;
        case 1: return "Reference";break;
        case 2: return "Designation";break;
        case 3: return "localisation";break;
        case 4: return "Famille";break;
        case 5: return "Qte min";break;
        case 6: return "Qte max";break;
        case 7: return "Qte disponible";break;
        case 8: return "Unité";break;
        case 9: return "Prix achat";break;
        case 10: return "Prix vente";break;
        default:
        return QString("Column %1").arg(section + 1);
        break;
        }
        break;
        default:
        QSortFilterProxyModel::headerData(section,orientation,role);
        break;
        }
        break;
        case Qt::Vertical :
        switch (role) {
        case Qt::DecorationRole :
        if(m_selectionModel)
        {
        QModelIndexList indexList = m_selectionModel->selection().indexes();
        if(!indexList.isEmpty())
        {
        QModelIndex index = indexList.at(0);
        if(index.isValid() )
        {
        return QPixmap("imgs/arrow.png");

                        }
        
                    }else
                        return QSortFilterProxyModel::headerData(section,Qt::Vertical,Qt::DecorationRole);
                }else
                    return QSortFilterProxyModel::headerData(section,Qt::Vertical,Qt::DecorationRole);
        
                break;
            default:
                return QSortFilterProxyModel::headerData(section,orientation,role);
                break;
            }
            break;
        default:
            return QSortFilterProxyModel::headerData(section,orientation,role);
            break;
        }
        
        
        
        return QVariant();
        

        }

        QVariant MytableModel::data(const QModelIndex &proxyIndex, int role) const
        {
        if(role == Qt::TextAlignmentRole)
        return Qt::AlignCenter;
        else if (role == Qt::DisplayRole)
        return QSortFilterProxyModel::data(proxyIndex , role);

        return QVariant();
        

        }

        void MytableModel::SetSelectionModel(QItemSelectionModel* selection)
        {
        m_selectionModel = selection ;
        }

        void MytableModel::emitHeaderChangedData(QModelIndex &current, QModelIndex &prev)
        {
        int currentRow = current.row();
        int previousRow = prev.row() ;
        emit headerDataChanged(Qt::Vertical,currentRow,previousRow);
        }

        @

        1 Reply Last reply
        0
        • A Offline
          A Offline
          andre
          wrote on last edited by
          #38

          In your constructor, you first set m_selectionModel to 0, meaning that it does not refer to a valid object. And on the next line, you try to connect to it anyway? You should make the connection from your SetSelectionModel method where you actually have the valid object, at line 92 of your cpp.

          Note that because you are not changing the stucture or sort order of your model, it is more efficient to use QIdentityProxyModel than QSortFilterProxyModel as your base class.

          1 Reply Last reply
          0
          • A Offline
            A Offline
            advseo32
            wrote on last edited by
            #39

            unfortunately, i can't get this work

            i don't know where i'm wrong @void MytableModel::SetSelectionModel(QItemSelectionModel *selection)
            {
            m_selectionModel = selection ;
            QObject::connect(m_selectionModel,SIGNAL(currentRowChanged(QModelIndex,QModelIndex)),
            this,SLOT(emitHeaderDataChanged(QModelIndex&,QModelIndex&))) ;
            }

            void MytableModel::emitHeaderDataChanged(QModelIndex &current, QModelIndex &prev)
            {
            int currentRow = current.row();
            int previousRow = prev.row() ;
            emit headerDataChanged(Qt::Vertical,currentRow,previousRow);

            }
            @

            1 Reply Last reply
            0
            • A Offline
              A Offline
              andre
              wrote on last edited by
              #40
              • Why do you use QObject:: before your connect?

              • What does this call return?

              • Do you see any application output for this connect statement?

              • Where do you call SetSelectionModel? And how?

              • What exactly does not work?

              • Does emitHeaderDataChanged get called or not?

              1 Reply Last reply
              0
              • A Offline
                A Offline
                advseo32
                wrote on last edited by
                #41
                Why do you use QObject:: before your connect?
                

                i forgot that the macro Q_OBJECT is included

                What does this call return?
                

                what function you mean, connect ??

                Do you see any application output for this connect statement?
                

                no

                Where do you call SetSelectionModel?

                i  call it in other file , where is use my view( to get selection model)
                

                And how?

                with @myMytableModel->SetSelectionModel(myView->selectionModel());@

                What exactly does not work?
                
                when the icon ( arrow.png) showed in vertical and the selection changed  the icon stay in vertical header and never disappear
                

                witch mean, all rows have an icon "arrow.png" in their left side ( vertical header) in stead of only the selected one !!

                Does emitHeaderDataChanged get called or not?
                

                i place break point in it but it's not called

                1 Reply Last reply
                0
                • A Offline
                  A Offline
                  andre
                  wrote on last edited by
                  #42

                  I see something weird in your headerData implementation:

                  What you do there, is that you only check if there are selected indices. You are not checking if any of the selected indices is on the row that the current header data is requested for. So indeed: as long as you have a selected item, it should make all your vertical headers render the arrow.

                  I do not get why you don't get the signal. Did you try with the selectionChanged signal from QItemSelectionModel?

                  1 Reply Last reply
                  0
                  • A Offline
                    A Offline
                    advseo32
                    wrote on last edited by
                    #43

                    i have modified the code , but i get an error

                    @Démarrage de E:\apprendreQt\build-gestionstock5-Desktop_Qt_5_1_0_MinGW_32bit-Debug\debug\gestionstock5.exe...ASSERT failure in QList<T>::at: "index out of range", file C:\Qt\Qt5.1.0\5.1.0\mingw48_32\include/QtCore/qlist.h, line 452
                    Invalid parameter passed to C runtime function.
                    Invalid parameter passed to C runtime function.

                    @
                    i have checked with debugger to get the source of the error , it crashed
                    when he execute this line

                    @QModelIndex index = indexList.at(0);@ ( line 9 )

                    her is headerData for vertical header @case Qt::Vertical :
                    switch (role) {
                    case Qt::DecorationRole :
                    if(m_selectionModel)
                    {
                    QModelIndexList indexList = m_selectionModel->selection().indexes();
                    if(!indexList.isEmpty())
                    {
                    QModelIndex index = indexList.at(0);
                    if(index.isValid() )
                    {

                                        if(index.row() == section)
                                            return  QPixmap("imgs/arrow.png");
                                        else
                                            return "**";
                    
                                    }
                    
                                }else
                                    return QIdentityProxyModel::headerData(section,Qt::Vertical,Qt::DecorationRole);
                            }else
                                return QIdentityProxyModel::headerData(section,Qt::Vertical,Qt::DecorationRole);
                    
                            break;
                        default:
                            return QIdentityProxyModel::headerData(section,orientation,role);
                            break;
                        }
                        break;@
                    

                    i have changed my signal to selectionChanged()
                    @void MytableModel::SetSelectionModel(QItemSelectionModel *selection)
                    {
                    m_selectionModel = selection ;
                    connect(m_selectionModel,SIGNAL(selectionChanged(QItemSelection,QItemSelection)),this,SLOT(emitHeaderDataChanged(QItemSelection,QItemSelection))) ;
                    }
                    @

                    and her is my slot ( the program enter into slot )

                    @void MytableModel::emitHeaderDataChanged(QItemSelection selected, QItemSelection deselected)
                    {
                    int selectedRow = selected.indexes().at(0).row() ;
                    int deselectedRow = deselected.indexes().at(0).isValid() ? deselected.indexes().at(0).row() : 0 ;

                    emit headerDataChanged(Qt::Vertical,selectedRow,deselectedRow);
                    

                    }
                    @

                    1 Reply Last reply
                    0
                    • SGaistS Offline
                      SGaistS Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on last edited by
                      #44

                      your indexList is empty and you are trying to access an element

                      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
                      • A Offline
                        A Offline
                        andre
                        wrote on last edited by
                        #45

                        [quote author="SGaist" date="1375786256"]your indexList is empty and you are trying to access an element[/quote]

                        No, as there is a check for that just two lines above...

                        1 Reply Last reply
                        0
                        • A Offline
                          A Offline
                          advseo32
                          wrote on last edited by
                          #46

                          [quote author="SGaist" date="1375786256"]your indexList is empty and you are trying to access an element[/quote]
                          i think there is a problem with connect methode

                          @void MytableModel::SetSelectionModel(QItemSelectionModel *selection)
                          {
                          m_selectionModel = selection ;
                          connect(m_selectionModel,SIGNAL(selectionChanged(QItemSelection,QItemSelection)),this,SLOT(emitHeaderDataChanged(QItemSelection,QItemSelection))) ;
                          }@

                          and SLOT

                          @void MytableModel::emitHeaderDataChanged(QItemSelection selected, QItemSelection deselected)
                          {
                          int selectedRow = selected.indexes().at(0).row() ;
                          int deselectedRow = deselected.indexes().at(0).isValid() ? deselected.indexes().at(0).row() : 0 ;

                          emit headerDataChanged(Qt::Vertical,selectedRow,deselectedRow);
                          

                          }
                          @

                          when i delete this line @connect(m_selectionModel,SIGNAL(selectionChanged(QItemSelection,QItemSelection)),this,SLOT(emitHeaderDataChanged(QItemSelection,QItemSelection))) ;@
                          from setSelectionModel(), it works perfectly :), without emitting headerDataChanged() signal

                          i wish to figure out what happened

                          1 Reply Last reply
                          0
                          • SGaistS Offline
                            SGaistS Offline
                            SGaist
                            Lifetime Qt Champion
                            wrote on last edited by
                            #47

                            [quote author="Andre" date="1375786790"]
                            [quote author="SGaist" date="1375786256"]your indexList is empty and you are trying to access an element[/quote]

                            No, as there is a check for that just two lines above...[/quote]

                            Indeed, keyboard/brain/finger bug, I was thinking about selected/deselected.indexes() which may be empty

                            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
                            • A Offline
                              A Offline
                              advseo32
                              wrote on last edited by
                              #48

                              [quote author="SGaist" date="1375787169"]
                              [quote author="Andre" date="1375786790"]
                              [quote author="SGaist" date="1375786256"]your indexList is empty and you are trying to access an element[/quote]

                              No, as there is a check for that just two lines above...[/quote]

                              Indeed, keyboard/brain/finger bug, I was thinking about selected/deselected.indexes() which may be empty[/quote]

                              Probably, because there is no selected row in my view at intial, so deseleted.indexs() is empty !

                              sorry, if you get a difficulty to understand me ( English is not my native language)

                              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