2017-05-09 14 views
1

없이 내가 숨겨진 수평 헤더 당신이 볼 수 있듯이, 중앙 열의 텍스트 때문에 열 너비의립니다QTableView : 상호 작용하는 열은 QHeaderView

table->horizontalHeader()->hide(); 

No header view

QTableView이 크기를 조정합니다.

텍스트를 보려면 사용자가 열의 크기를 조정해야하지만 헤더가 없으면이 작업을 수행 할 수 없습니다.

내가 할 수 있기를 원하는 것은 내 마우스를 열의 가장자리 위로 가져 가서 일반 크기 조정 아이콘이 나타나도록 한 다음 사용자가 열을 더 넓게 끌 수있게하는 것입니다.

이것이 가능합니까? 이 청소기 보였다으로 이벤트 필터 다음과 같은 두 개의 클래스를 사용하여 작업 할 수

+0

아마 솔루션은 ResizeColumnsToContents''모든 컬럼의 크기를 조절하는 버튼을 추가하는 것입니다. –

답변

1

있어 뭔가 ...

/* 
* Subclass of QTableView that provides notification when the mouse cursor 
* enters/leaves a column boundary. 
*/ 
class headerless_table_view: public QTableView { 
    using super = QTableView; 
public: 
    explicit headerless_table_view (QWidget *parent = nullptr) 
    : super(parent) 
    , m_boundary_width(10) 
    , m_column_index(-1) 
    { 
     viewport()->setMouseTracking(true); 
     viewport()->installEventFilter(this); 
    } 

    /* 
    * @return The index of the column whose right hand boundary the cursor lies 
    *   on or -1 if not on a boundary. 
    */ 
    int column_index() const 
    { 
     return(m_column_index); 
    } 
protected: 
    virtual bool eventFilter (QObject *obj, QEvent *event) override 
    { 
     if (event->type() == QEvent::MouseMove) { 
     if (auto *e = dynamic_cast<QMouseEvent *>(event)) { 
      auto col_left = columnAt(e->pos().x() - m_boundary_width/2); 
      auto col_right = columnAt(e->pos().x() + m_boundary_width/2); 
      bool was_on_boundary = m_column_index != -1; 
      if (col_left != col_right) { 
      if (m_column_index == -1) { 
       if (col_left != -1) { 
       m_column_index = col_left; 
       } 
      } 
      } else { 
      m_column_index = -1; 
      } 
      bool is_on_boundary = m_column_index != -1; 
      if (is_on_boundary != was_on_boundary) { 
      entered_column_boundary(is_on_boundary); 
      } 
     } 
     } 
     return(super::eventFilter(obj, event)); 
    } 

    /* 
    * Called whenever the cursor enters or leaves a column boundary. if 
    * `entered' is true then the index of the column can be obtained using 
    * `column_index()'. 
    */ 
    virtual void entered_column_boundary (bool entered) 
    { 
    } 
private: 
    int m_boundary_width; 
    int m_column_index; 
}; 

/* 
* Subclass of headerless_table_view that allows resizing of columns. 
*/ 
class resizable_headerless_table_view: public headerless_table_view { 
    using super = headerless_table_view; 
public: 
    explicit resizable_headerless_table_view (QWidget *parent = nullptr) 
    : super(parent) 
    , m_dragging(false) 
    { 
     viewport()->installEventFilter(this); 
    } 
protected: 
    virtual bool eventFilter (QObject *obj, QEvent *event) override 
    { 
     if (auto *e = dynamic_cast<QMouseEvent *>(event)) { 
     if (event->type() == QEvent::MouseButtonPress) { 
      if (column_index() != -1) { 
      m_mouse_pos = e->pos(); 
      m_dragging = true; 
      return(true); 
      } 
     } else if (event->type() == QEvent::MouseButtonRelease) { 
      m_dragging = false; 
     } else if (event->type() == QEvent::MouseMove) { 
      if (m_dragging) { 
      int delta = e->pos().x() - m_mouse_pos.x(); 
      setColumnWidth(column_index(), columnWidth(column_index()) + delta); 
      m_mouse_pos = e->pos(); 
      return(true); 
      } 
     } 
     } 
     return(super::eventFilter(obj, event)); 
    } 

    /* 
    * Override entered_column_boundary to update the cursor sprite when 
    * entering/leaving a column boundary. 
    */ 
    virtual void entered_column_boundary (bool entered) override 
    { 
     if (entered) { 
     m_cursor = viewport()->cursor(); 
     viewport()->setCursor(QCursor(Qt::SplitHCursor)); 
     } else { 
     viewport()->setCursor(m_cursor); 
     } 
    } 
private: 
    bool m_dragging; 
    QPoint m_mouse_pos; 
    QCursor m_cursor; 
}; 

나는 두 개의 클래스를 통해 그것을 분할 끝났다.

어쨌든, 이전의 예제 코드에서 QTableViewresizable_headerless_table_view으로 바꾸는 것만으로도 원하는 결과를 얻은 것으로 보입니다. 마우스를 열 경계 위로 이동하고 관련 경계를 끌면 커서 스프라이트가 변경됩니다.

가 필요하시면 얼마나 가까운 확실하지,하지만 ...이

+0

와우, 정말 고마워! 고마워! 나는 그것을 시도 할 것이다! –