//mainwindow.h
#ifndef TABLEWIDGET_H
#define TABLEWIDGET_H
#include <QWidget>
#include <QTableWidget>
#include <QLabel>
class TableWidget : public QWidget
{
Q_OBJECT
public:
TableWidget(QWidget *parent = 0);
~TableWidget();
void init();
protected:
bool eventFilter(QObject *obj, QEvent *e);
private:
QTableWidget *table;
QLabel *label;
};
#endif // TABLEWIDGET_H
//mainwindow.cpp
#include "tablewidget.h"
#include <QEvent>
#include <QMouseEvent>
TableWidget::TableWidget(QWidget *parent)
: QWidget(parent)
{
init();
}
TableWidget::~TableWidget()
{
}
void TableWidget::init()
{
table = new QTableWidget(this);
table->setRowCount(7);
table->setColumnCount(5);
table->resize(this->size());
label = new QLabel(table);
label->setText(QString("ID"));
label->move(1, 5);
label->installEventFilter(this);
}
bool TableWidget::eventFilter(QObject *obj, QEvent *e)
{
if (obj == label){
if (e->type() == QEvent::MouseButtonPress){
QMouseEvent *mouseEvent = static_cast<QMouseEvent *>(e);
if (mouseEvent->button() == Qt::LeftButton){
table->selectAll();
return true;
}else{
return false;
}
}
}
}