Отображение позиции в QHeaderView в контекст прокрученного окна просмотра - PullRequest
0 голосов
/ 25 июня 2019

У меня есть QHeaderView, который делает несколько вещей, нажимая слева или справа от центра заголовка раздела.

Для этого мне нужно знать позицию щелчка по отношению к (возможно прокручиваемому) содержимому окна просмотра QHeaderView . Фактическая позиция щелчка, однако, относится только к QHeaderView (который всегда фиксирован).

Я пробовал варианты mapTo / From, но не могу найти правильный способ сделать это. Вот упрощенный код:

void MyTableHeader::headerSectionClicked(int section_index, int click_pos)
{
    int section_size = sectionSize(0); // All sections in a header are equally sized
    int section_center = (section_size * (section_index+ 1)) - (section_size / 2); // Center of the clicked section

    if (section_index>= 0)// if mouse is over an item
    {
        if (orientation() == Qt::Horizontal)
        {
            QPoint x_pos = QPoint(click_pos, 0);
            int mapped_offset = viewport()->mapFrom(this, x_pos).x();

            if (mapped_offset != -1)
            {
                // If the click was to the right of the center, iterate on the index
                if (mapped_offset >= section_center)
                {
                    section_index++;
                }
            }
        }
        else
        {
            // Same thing for the Y-dimension
        }
    }

    // Neat stuff after this
}

В той части, где возникает проблема, я хочу узнать, на какой стороне раздела произошел щелчок.

// If the click was to the right of the center, iterate on the index
if (mapped_offset >= section_center)
{
    in_index++;
}

Этот mapped_offset неправильно ссылается на тот же контекст, что и центр сечения.

1 Ответ

1 голос
/ 25 июня 2019

Следующее решение может дать вам представление о том, что делать.

MyHeaderView.h

#pragma once

#include <QHeaderView>

class MyHeaderView : public QHeaderView
{
    Q_OBJECT
public:
    MyHeaderView(Qt::Orientation orientation, QWidget* parent = nullptr);
};

MyHeaderView.cpp

#include "MyHeaderView.h"
#include <QDebug>

MyHeaderView::MyHeaderView(Qt::Orientation orientation, QWidget* parent) : QHeaderView(orientation, parent)
{
    setSectionsClickable(true);
    connect(this, &MyHeaderView::sectionClicked, [this](int section)
        {
            QRect currentSectionRect;
            currentSectionRect.setRect(sectionViewportPosition(section), 0, sectionSize(section), viewport()->height());
            auto pos = QCursor::pos();
            auto localPos = mapFromGlobal(pos);

            qDebug() << currentSectionRect << localPos;
            qDebug() << currentSectionRect.contains(localPos); //"Always true!"
        });
}

main.cpp

#include "MyHeaderView.h"
#include <QApplication>
#include <QTableView>
#include <QStandardItemModel>

int main(int argc, char **args)
{
    QApplication app(argc, args);
    auto model = new QStandardItemModel;
    auto view = new QTableView;
    view->setModel(model);
    model->setRowCount(4);
    model->setColumnCount(4);
    view->setHorizontalHeader(new MyHeaderView(Qt::Horizontal));
    view->show();
    app.exec();
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...