4

将个人 Widget (在我的情况下是一组 QPixmap + QLabel + QComboBox )作为 QTableWidget 中的标题的最简单方法是什么(如果可能,不子类化 QTableWidget )?

因为我们有函数QTableWidget::setCellWidget(int row, int column, QWidget * widget),所以对数组内容做起来相对容易,但是对于标题,我们只有QTableWidget::setHorizontalHeaderItem (int column, QTableWidgetItem * item)

非常感谢你 !

编辑:也许我找到了一种非常不雅的方法来做到这一点,它可能会奏效:

1)我用一个空的 QString 标题初始化每一列

2)我用和提取标题的每个部分的大小和_table->horizontalHeader()->sectionPosition(index)位置_table->horizontalHeader()->sectionSize(index)

3)然后我在每个部分上画一个 QFrame 并留有一点边距

4)最后,我将我的 QWidgets 加载到这些 QFrame 中

如果其他人有更好的解决方案,我很感兴趣......

4

2 回答 2

1

我想setCellWidget()会完成这项工作。我没有看到更简单的方法。如果你不使用 QFrame 的附加功能,你也可以使用 QWidget 来代替 QFrame。

于 2012-08-16T16:33:17.397 回答
1

我做了很多研究来创建一个类似过滤器标题的excel。

更换标头没有简单快捷的方法。

最后我找到了这篇博文:Qt Support Weekly #27 – Widgets on a header。根据其指南,这里是创建 QHeaderView 和 QTableWidget 子类的完整代码,该子类可以接受自定义小部件作为标题项:

customtableheader.h:

#ifndef CUSTOMTABLEHEADER_H
#define CUSTOMTABLEHEADER_H

#include <QWidget>
#include <QHeaderView>

class CustomTableHeader : public QHeaderView
{
    public:
        struct Margins
        {
            int left;
            int right;
            int top;
            int buttom;

            Margins(int left = 2, int right = 2, int top = 2, int buttom = 2);
        };

        CustomTableHeader(Qt::Orientation orientation,
                          QWidget         *parent = nullptr);

        void FixComboPositions();

        void SetItemWidget(int index, QWidget * widget);
        void SetItemMargins(int index, Margins margins);

    private:
        struct Item
        {
            QWidget * item;
            Margins margins;

            Item();
        };

        QMap<int, Item> mItems;

        void showEvent(QShowEvent * e);
        void HandleSectionResized(int i);
        void HandleSectionMoved(int logical, int oldVisualIndex, int newVisualIndex);
};
#endif // CUSTOMTABLEHEADER_H

customtableheader.cpp:

#include "customtableheader.h"

CustomTableHeader::CustomTableHeader(Qt::Orientation orientation, QWidget * parent) :
    QHeaderView(orientation, parent)
{
    connect(this, &CustomTableHeader::sectionResized, this, &CustomTableHeader::HandleSectionResized);
    connect(this, &CustomTableHeader::sectionMoved,   this, &CustomTableHeader::HandleSectionMoved);
}


void CustomTableHeader::showEvent(QShowEvent *e)
{
    for (int i = 0; i < count(); i++)
    {
        if (!mItems[i].item)
            mItems[i].item = new QWidget(this);
        else
            mItems[i].item->setParent(this);

        mItems[i].item->setGeometry(sectionViewportPosition(i) + mItems[i].margins.left,
                                    mItems[i].margins.top,
                                    sectionSize(i) - mItems[i].margins.left - mItems[i].margins.right - 1,
                                    height() - mItems[i].margins.top - mItems[i].margins.buttom - 1);
        mItems[i].item->show();
    }

    QHeaderView::showEvent(e);
}


