简体   繁体   中英

how to create a qtablewidget with custom header

I'm trying to create an exact copy of the following table in Qt

在此处输入图片说明

How would I create a header like that? Is there a way to do it in Qt ?

You could customize every single header column by using setHorizontalHeaderItem or just set the text in all column header by using setHorizontalHeadersLabels .

An easy way to implement your attached image is by customizing your own QWidget. Use a vertical layour and consider your Icp (mA) header as a custom label with a center alignment.

Then insert your QTableWidget and set the headers as (3kOmega, 5.1kOmega & 11kOmega). Something like this:

QWidget* container = new QWidget(this);
QVBoxLayout* layout = new QVBoxLayout(this);

// Custom label
QLabel* header = new QLabel(this);
header->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
header->setAlignment(Qt::AlignHCenter);
header->setText("Icp (mA)");

// Custom QTableWidget
QTableWidget* table = new QTableWidget(this);
table->setColumnCount(3);
QStringList LIST;
LIST << "3k" << "11k" << "15k";
table->setHorizontalHeaderLabels(LIST);
table->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
table->horizontalHeader()->setStretchLastSection(true);

layout->addWidget(header);
layout->addWidget(table);
container->setLayout(layout);
setCentralWidget(container);

Giving you something like this:

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM