qt/ledset/table.h

73 lines
2.5 KiB
C
Raw Normal View History

2022-12-16 15:08:53 +08:00
#ifndef TABLE_H
#define TABLE_H
#include <QTableWidget>
#include <QHeaderView>
struct ColAttr {
ColAttr(QString field, QString text, int width = 0, QHeaderView::ResizeMode resizeMode = QHeaderView::Interactive) : field(field), text(text), width(width), resizeMode(resizeMode) {}
ColAttr(QString field, QString text, QHeaderView::ResizeMode resizeMode) : field(field), text(text), resizeMode(resizeMode) {}
QString field;
QString text;
int width{0};
QHeaderView::ResizeMode resizeMode{QHeaderView::Interactive};
};
class Table : public QTableWidget {
Q_OBJECT
public:
explicit Table(QWidget *parent = nullptr) : QTableWidget{parent} {}
Table(std::initializer_list<ColAttr> colAttrs, QWidget *parent = nullptr);
inline Table *setDefs() {
setSelectionBehavior(QTableWidget::SelectRows);
setEditTriggers(QAbstractItemView::NoEditTriggers);
setAlternatingRowColors(true);
horizontalHeader()->setBackgroundRole(QPalette::Window);
return this;
}
inline Table *setStretch() {
horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
return this;
}
inline Table *setResizeToContents() {
horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);
return this;
}
void setHeaderText(QString column, QString text) {
auto col = mFieldMap[column];
auto item = horizontalHeaderItem(col);
if(item==0) setHorizontalHeaderItem(col, item = new QTableWidgetItem());
item->setText(text);
}
auto item(int row, QString column) {
auto col = mFieldMap[column];
return QTableWidget::item(row, col);
}
void setItem(int row, QString column, QTableWidgetItem *item) {
auto col = mFieldMap[column];
QTableWidget::setItem(row, col, item);
}
void setValue(int row, QString column, const QString &text) {
auto col = mFieldMap[column];
QTableWidget::setItem(row, col, new QTableWidgetItem(text));
}
auto cellWidget(int row, QString column) {
auto col = mFieldMap[column];
return QTableWidget::cellWidget(row, col);
}
void setCellWidget(int row, QString column, QWidget *widget) {
auto col = mFieldMap[column];
QTableWidget::setCellWidget(row, col, widget);
}
QMap<QString,int> mFieldMap;
protected:
int sizeHintForColumn(int column) const override;
};
#endif // TABLE_H