void CustomTableHeader::HandleSectionResized(int i)
{
    int logical;

    for (int j = visualIndex(i); j < count(); j++)
    {
        logical = logicalIndex(j);
        mItems[logical].item->setGeometry(sectionViewportPosition(logical) + mItems[i].margins.left,
                                          mItems[i].margins.top,
                                          sectionSize(logical) - mItems[i].margins.left - mItems[i].margins.right - 1,
                                          height() - mItems[i].margins.top - mItems[i].margins.buttom - 1);
    }
}


void CustomTableHeader::HandleSectionMoved(int logical, int oldVisualIndex, int newVisualIndex)
{
    Q_UNUSED(logical);

    for (int i = qMin(oldVisualIndex, newVisualIndex); i < count(); i++)
    {
        int logical = logicalIndex(i);
        mItems[logical].item->setGeometry(sectionViewportPosition(logical) + mItems[i].margins.left,
                                          mItems[i].margins.top,
                                          sectionSize(logical) - mItems[i].margins.left - mItems[i].margins.right - 1,
                                          height() - mItems[i].margins.top - mItems[i].margins.buttom - 1);
    }
}


void CustomTableHeader::FixComboPositions()
{
    for (int i = 0; i < count(); i++)
        mItems[i].item->setGeometry(sectionViewportPosition(i) + mItems[i].margins.left,
                                    mItems[i].margins.top,
                                    sectionSize(i) - mItems[i].margins.left - mItems[i].margins.right - 1,
                                    height() - mItems[i].margins.top - mItems[i].margins.buttom - 1);
}


void CustomTableHeader::SetItemWidget(int index, QWidget * widget)
{
    widget->setParent(this);
    mItems[index].item = widget;
}


void CustomTableHeader::SetItemMargins(int index, CustomTableHeader::Margins margins)
{
    mItems[index].margins = margins;
}


CustomTableHeader::Margins::Margins(int left, int right, int top, int buttom) :
    left(left),
    right(right),
    top(top),
    buttom(buttom)

{
}


CustomTableHeader::Item::Item() :
    item(nullptr)
{
}

customheaderedtable.h:

#ifndef CUSTOMHEADEREDTABLE_H
#define CUSTOMHEADEREDTABLE_H

#include <QtCore/qglobal.h>
#include <QTableWidget>

#include "customtableheader.h"

class CustomHeaderedTable : public QTableWidget
{
    public:
        CustomHeaderedTable(QWidget * parent = Q_NULLPTR);

        void scrollContentsBy(int dx, int dy);

        void SetHorizontalHeaderItemWidget(int column, QWidget * widget);
        void SetHorizontalHeaderItemMargins(int column, CustomTableHeader::Margins margins);

    private:
        CustomTableHeader * mHorizontalHeader;
};
#endif // CUSTOMHEADEREDTABLE_H

customheaderedtable.cpp:

#include "customheaderedtable.h"

CustomHeaderedTable::CustomHeaderedTable(QWidget * parent) :
    QTableWidget(parent)
{
    mHorizontalHeader = new CustomTableHeader(Qt::Orientation::Horizontal, this);
    setHorizontalHeader(mHorizontalHeader);
}


void CustomHeaderedTable::scrollContentsBy(int dx, int dy)
{
    QTableWidget::scrollContentsBy(dx, dy);

    if (dx != 0)
        mHorizontalHeader->FixComboPositions();
}


void CustomHeaderedTable::SetHorizontalHeaderItemWidget(int column, QWidget * widget)
{
    mHorizontalHeader->SetItemWidget(column, widget);
}


void CustomHeaderedTable::SetHorizontalHeaderItemMargins(int column, CustomTableHeader::Margins margins)
{
    mHorizontalHeader->SetItemMargins(column, margins);
}

样品用途:

QComboBox * combo = new QComboBox();
combo->addItem("1");
combo->addItem("2");
combo->addItem("3");

CustomHeaderedTable table = new CustomHeaderedTable(this);
table->setRowCount(70);
table->setColumnCount(70);
table->SetHorizontalHeaderItemWidget(1, combo);
于 2017-11-08T09:25:54.243 回答