QTableView:交互式列调整大小而不使用QHeaderView

5

我有一个隐藏了水平表头的 QTableView

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

无标题视图

从图中可以看到,由于栏目宽度的限制,中心栏的文本被裁剪了。

要查看文本,用户需要调整栏目大小,但是没有标题,我无法做到这一点。

我想做的是将鼠标悬停在栏目边缘上,并使普通的调整大小图标出现,然后允许用户拖动栏目变宽。

这种操作是否可行?


1
也许解决方案是添加一个按钮,使用“ResizeColumnsToContents”来调整所有列的大小。 - Michael
1个回答

6

使用事件过滤器和以下两个类使某些东西能够工作...

/*
 * 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;
};

由于这样做更加简洁,我最终将其分成两个类。

无论如何,在我找到的一些旧示例代码中,仅仅将QTableView替换为resizable_headerless_table_view似乎已经产生了期望的效果——当鼠标位于列边界上方时,光标精灵会改变,并且相关的边界可以被拖动。

不确定它是否符合您的要求,但是……


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接