问题
There are N
columns with manual resizing width from left. Other columns widths are resizing only when columns with manual resizing are resizing. I need to prevent cursor icon changing when cursor is under borders of sections without manual resizing.
What did I try to do. But this is work not very good.
table_header_border.zip
#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;
}
回答1:
You should use QEvent::Enter
and QEvent::Leave
for better result.
Use next event filter:
In header:
protected:
bool eventFilter(QObject *obj, QEvent *event);
In constructor:
qApp->installEventFilter(this);
EventFilter:
bool MainWindow::eventFilter(QObject *obj, QEvent *event)
{
if (obj == ui->tableWidget && event->type() == QEvent::Enter)
{
qApp->setOverrideCursor(QCursor(Qt::ArrowCursor));
//or
qApp->setOverrideCursor(ui->tableWidget->cursor());
qDebug() << "added";
}
if (obj == ui->tableWidget && event->type() == QEvent::Leave)
{
qApp->restoreOverrideCursor();
}
return QObject::eventFilter(obj, event);
}
回答2:
horizontalHeader()->setSectionResizeMode(i, QHeaderView::Fixed);
来源:https://stackoverflow.com/questions/26326555/how-can-i-prevent-transform-cursor-to-splithcursor-when-its-under-border-betwee