qt/ledset/gutil/qgui.cpp

45 lines
1.9 KiB
C++
Raw Normal View History

2023-03-31 18:44:09 +08:00
#include "qgui.h"
2023-05-27 17:43:57 +08:00
#include <QResizeEvent>
2022-08-25 18:43:03 +08:00
2023-03-31 18:44:09 +08:00
const int AlignRight = Qt::AlignRight | Qt::AlignVCenter;
2023-05-27 17:43:57 +08:00
Table::Table(std::initializer_list<ColAttr> colAttrs, int rows, QWidget *parent) : QTableWidget{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);
if(it->width > 0) horizontalHeader()->resizeSection(i, it->width);
2023-05-27 17:43:57 +08:00
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);
}
2023-08-21 11:21:00 +08:00
mFieldMap.emplace(it->field, i++);
2022-09-07 23:10:35 +08:00
}
}
2023-03-31 18:44:09 +08:00
2023-05-27 17:43:57 +08:00
void Table::resizeEvent(QResizeEvent *event) {
QTableWidget::resizeEvent(event);
if(noStretch || event->size().width() == event->oldSize().width()) return;
resizeSec();
}
void Table::resizeSec() {
2022-08-25 18:43:03 +08:00
auto header = horizontalHeader();
2023-05-27 17:43:57 +08:00
int colCnt = columnCount(), remainWidth = header->width(), stretchWidth = 0, secWidth;
QTableWidgetItem *item;
for(int cc=0; cc<colCnt; cc++) if((item = horizontalHeaderItem(cc))) {
2023-08-21 11:21:00 +08:00
if((secWidth = item->data(0x99).toInt()) > 0) stretchWidth += secWidth;
else remainWidth -= header->sectionSize(cc);
}
2023-05-27 17:43:57 +08:00
if(remainWidth<=0 || stretchWidth==0) return;
for(int cc=0; cc<colCnt; cc++) if((item = horizontalHeaderItem(cc)) && (secWidth = item->data(0x99).toInt()) > 0) header->resizeSection(cc, secWidth * remainWidth / stretchWidth);
2022-08-25 18:43:03 +08:00
}
2023-08-21 11:21:00 +08:00
void Table::updateGeometries() {
QTableWidget::updateGeometries();
emit updGeos();
}