QTreeView冻结列(QTreeView第一列不随滑块移动)

 
  
#ifndef FREEZETABLEVIEW_H
#define FREEZETABLEVIEW_H

#include 
class QStandardItemModel;
//! [Widget definition]
class FreezeTreeView : public QTreeView 
{
     Q_OBJECT

public:
      FreezeTreeView(QWidget * parent);
      ~FreezeTreeView();
      virtual void setModel(QStandardItemModel * model);
      void init();
protected:
      void resizeEvent(QResizeEvent *event) override;
      QModelIndex moveCursor(CursorAction cursorAction, Qt::KeyboardModifiers modifiers) override;
      //void scrollTo (const QModelIndex & index, ScrollHint hint = EnsureVisible) override;

private slots:
      void updateSectionWidth(int logicalIndex, int oldSize, int newSize);
      //void updateSectionHeight(int logicalIndex, int oldSize, int newSize);
      void slotExpanded(const QModelIndex&index);
      void slotcollapsed(const QModelIndex &index);
      void updateselfGeometry(int logicalIndex, int oldSize, int newSize);

private:
    void updateFrozenTableGeometry();
private:
    QTreeView *frozenTreeView;
    bool m_mark;
};

#endif // FREEZETABLEWIDGET_H


#include "freezetreeview.h"
#include 
#include 
#include 
#include 
#include 

void FreezeTreeView::setModel(QStandardItemModel * model)
{
    QTreeView::setModel(model);
    frozenTreeView->setModel(model);
}

FreezeTreeView::FreezeTreeView(QWidget * parent)
    : QTreeView(parent)
{
    m_mark = false;
    frozenTreeView = new QTreeView(this);
    viewport()->stackUnder(frozenTreeView);
    frozenTreeView->setFocusPolicy(Qt::NoFocus);
    frozenTreeView->setStyleSheet("QTreeView { border: none;}");
    frozenTreeView->setSelectionMode(QAbstractItemView::ExtendedSelection);
    connect(header(), &QHeaderView::sectionResized, this, &FreezeTreeView::updateSectionWidth);
    connect(frozenTreeView->header(), &QHeaderView::sectionResized, this, &FreezeTreeView::updateselfGeometry);
    connect(verticalScrollBar(), &QAbstractSlider::valueChanged, frozenTreeView->verticalScrollBar(), &QAbstractSlider::setValue);
    connect(frozenTreeView, SIGNAL(expanded(const QModelIndex&)), this, SLOT(slotExpanded(const QModelIndex &)));
    connect(frozenTreeView, SIGNAL(collapsed(const QModelIndex &)), this, SLOT(slotcollapsed(const QModelIndex &)));
    connect(this, SIGNAL(collapsed(const QModelIndex &)), this, SLOT(slotcollapsed(const QModelIndex &)));
}

void FreezeTreeView::slotExpanded(const QModelIndex&index)
{
    this->expand(index);
}

void FreezeTreeView::slotcollapsed(const QModelIndex &index)
{
    this->collapse(index);
}


FreezeTreeView::~FreezeTreeView()
{
      delete frozenTreeView;
}

void FreezeTreeView::init()
{
      frozenTreeView->setSelectionModel(selectionModel());
      for (int col = 1; col < this->model()->columnCount(); ++col)
            frozenTreeView->setColumnHidden(col, true);

      frozenTreeView->setColumnWidth(0, columnWidth(0) );
      frozenTreeView->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
      frozenTreeView->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
      frozenTreeView->show();
      updateFrozenTableGeometry();
      setHorizontalScrollMode(ScrollPerPixel);
      setVerticalScrollMode(ScrollPerPixel);
      frozenTreeView->setVerticalScrollMode(ScrollPerPixel);
}


void FreezeTreeView::updateSectionWidth(int logicalIndex, int /* oldSize */, int newSize)
{
      if (logicalIndex == 0){
            frozenTreeView->setColumnWidth(0, newSize);
            updateFrozenTableGeometry();
      }
}


void FreezeTreeView::updateselfGeometry(int logicalIndex, int oldSize, int newSize)
{
    frozenTreeView->setGeometry(1, 1, newSize,
        viewport()->height() + header()->height());
    this->setColumnWidth(0, newSize);
}


void FreezeTreeView::resizeEvent(QResizeEvent * event)
{
      QTreeView::resizeEvent(event);
      updateFrozenTableGeometry();
 }

void FreezeTreeView::updateFrozenTableGeometry()
{
    frozenTreeView->setGeometry(1, 1, columnWidth(0),
        viewport()->height() + header()->height());
}

QModelIndex FreezeTreeView::moveCursor(CursorAction cursorAction,
                                          Qt::KeyboardModifiers modifiers)
{ 
    QModelIndex current = QTreeView::moveCursor(cursorAction, modifiers);
   int x = visualRect(current).topLeft().x();
   int column = current.column();
  
    if (this->isExpanded(current)) {
        frozenTreeView->expand(current);
    }
    else {
        frozenTreeView->collapse(current);
    }
    return current;
}
//
//
//void FreezeTreeView::scrollTo (const QModelIndex & index, ScrollHint hint){
//    if (index.column() > 0)
//        QTreeView::scrollTo(index, hint);
//}
//


//void FreezeTreeView::slotPushButton()
//{
//    QItemSelectionModel * model = this->selectionModel();
//    QModelIndexList list = model->selectedIndexes();
//    qDebug() << list.size();
//}

 
  
 
  
 
  
 
 

你可能感兴趣的:(QTreeView冻结列(QTreeView第一列不随滑块移动))