Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 使用自定义QHeaderView读取模型数据_C++_Qt_Qheaderview - Fatal编程技术网

C++ 使用自定义QHeaderView读取模型数据

C++ 使用自定义QHeaderView读取模型数据,c++,qt,qheaderview,C++,Qt,Qheaderview,我想设置一个自定义QHeaderView来旋转水平标题的文本。我在和一个QS标准模型一起工作 目前我还没有这个 class QHeaderViewR : public QHeaderView { public: QHeaderViewR():QHeaderView(Qt::Horizontal) {} void paintSection(QPainter * painter, const QRect & rect, int logicalIndex) const

我想设置一个自定义QHeaderView来旋转水平标题的文本。我在和一个QS标准模型一起工作

目前我还没有这个

class QHeaderViewR : public QHeaderView
{
public:
    QHeaderViewR():QHeaderView(Qt::Horizontal)
    {}

    void paintSection(QPainter * painter, const QRect & rect, int logicalIndex) const
    {
      QPen pen(Qt::black);
      painter->setPen(pen);
      painter->translate(rect.width() * logicalIndex, (logicalIndex * -18) -18);
      painter->rotate(90);
      painter->drawText(rect,"header");
    }
};
我真的不明白我对翻译做了什么。我只是反复尝试,直到它与列匹配为止。但它做得并不完美,而且它毫无理由地剪切了文本。我应该怎样做才能使文本与列匹配而不被截断

另一件事是,我不想只在每一列上写“标题”。要查看的模型的每个列都指定了HorizontalHeaderItem,我想显示这些标题


提前谢谢你的帮助

我解决了它。只是添加了一个QStringList作为构造函数的参数,并使用逻辑索引对其进行迭代。这是最终结果

class QHeaderViewR : public QHeaderView
{
QStringList heads;

public:
    QHeaderViewR(QStringList _heads):QHeaderView(Qt::Horizontal)
    {

        heads = _heads;
    }

    void paintSection(QPainter * painter, const QRect & rect, int logicalIndex) const
    {


        QPen pen(Qt::black);
        painter->setPen(pen);

        painter->rotate(90);
        painter->translate(0,-rect.width()+1);

        QRect copy = rect;

        copy.setWidth(rect.height());
        copy.setHeight(rect.width());
        copy.moveTo(0,-this->sectionPosition(logicalIndex));

        if (logicalIndex == 0)
        {
            copy.setHeight(rect.width()-1);
        }

        painter->drawText(copy, " " + heads.at(logicalIndex));
        painter->drawRect(copy);
    }
};

因为QHeaderView只是一个视图,所以您应该从模型中获取要显示的数据

因此,类似于:

要在模型上设置标题,请使用例如

QStandardItemModel::setHorizontalHeaderLabels(const QStringList &labels)

我找到了匹配文本的方法。该死的旋转比文档显示的要复杂得多。多亏了VoidRealm的一段视频,我终于弄明白了。我仍然没有弄清楚如何从模型中获取数据。
QStandardItemModel::setHorizontalHeaderLabels(const QStringList &labels)