Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
299 views
in Technique[技术] by (71.8m points)

qt - 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;
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...