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

Qtableview verticalheader show icon instead of numbers

Scheduled Pinned Locked Moved General and Desktop
48 Posts 4 Posters 22.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
    #1

    Hi, every body

    i have Qtableview with Qsqlquerymodel,

    i want to show an icon on each vertical header when the the selection is active in that header

    should i sublcass QHeaderView class and implement neccessary function

    after setting up vertical header

    @setVerticalHeader(QHeaderView * header);@
    

    or i can use another solution without subclassing QheaderView ???

    i try to this way without subclassing

    @ ui->tableView->verticalHeader()->model()->
    setData(ui->tableView->verticalHeader()->model()->index(1,1),"**",Qt::DisplayRole) ;
    @

    but nothing is work correctly

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

      You should subclass either QSqlQueryModel or QIdentityProxyModel. There, you reimplement the headerData function to return the data you want to have appearing in your header. If you use the proxy, you use it between your current QSqlQueryModel and your QTableView, while if you use the QSqlQueryModel subclass you use that subclass instead of your current QSqlQueryModel. The role you are looking for is the DecorationRole.

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

        Yes! andre i know how i can use proxymodel between mymodel and myvie

        thank's for guideing me

        but the only problem now is

        how i can get selection even inside HeaderData() function

        her is my code @QVariant MytableModel::headerData(int section, Qt::Orientation orientation, int role) const
        {
        if( orientation == Qt::Vertical && role == Qt::DecorationRole)
        // how i can know when the user select a row
        // So, i will use condition like so
        if(row.IsSelectedByUser) // this is my main idea
        return QPixmap("imgs/arrow.png");

        return QVariant();
        

        }@

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

          Ah, well, that was something you did not specify in your question.
          The simple answer is: you can not. The selection state is something that is not a property of the model, but of the view in the Qt model/view setup. So, your model knows nothing of the selection state.

          To me, that is a reason to use a proxy model here, and give it a setter for a QItemSelectionModel. Then, set the item selection model from your view on your proxy model, and use the item selection model to find out if the current row is selected or not.

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

            this is waht you mean ??
            for example my proxy model is : mytablemodel
            myview is : tableView;

            i will implement a setter in mytablemodel like so

            @QItemSelectionModel SetSelectionModel(QItemSelectionModel *selection) ;@

            then in main coeur return the selection

            @QItemSelectionModel MytableModel::SetSelectionModel(QItemSelectionModel *selection)
            {
            return selection;
            }
            @

            then get the selectionmdel from the tableView

            @tableView->selectionModel() ;@

            then
            @mytablemdoel->SetSelectionModel(tableView->selectionModel() )@

            plz check it out

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

              No, that's not what I mean. I mean:

              @
              class MyProxyModel: public QIdentityModel
              {
              public:
              void setSelectionModel(QItemSelectionModel* selection)
              {
              m_selectionModel = selection;
              }

              QVariant headerData(int section, Qt::Orientation orientation, int role) const;
              

              private:
              QItemSelectionModel* m_selectionModel;
              };

              QVariant MyProxyModel::headerData(...)
              {
              if (m_selectionModel)
              {
              //use the selection model to figure out if the current row is selected
              // if so, return your arrow icon for the decoration role
              }

              return QIdentityProxyModel::headerData(...);
              

              }

              //where you create the models and view stuff
              MyProxyModel* proxy = new MyProxyModel(myTableView);
              proxy->setSourceModel(mySqlModel);
              myTableView->setModel(proxy);
              proxy->setSelectionModel(myTableView->selectionModel());
              @

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

                Yes, this what i write in code above

                mytablemodel is my proxymodel :)

                i don't write the code, i just want to check if it's the idea that you want

                finally, thank's for quick answers, complet example :)

                regards, abdeu

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

                  i have implemented the functions but the programme crashed

                  her is my header file @#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;

                  };
                  @

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

                  her is my main

                  @
                  // my main model venteModel
                  // my proxy model ProxyProduitModel
                  // my view listProduitTableView

                  ProxyProduitModel->SetSelectionModel(listProduitTableView->selectionModel());
                  venteModel->setQuery("select *from Produit");
                  ProxyProduitModel->setSourceModel(venteModel);
                  listProduitTableView->setModel(ProxyProduitModel);

                  @

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

                    You'll have to do better than "it crashes". Fire up your debugger when that happens, and figure out what is going wrong from that.

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

                      i'm not familiare with debugger but, i have try to figure out ,what's the problem, and i get the line where the problem is happned

                      this @
                      // Her i get the index(0,0) from proxymodel
                      QModelIndex index = this->index(0,0) ;
                      // then i have maked the test

                      if( orientation == Qt::Vertical)
                           if( orientation == Qt::Vertical && Qt::DecorationRole )
                            // her is the line , the programme crashes her 
                      

                      if(m_selectionModel->isRowSelected(index.row(),index)) ;
                      return QPixmap("imgs/arrow.png");@

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

                        Now, the problem is in the index of selected row

                        i try to get then selected index in view like so, but isn't works
                        @proxyModel->sourceModel()->index(0,0) ;@
                        or @proxyModel->index(0,0) ;@

                        i want to know how i get the selected row index, to be able to use it in

                        @isRowSelected(int row, const QModelIndex & parent)
                        and
                        isSelected(const QModelIndex & index)
                        @

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

                          Now, it's clear that some thing is wrong with index, but i can't figured out
                          where is the problem

                          her is my code @
                          if( orientation == Qt::Vertical)
                          {
                          if( role == Qt::DecorationRole )
                          {
                          QModelIndexList indexes = m_selectionModel->selection().indexes();
                          QModelIndex index = m_selectionModel->selection().indexes().at(0);
                          if(m_selectionModel->isSelected(index))
                          return QPixmap("imgs/arrow.png");
                          }
                          }@

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

                            Hi,

                            indexes might be empty, you don't check that case

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

                                          • Login

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