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

天大地大妈咪最大 提交于 2020-01-07 09:28:11

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!