从左侧有N
手动调整宽度的列。仅当手动调整大小的列正在调整大小时,才会调整其他列的宽度。当光标位于部分边框下方时,我需要防止光标图标发生变化,而无需手动调整大小。
我试图做什么。但这是工作不是很好。
#include "mainwindow.h"
#include "ui_mainwindow.h"
const int N = 2;
//==============================================================================
int nWidth(const QTableWidget *table)
{
int ret = 0;
if (table->verticalHeader()->isVisible())
{
ret += table->verticalHeader()->width();
}
for (int i = 0; i < N; i++)
{
ret += table->columnWidth(i);
}
return ret;
}
bool isInNColumns(const QTableWidget *table)
{
QPoint cursorPos = table->mapFromGlobal(QCursor::pos());
return cursorPos.x() < nWidth(table) + 5;
}
//==============================================================================
class MyHorizontalHeader : public QHeaderView
{
public:
MyHorizontalHeader(QWidget *parent=0) : QHeaderView(Qt::Horizontal, parent)
{
setMouseTracking(true);
}
private slots:
void mouseMoveEvent(QMouseEvent *event)
{
QHeaderView::mouseMoveEvent(event);
if (cursor().shape() == Qt::SplitHCursor)
{
QTableWidget *table = dynamic_cast<QTableWidget *>(parent());
if (table != NULL && !isInNColumns(table))
{
qApp->setOverrideCursor(QCursor(Qt::ArrowCursor));
return;
}
qApp->setOverrideCursor(QCursor(Qt::SplitHCursor));
}
}
};
//==============================================================================
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->tableWidget->setHorizontalHeader(new MyHorizontalHeader(this));
}
MainWindow::~MainWindow()
{
delete ui;
}