qtqtablewidgetqcursor

How can I prevent transform cursor to SplitHCursor when it's under border between QHeaderView sections


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.

enter image description here

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

Solution

  • horizontalHeader()->setSectionResizeMode(i, QHeaderView::Fixed);