#include "qgui.h" #include const int AlignRight = Qt::AlignRight | Qt::AlignVCenter; Table::Table(std::initializer_list colAttrs, int rows, QWidget *parent) : QTableWidget{rows, (int)colAttrs.size(), parent} { int i = 0; for(typename std::initializer_list::const_iterator it = colAttrs.begin(); it != colAttrs.end(); ++it) { auto item = horizontalHeaderItem(i); if(item==0) setHorizontalHeaderItem(i, item = new QTableWidgetItem()); item->setText(it->text); if(it->width > 0) horizontalHeader()->resizeSection(i, it->width); if(it->resizeMode != QHeaderView::Interactive) { if(it->resizeMode==QHeaderView::Stretch && it->width>0) { item->setData(0x99, it->width > 0 ? it->width : 100); noStretch = false; } else horizontalHeader()->setSectionResizeMode(i, (QHeaderView::ResizeMode)it->resizeMode); } mFieldMap.emplace(it->field, i++); } } void Table::resizeEvent(QResizeEvent *event) { QTableWidget::resizeEvent(event); if(noStretch || event->size().width() == event->oldSize().width()) return; resizeSec(); } void Table::resizeSec() { auto header = horizontalHeader(); int colCnt = columnCount(), remainWidth = header->width(), stretchWidth = 0, secWidth; QTableWidgetItem *item; for(int cc=0; ccdata(0x99).toInt()) > 0) stretchWidth += secWidth; else remainWidth -= header->sectionSize(cc); } if(remainWidth<=0 || stretchWidth==0) return; for(int cc=0; ccdata(0x99).toInt()) > 0) header->resizeSection(cc, secWidth * remainWidth / stretchWidth); } void Table::updateGeometries() { QTableWidget::updateGeometries(); emit updGeos(); }