任何人都可以给我一些在 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();
}
最佳答案
我创建了一个继承自QHBoxLayout
的列跟踪布局。它不如将小部件嵌入到标题中那么好,但至少它使小部件看起来绑定(bind)到各自的表格列:
演示屏幕截图
该项目托管在 GitHub 上:https://github.com/sashoalm/ColumnAlignedLayout .
您只需要columnalignedlayout.cpp和columnalignedlayout.h
由于它们足够小,我将直接粘贴它们。
columnalignedlayout.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
列对齐布局.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()));
然后在槽中调用invalidate():
void MainWindow::invalidateAlignedLayout()
{
alignedLayout->invalidate();
}
关于excel - 如何在QTableWidget上实现类似Excel的过滤机制?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8384309/