QTableView:没有 QHeaderView 的交互式列调整大小

标签 qt qtableview qheaderview

我有一个 QTableView带有隐藏的水平标题

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

No header view

如您所见,由于列宽,中间列中的文本被剪裁了。

要查看文本,用户需要调整列的大小,但没有标题,我无法执行此操作。

我希望能够做的是将鼠标悬停在列的边缘并显示正常的调整大小图标,然后允许用户将列拖得更宽。

这可能吗?

最佳答案

使用事件过滤器和以下两个类可以工作......

/*
 * 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在一些旧的示例代码中,我发现似乎具有预期的效果——当鼠标位于列边界上时,光标 Sprite 会发生变化,并且可以拖动相关边界。

不确定它与您所追求的有多接近,但是......

关于QTableView:没有 QHeaderView 的交互式列调整大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43866209/

相关文章:

Qt字体跟踪

c++ - Qt 5中String的Base64编码

python - PyQt4 无法创建简单的 GUI 应用程序

c++ - 更改 QHeaderView 数据

c++ - Qt XML 中的属性顺序不正确

qt - PyQt-QtableView header 。如何添加右上角的小按钮来隐藏/显示带有选中/未选中复选框的列?

c++ - 多列列表的 Qt 最佳控件?

c++ - 在 QTableView 中选择多个项目时如何禁用按钮

python - 防止 TableView 标题突出显示

c++ - 添加自定义小部件作为 QTableWidget Horizo​​ntalHeader