Which control to use for this kind of report?
-
I wanted to achieve something like this:
With
QTreeView
so far I've been able to do something like this:and I've to drag those columns to the right manually. In
QTableView
there's no built in grouping option, where I can give it a header style, item style and footer style. In the first GIF I've used a WPF ListBox with GroupStyle. For all sort of Financial Statements I used that technique. How to make this kind of report in Qt? -
I think it's possible. Have look at:
void QHeaderView::setSectionResizeMode(QHeaderView::ResizeMode mode)
Sets the constraints on how the header can be resized to those described by the given mode.
This function was introduced in Qt 5.0.
See also sectionResizeMode(), resizeMode(), length(), and sectionResized().void QHeaderView::setSectionResizeMode(int logicalIndex, QHeaderView::ResizeMode mode)
Sets the constraints on how the section specified by logicalIndex in the header can be resized to those described by the given mode. The logical index should exist at the time this function is called.
Note: This setting will be ignored for the last section if the stretchLastSection property is set to true. This is the default for the horizontal headers provided by QTreeView.
This function was introduced in Qt 5.0.
See also setStretchLastSection() and resizeContentsPrecision(). -
With these:
Window::Window(QWidget *parent) : QWidget(parent){ auto lay = new QVBoxLayout(this); setLayout(lay); tree = new QTreeView(this); auto model = new QStandardItemModel(tree); QFontMetrics fm(font()); int width = 0; model->setColumnCount(3); for (int i = 1; i < 4; i++) { auto header = new QStandardItem("Some Header " + QString::number(i)); header->setFont(QFont(font().family(), -1, QFont::Bold, false)); for (int j = 1; j < 3; j++) { auto col1 = new QStandardItem("Some text"); auto col2 = new QStandardItem(QString::number(100)); auto col3 = new QStandardItem(QString::number(100)); header->appendRow(QList<QStandardItem*>() << col1 << col2 << col3); } model->appendRow(header); auto col1 = new QStandardItem("Total"); auto text = QString::number(100); auto col2 = new QStandardItem(text); auto col3 = new QStandardItem(text); auto w = fm.boundingRect(text).width(); if(w > width) width = w; model->appendRow(QList<QStandardItem*>() << col1 << col2 << col3); } tree->setModel(model); tree->setItemDelegate(new Delegate()); tree->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); tree->header()->setStretchLastSection(false); tree->header()->hide(); tree->setColumnWidth(1, width); tree->setColumnWidth(2, width); tree->expandAll(); tree->setRootIsDecorated(false); lay->addWidget(tree); } Window::~Window(){ } void Window::resizeEvent(QResizeEvent*){ auto total = tree->width(); auto remaining = total - tree->columnWidth(1) - tree->columnWidth(2) - tree->style()->pixelMetric(QStyle::PM_ScrollBarExtent); tree->setColumnWidth(0, remaining); }
got rid of the manual resizing issue:
Still, treeview isn't, probably, the right control to do that! If anyone knows some better way, let me know.
-
I think it's possible. Have look at:
void QHeaderView::setSectionResizeMode(QHeaderView::ResizeMode mode)
Sets the constraints on how the header can be resized to those described by the given mode.
This function was introduced in Qt 5.0.
See also sectionResizeMode(), resizeMode(), length(), and sectionResized().void QHeaderView::setSectionResizeMode(int logicalIndex, QHeaderView::ResizeMode mode)
Sets the constraints on how the section specified by logicalIndex in the header can be resized to those described by the given mode. The logical index should exist at the time this function is called.
Note: This setting will be ignored for the last section if the stretchLastSection property is set to true. This is the default for the horizontal headers provided by QTreeView.
This function was introduced in Qt 5.0.
See also setStretchLastSection() and resizeContentsPrecision(). -
@mpergand, forgot that! used that in
QueryLite
. With these:tree->header()->setSectionResizeMode(0, QHeaderView::Stretch); tree->header()->setSectionResizeMode(1, QHeaderView::ResizeToContents); tree->header()->setSectionResizeMode(2, QHeaderView::ResizeToContents);
no need to calculate the width of text in columns and I can get rid of that
resizeEvent
.