#ifndef TABLE_H #define TABLE_H #include #include 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} {} Table(std::initializer_list 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; } 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 mFieldMap; protected: int sizeHintForColumn(int column) const override; }; #endif // TABLE_H