qt/ledset/table.h

67 lines
1.9 KiB
C
Raw Normal View History

2022-08-25 18:43:03 +08:00
#ifndef TABLE_H
#define TABLE_H
#include <QTableWidget>
#include <QHeaderView>
struct ColAttr {
QString field;
QString text;
int width{0};
};
class Table : public QTableWidget {
Q_OBJECT
public:
enum ResizeMode {
Stretch = -0x1000000,
ResizeToContents
};
explicit Table(QWidget *parent = nullptr) : QTableWidget{parent} {}
2022-09-07 23:10:35 +08:00
Table(std::initializer_list<ColAttr> colAttrs, QWidget *parent = nullptr);
2022-08-25 18:43:03 +08:00
2022-09-07 23:10:35 +08:00
inline Table *setDefs() {
2022-08-25 18:43:03 +08:00
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;
}
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