qt/ledset/gutil/qgui.cpp

34 lines
1.6 KiB
C++
Raw Normal View History

2023-03-31 18:44:09 +08:00
#include "qgui.h"
2022-08-25 18:43:03 +08:00
2023-03-31 18:44:09 +08:00
const int AlignRight = Qt::AlignRight | Qt::AlignVCenter;
Table::Table(std::initializer_list<ColAttr> colAttrs, int rows, QWidget *parent) : NumTable{rows, (int)colAttrs.size(), parent} {
2022-09-07 23:10:35 +08:00
int i = 0;
for(typename std::initializer_list<ColAttr>::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);
item->setData(0x99, it->width);
if(it->width > 0) horizontalHeader()->resizeSection(i, it->width);
2023-03-31 18:44:09 +08:00
if(it->resizeMode > -1) horizontalHeader()->setSectionResizeMode(i, (QHeaderView::ResizeMode)it->resizeMode);
2022-09-07 23:10:35 +08:00
mFieldMap.insert(it->field, i++);
}
}
2023-03-31 18:44:09 +08:00
2022-08-25 18:43:03 +08:00
int Table::sizeHintForColumn(int column) const {
auto item = horizontalHeaderItem(column);
2022-09-07 23:10:35 +08:00
if(item==0) return QTableWidget::sizeHintForColumn(column);
2022-08-25 18:43:03 +08:00
int width = item->data(0x99).toInt();
2022-09-07 23:10:35 +08:00
if(width==0) return QTableWidget::sizeHintForColumn(column);
2022-08-25 18:43:03 +08:00
auto header = horizontalHeader();
2022-12-16 15:08:53 +08:00
if(header->sectionResizeMode(column) != QHeaderView::ResizeToContents) return QTableWidget::sizeHintForColumn(column);
2022-08-25 18:43:03 +08:00
int colCnt = columnCount();
int remainWidth = header->width(), stretchWidth = width;
for(int cc=0; cc<colCnt; cc++) if(cc!=column && (item = horizontalHeaderItem(cc))) {
2022-12-16 15:08:53 +08:00
if(header->sectionResizeMode(cc) == QHeaderView::ResizeToContents) stretchWidth += item->data(0x99).toInt();
2022-08-25 18:43:03 +08:00
else remainWidth -= item->data(0x99).toInt();
}
if(remainWidth<=0) return QTableWidget::sizeHintForColumn(column);
return width * remainWidth / stretchWidth;
}