3

谁能给我一些想法来在 QTableWidget 上创建一个过滤器机制(如 Microsoft Excel 中可用的)?

每当我单击列名时,我希望标题过滤器机制自动为该表激活。

我在 Windows 上构建。


更新这是我的部分实现。但我需要帮助实现插槽testanother(const QString &text)以在表中显示匹配数据并隐藏不匹配的数据。

bool TableData::filterSlot() {
    int columnCount = this->tablewidget->columnCount();
    int rowCount = this->tablewidget->rowCount();
    QStringList filterList;
    QString temp_string;
    qDebug()<<"Count inside filter slot is";
    qDebug()<<rowCount<<":"<<columnCount;
    for(int c = 0; c<columnCount;c++) {
        for(int r = 0; r<rowCount;r++) {
            temp_string = this->tablewidget->item(r,c)->text(); 
            if(!filterList.contains(temp_string))
                filterList << temp_string;
        }
        filterList << "None";
        combo = new QComboBox(tablewidget);
        combo->addItems(filterList);
        combo->setCurrentIndex(filterList.count()-1);
        this->tablewidget->setCellWidget(0,c,combo);
        filterList.clear();
        connect(combo,SIGNAL(activated(const QString &)),
            this,SLOT(testAnother(const QString &)));
    }
    return true;
}

void TableData::testAnother(const QString &text) {
    int c = sender()->objectName().toInt();
}
4

2 回答 2

3

我创建了一个列跟踪布局,它继承自QHBoxLayout. 它不如将小部件嵌入到标题中,但至少它给出了小部件绑定到它们各自的表格列的外观:

演示截屏

在此处输入图像描述

该项目托管在 GitHub 上,地址为https://github.com/sashoalm/ColumnAlignedLayout

您只需要 columnalignedlayout.cpp 和 columnalignedlayout.h

由于它们足够小,我将直接粘贴它们。

列对齐布局.h

#ifndef COLUMNALIGNEDLAYOUT_H
#define COLUMNALIGNEDLAYOUT_H

#include <QHBoxLayout>

class QHeaderView;

class ColumnAlignedLayout : public QHBoxLayout
{
    Q_OBJECT
public:
    ColumnAlignedLayout();
    explicit ColumnAlignedLayout(QWidget *parent);
    void setTableColumnsToTrack(QHeaderView *view) { headerView = view; }

signals:

public slots:

private:
    void setGeometry(const QRect &r);
    QHeaderView *headerView;
};

#endif // COLUMNALIGNEDLAYOUT_H

columnalignedlayout.cpp

#include "columnalignedlayout.h"
#include <QHeaderView>

ColumnAlignedLayout::ColumnAlignedLayout()
    : QHBoxLayout()
{

}

ColumnAlignedLayout::ColumnAlignedLayout(QWidget *parent)
    : QHBoxLayout(parent)
{

}

void ColumnAlignedLayout::setGeometry(const QRect &r)
{
    QHBoxLayout::setGeometry(r);

    Q_ASSERT_X(headerView, "layout", "no table columns to track");
    if (!headerView) {
        return;
    }

    Q_ASSERT_X(headerView->count() == count(), "layout", "there must be as many items in the layout as there are columns in the table");
    if (headerView->count() != count()) {
        return;
    }

    Q_ASSERT(parentWidget());

    int widgetX = parentWidget()->mapToGlobal(QPoint(0, 0)).x();
    int headerX = headerView->mapToGlobal(QPoint(0, 0)).x();
    int delta = headerX - widgetX;

    for (int ii = 0; ii < headerView->count(); ++ii) {
        int pos = headerView->sectionViewportPosition(ii);
        int size = headerView->sectionSize(ii);

        auto item = itemAt(ii);
        auto r = item->geometry();
        r.setLeft(pos + delta);
        r.setWidth(size);
        item->setGeometry(r);
    }
}

示例用法:

alignedLayout = new ColumnAlignedLayout();
alignedLayout->addWidget(new QLineEdit(this));
alignedLayout->addWidget(new QLineEdit(this));
alignedLayout->addWidget(new QLineEdit(this));
alignedLayout->addWidget(new QLineEdit(this));
ui->widget->setLayout(alignedLayout);
alignedLayout->setTableColumnsToTrack(ui->tableWidget->horizontalHeader());
alignedLayout->setParent(ui->widget);
connect(ui->tableWidget->horizontalHeader(), SIGNAL(sectionResized(int,int,int)), SLOT(invalidateAlignedLayout()));
connect(ui->tableWidget->horizontalScrollBar(), SIGNAL(valueChanged(int)), SLOT(invalidateAlignedLayout()));

然后在 slot 中调用 invalidate():

void MainWindow::invalidateAlignedLayout()
{
    alignedLayout->invalidate();
}
于 2016-09-11T10:57:37.440 回答
1

没有任何内置功能可以实现该特定功能。但是你可以从 QHeaderView 派生出你自己的类,就像这个人所做的那样:

http://lists.qt.nokia.com/pipermail/qt-interest/2009-August/011654.html

我试过了,它似乎成功地将小部件——一个文本框和一个组合框——放在了相应的标题列下方。使用这种技术,如果您有一个长表格并滚动,过滤器将保留在标题中。

(有一个先前的线程,人们建议将“假”行放入代理数据模型中。这是一种非常可怕的方法,当您向下滚动数据时,过滤器列也会从顶部滚动。)

他的示例代码没有显示过滤。但是您可以通过查看基本排序/过滤模型示例来了解该功能的模式:

http://doc.qt.nokia.com/latest/itemviews-basicsortfiltermodel.html

好消息是 Qt 足够灵活来处理这种事情。坏(?)消息是,您放在列标题中的那些小部件如何填充取决于您。除非您编写它,否则不会进行任何扫描来查找唯一值并将它们呈现在组合框中。不过,使用固定列表填充将是微不足道的。

于 2011-12-05T16:39:26.247 回答