谁能给我一些在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
继承的列跟踪布局。它不像将小部件嵌入标题中那样好,但是至少它使小部件绑定到其各自的表列上:
演示屏幕截图
该项目托管在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
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()));
然后在插槽中调用invalidate():
void MainWindow::invalidateAlignedLayout()
{
alignedLayout->invalidate();
}
关于excel - 如何在QTableWidget上实现类似Excel的筛选器机制?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/8384309/