Incorrect header strings in Qtreeview



  • Hi All,

    QVariant treeModel::headerData(int section, Qt::Orientation orientation)  {
    
        if (orientation == Qt::Horizontal && (role == Qt::DisplayRole )
        {
              qDebug << "\n I am here " << " section " << section;
              qDebug << "\n val is  " << columnHeaderNames[section]  ;
               return QVariant((columnHeaderNames[section]).c_str());
      
        }
        return QVariant();
    
    }
    

    In this code output is

    section 0 "A"
    section 4 "B"
    "section 2 "C"
    section 3 "D"
    section 1 "E"
    section 5 "F:

    Ideally from the about values the header order in QtreeView should be

    A--- E ---- C ---D -- B- F

    but in my QTreeView I am getting header order as

    A--- B ---- C ---D -- E- F

    Can some one know what I am missing
    the other code is also


  • Lifetime Qt Champion

    Hi,

    What is columnHeaderNames ? How are you building it ?



  • @Qt-Enthusiast said in Incorrect header strings in Qtreeview:

    QVariant treeModel::headerData(int section, Qt::Orientation orientation) {

    if (orientation == Qt::Horizontal && (role == Qt::DisplayRole )
    {
          qDebug << "\n I am here " << " section " << section;
          qDebug << "\n val is  " << columnHeaderNames[section]  ;
           return QVariant((columnHeaderNames[section]).c_str());
    
    }
    return QVariant();
    

    }

    here is more code

    class TreeView: public QTreeView {
        
    }    
    
    class TreeModel:public QAbstractItemModel {
        
    }
    
    TreeView::TreeView() {
        setHeaderProperties(header());  
    }    
    
    void TreeView::setHeaderProperties(QHeaderView* hdr)
    {
    
       connect(hdr, SIGNAL(sectionMoved(int, int, int)), _csvViewerHandle, SLOT(slotSectionMoved(int, int, int)));
        
    }
      
          
    void myClass::slotSectionMoved(int logicalIdx,int oldVisualIndex ,int newVisualIndex) {
            
        TreeModel treeModel;
        qDebug() << "\n  oldVisualIndex " <<  oldVisualIndex;
        qDebug() << "\n  newVisualIndex" <<  newVisualIndex;
        std::vector<string> columnHeaderNames = getGetColumnHeaders();
        
        for(int i =0 ; i < columnHeaderNames.size();i++) {
           qDebug() << "\n c Before olumnHeadr val is for i " << i << QString::fromStdString(columnHeaderNames[i]);
        } 
    
    
        string colName =  columnHeaderNames[oldVisualIndex];
        columnHeaderNames.erase(columnHeaderNames.begin()+oldVisualIndex);
      	columnHeaderNames.insert(columnHeaderNames.begin()+(newVisualIndex), colName);
        treeModel->setColumnHedaers(columnHeaderNames);
        
        for(int i =0 ; i < columnHeaderNames.size();i++) {
           qDebug() << "\n After Order is for i " << i << QString::fromStdString(columnHeaderNames[i]);
           
           p_TreeModel->setHeaderData(i,Qt::Horizontal, QString::fromStdString(columnHeaderNames[i])); 
        } 
    
    }
    
    void myClass::getColumnHedaers(int logicalIdx,int oldVisualIndex ,int newVisualIndex) {
       vector<string> = { A,B,C,D,E,F}
    }
    
    void TreeModel::setColumnHedaers(vector<string> headers ) {
        _columnHeaders = hedaers;
        
    }    
    
    QVariant TreeModel::headerData(int section, Qt::Orientation orientation)  {
    
        if (orientation == Qt::Horizontal && (role == Qt::DisplayRole )
        {
              qDebug << "\n I am here " << " section " << section;
              qDebug << "\n val is  " << columnHeaderNames[section]  ;
               return QVariant((_columnHeaderNames[section]).c_str());
      
        }
        return QVariant();
    
    }
    


  • Doesn't even compile...

    TreeModel treeModel;
    treeModel->setColumnHedaers(columnHeaderNames);
    p_TreeModel->setHeaderData

    What is myClass?

    Why are you trying to update the model column order from slotSectionMoved?



  • @VRonin said in Incorrect header strings in Qtreeview:

    to
    class myclass: public QObject

    I am trying to implement a column drag functionality in which If I drag a column header from oldIndex to newVisIndex I should be able to change the order of the columns



  • @Qt-Enthusiast said in Incorrect header strings in Qtreeview:

    I am trying to implement a column drag functionality in which If I drag a column header from oldIndex to newVisIndex I should be able to change the order of the columns

    QTreeView already does that.

    #include <QApplication>
    #include <QTreeView>
    #include <QStandardItemModel>
    int main(int argc, char **argv) {
        QApplication app(argc, argv);
        QStandardItemModel baseModel;
        baseModel.insertRows(0, 3);
        baseModel.insertColumns(0, 4);
        QTreeView view;
        view.setModel(&baseModel);
        view.show();
        return app.exec();
    }
    


  • @VRonin said in Incorrect header strings in Qtreeview:

    #include <QApplication>
    #include <QTreeView>
    #include <QStandardItemModel>
    int main(int argc, char **argv) {
    QApplication app(argc, argv);
    QStandardItemModel baseModel;
    baseModel.insertRows(0, 3);
    baseModel.insertColumns(0, 4);
    QTreeView view;
    view.setModel(&baseModel);
    view.show();
    return app.exec();
    }

    which part of code impliments column drag and I have QAbsractItem mode not QStandardItemModel



  • @Qt-Enthusiast said in Incorrect header strings in Qtreeview:

    which part of code impliments column drag

    None, that's the point. It comes for free with QTreeView.

    @Qt-Enthusiast said in Incorrect header strings in Qtreeview:

    I have QAbsractItem mode not QStandardItemModel

    The model never knows the column movement happened.



  • Also If I have to change the header list order in some backend API then what is to be done how to do that , I need a slot that tell header col order has changes



  • @Qt-Enthusiast said in Incorrect header strings in Qtreeview:

    Also If I have to change the header list order in some backend API then what is to be done how to do that

    You can use a proxy like KRearrangeColumnsProxyModel



  • is there any way I can stop inbuilt column drag functionality and impliment my own



  • view->headerView()->setSectionsMovable(false);



  • @VRonin said in Incorrect header strings in Qtreeview:

    setSectionsMovable
    I want to reimpliment my header drag can this we done



  • void winBase::slotSectionMoved(int logicalIdx, int oldVisualIndex, int newVisualIndex)
    {
        vector<string> updateCols; 
        for(int i =0 ; i < columnHeaderNames.size();i++) {
           QVariant var =  p_TreeModel->headerData(i,Qt::Horizontal);
           QString colName  = var.toString(); 
           updateCols.push_back(colName.toStdString());
            qDebug() << "\n I am here " << colName << " i =" << i;  
     
         
        } 
        setVisibleCols(updateCols); // this update backend column order which is python dataframes
    
    
    }
    
    void winBase::reorder()
    1884     for(int i = 0; i < _listOfVisibleCols.size();i++)
    1885         getTreeModel()->setHeaderData(i,Qt::Horizontal,QString::fromStdString(_listOfVisibleCols[i]));
    1886 
    1887     slotColumnChange(_listOfVisibleCols);
    }
    
    void WinBase::slotColumnyChange(vector<string>& visibleColList)
     {
         treeModel->showColumns(visibleColList); ----------------updates Python data frame
    }
    


  • What I am doing wrong



  • What's the link between winBase::slotSectionMoved and winBase::reorder()?



  • updated the backend API

    When I move th header , is there any way , I can update order of columns



  • I think I lost you in all the code.

    In theory reimplementing QAbstractItemModel::moveColumns should be what you want (you may need to call it recursively though)



  • @Qt-Enthusiast said in Incorrect header strings in Qtreeview:

    When I drag the columns I want the upddate the column order list



  • Ok I tell my goal
    When I move or drag the column headers, there is a backend python data frame which has column headers , I want to send the updated column header orders to the backend python layer . How do I do it , You can tell the code from scratch


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.