添加自定义小部件作为 QTableWidget HorizontalHeader

问题描述 投票:0回答:3

是否可以在

QWidget
上显示自定义
QHeaderView
而不仅仅是通用字符串?

另外,是否可以仅更改

horizontalHeaderItem
的高度?当我这样做时它崩溃了:

(ui->myTable->horizontalHeaderItem(0))->setSizeHint(QSize(200,300));
c++ qt qt5 qtablewidget qheaderview
3个回答
6
投票

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

替换 header 没有简单快速的方法。

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

自定义表头.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

自定义表头.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)
{
}

自定义标题表.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

自定义标题表.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);

0
投票

关于第一个问题:

您可以使用

setIndexWidget
QHeaderView
。但我认为为此目的您应该使用自定义
QTableView
QHeaderView

关于你的第二个问题:

使用

horizontalHeaderItem(0)
之前,您应该创建并设置此项目:
setHorizontalHeaderItem(0, new QTableWidgetItem("Test"));


-1
投票

我通过在我的

.cpp
文件中而不是在设计文件中以编程方式添加标头来解决这个问题。

QTableWidgetItem *header1 = new QTableWidgetItem("Title of Header 1");
header1->setBackgroundColor(QColor(81,81,81));
header1->setIcon(QIcon(QPixmap(":/images/header1Icon.png")));    
header1->setSizeHint(QSize(450,35));
ui->myTable->setHorizontalHeaderItem(0, header1); 
© www.soinside.com 2019 - 2024. All rights reserved.