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
Qt 6.11 is out! See what's new in the release blog

Qtableview verticalheader show icon instead of numbers

Scheduled Pinned Locked Moved General and Desktop
48 Posts 4 Posters 23.5k 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
    #14

    i have checked is the list isempty or no but the same problem

    @if( role == Qt::DecorationRole )
    {
    QModelIndexList IndexsList = m_selectionModel->selection().indexes();
    if(!IndexsList.isEmpty())
    {
    QModelIndex index = m_selectionModel->selection().indexes().at(0);
    if(m_selectionModel->isSelected(index))
    return QPixmap("imgs/square.png");
    }
    }
    }@

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

      this is another code but, unfortunately also not working
      @if( role == Qt::DecorationRole )
      {
      //i have used debugger and, the app crashed when execute the line below
      bool indexListOk = m_selectionModel->selection().indexes().isEmpty() ;
      if(!indexListOk)
      {
      QModelIndex index = m_selectionModel->selection().indexes().at(0);
      if(m_selectionModel->isSelected(index))
      return QPixmap("imgs/square.png");

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

        Did you set m_selectionModel to something valid ?

        If you follow the code from Andre carefully, you'll see that it checks first whether m_selectionModel is not NULL. Which also means that you must set it to NULL in your constructor.

        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
          #17

          Oh! , i forgot to do this , thank's SGaist, andre, i will try to implement this
          :)

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

            Always initializes all your pointers in the constructor either to something valid or NULL like in this case.

            And check for NULL for all pointer variables that are initialized to NULL each time you'll be using these variables

            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
              #19

              Yes, the probem caused by non initialisation of m_selectionModel pointer

              but still i have a small a problem

              when i select a row the icon show up, but when i leave the row (i can't get the icon disappear
              @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);
                      }
              

              @

              1 Reply Last reply
              0
              • JKSHJ Offline
                JKSHJ Offline
                JKSH
                Moderators
                wrote on last edited by
                #20

                @Andre: The headerDataChanged() signal doesn't get emitted with this method, correct? Thus the view won't be notified to update the headers when it's meant to?

                [quote author="advseo32" date="1375459821"]i'm not familiare with debugger[/quote]Take time to learn. It's a very valuable tool for programmers: http://qt-project.org/doc/qtcreator-2.8/creator-debugging.html

                Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

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

                  [quote author="SGaist" date="1375475299"]Always initializes all your pointers in the constructor either to something valid or NULL like in this case.

                  And check for NULL for all pointer variables that are initialized to NULL each time you'll be using these variables[/quote]
                  Ok, i got it now :)

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

                    bq.
                    [quote author="advseo32" date="1375459821"]i'm not familiare with debugger[/quote]Take time to learn. It's a very valuable tool for programmers: http://qt-project.org/doc/qtcreator-2.8/creator-debugging.html[/quote]

                    thanks JKSH, i will learn how i can use debugger

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

                      the icon showed randomly in vertical header

                      her is my code @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() )
                      {
                      for (int i=0;i<this->rowCount();++i)
                      {
                      if(i == index.row())
                      return QPixmap("imgs/arrow.png");
                      else
                      return QSortFilterProxyModel::headerData(section,Qt::Vertical,Qt::DecorationRole);
                      }
                      }
                      }else
                      return QSortFilterProxyModel::headerData(section,Qt::Vertical,Qt::DecorationRole);

                              }else
                                  return QSortFilterProxyModel::headerData(section,Qt::Vertical,Qt::DecorationRole);
                      
                      
                              break;
                          default:
                              QSortFilterProxyModel::headerData(section,orientation,role);
                              break;
                          }@
                      
                      1 Reply Last reply
                      0
                      • A Offline
                        A Offline
                        andre
                        wrote on last edited by
                        #24

                        [quote author="JKSH" date="1375487264"]@Andre: The headerDataChanged() signal doesn't get emitted with this method, correct? Thus the view won't be notified to update the headers when it's meant to?[/quote]

                        The point of my answer was not to provide a complete, ready to use solution, but give a pointer in the right direction. Emitting the changed signal was considered as an excersise for the reader, as was properly initializing the pointer to the selection model. The code I showed was just typed in quickly directly in the forum editor...

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

                          i have emitted a signal to inform changes in vertical header but it won't
                          compile

                          and show me this error @E:\apprendreQt\gestionstock5\mytablemodel.cpp:54: erreur : passing 'const MytableModel' as 'this' argument of 'void QAbstractItemModel::headerDataChanged(Qt::Orientation, int, int)' discards qualifiers [-fpermissive]@

                          @if(m_selectionModel)
                          {

                                      QModelIndexList indexList = m_selectionModel->selection().indexes();
                                      if(!indexList.isEmpty())
                                      {
                                          QModelIndex index =   indexList.at(0);
                                          if(index.isValid() )
                                          {
                                              emit headerDataChanged(Qt::Vertical,0,this->rowCount()-1);
                                              return  QPixmap("imgs/arrow.png");
                          
                                          }
                          
                                      }else
                                          return QSortFilterProxyModel::headerData(section,Qt::Vertical,Qt::DecorationRole);
                                  }else
                                      return QSortFilterProxyModel::headerData(section,Qt::Vertical,Qt::DecorationRole);@
                          
                          1 Reply Last reply
                          0
                          • SGaistS Offline
                            SGaistS Offline
                            SGaist
                            Lifetime Qt Champion
                            wrote on last edited by
                            #26

                            Because you can't emit a signal from a const function

                            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
                              #27

                              [quote author="SGaist" date="1375558375"]Because you can't emit a signal from a const function[/quote]
                              I don't know about this, thank's, but really i haven't an idea on
                              where i will place this emit

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

                                i have tried many ways to infrome view that the data is changed
                                with out any success

                                @void MytableModel::SetSelectionModel(QItemSelectionModel* selection)
                                {
                                m_selectionModel = selection ;
                                emit headerDataChanged(Qt::Vertical,0,this->rowCount()-1);
                                }
                                @

                                i can't emit signal in const functions !! where i can emit that signal ?

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

                                  Well, you want that arrow showed when you select a row, right ? In that case you need to detect that a selection has been made/changed, then tell the world that your header data has changed.

                                  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
                                    #30

                                    [quote author="SGaist" date="1375652135"]Well, you want that arrow showed when you select a row, right ? In that case you need to detect that a selection has been made/changed, then tell the world that your header data has changed.[/quote]

                                    Yes,

                                    i have use this in my main and still not working, i don't know if is the correct place @ connect(listProduitTableView->selectionModel(),
                                    SIGNAL(selectionChanged(QItemSelection,QItemSelection)),listProduitTableView->verticalHeader(),
                                    SLOT(headerDataChanged(Qt::Vertical,int,int)));@

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

                                      You can't connect two signals with different signatures like that. When starting the application you should have seen an error about that on the console.

                                      Please read the Signals and Slots chapter in Qt's documentation.

                                      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
                                        #32

                                        [quote author="SGaist" date="1375653308"]You can't connect two signals with different signatures like that. When starting the application you should have seen an error about that on the console.

                                        Please read the Signals and Slots chapter in Qt's documentation.[/quote]
                                        Yeah! the console show me an error,

                                        i don't know what the signature , it's the parameters ???
                                        if yes , how i can connect Signal selectionChanged() with cheaderDataChanged()

                                        i tried to get this work, but it won't work for me , i miss many many concepts in model/view programming

                                        @ connect(listProduitTableView->selectionModel(),SIGNAL(currentRowChanged(QModelIndex,QModelIndex)),listProduitTableView->verticalHeader(),SLOT(dataChanged(QModelIndex,QModelIndex)));
                                        @

                                        or maybe my implement is wrong, plz check it out
                                        her is the header file of custommodel
                                        @#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) ;
                                        private:
                                        QItemSelectionModel *m_selectionModel;

                                        };@
                                        her is the cpp file of custommodel

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

                                        {
                                        m_selectionModel = 0;

                                        }

                                        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 ;
                                        }
                                        @
                                        @@

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

                                          The signature of a function consists of the return type, the parameter types and constness of the function. You're expected to do your own debugging though.

                                          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