124 lines
3.6 KiB
C++
124 lines
3.6 KiB
C++
#pragma execution_character_set("utf-8")
|
|
|
|
#include "checkbox_header_table.h"
|
|
#include <QPainter>
|
|
#include <QApplication>
|
|
#include <QTextCodec>
|
|
|
|
CheckBoxHeader::CheckBoxHeader(Qt::Orientation orientation, QWidget* parent)
|
|
: QHeaderView(orientation, parent)
|
|
, isChecked(false)
|
|
{
|
|
setSectionsClickable(true);
|
|
connect(this, &QHeaderView::sectionClicked, this, &CheckBoxHeader::handleSectionClicked);
|
|
}
|
|
|
|
void CheckBoxHeader::paintSection(QPainter* painter, const QRect& rect, int logicalIndex) const
|
|
{
|
|
painter->save();
|
|
|
|
if (logicalIndex == 0) {
|
|
const int checkboxSize = 15;
|
|
checkboxRect = rect;
|
|
|
|
// 计算复选框位置
|
|
int x = rect.x() + 5; // 从左边留出5像素的间距
|
|
int y = rect.y() + (rect.height() - checkboxSize) / 2;
|
|
|
|
// 绘制复选框
|
|
painter->setPen(Qt::black);
|
|
painter->drawRect(x, y, checkboxSize, checkboxSize);
|
|
|
|
if (isChecked) {
|
|
painter->drawLine(x + 3, y + 7, x + 6, y + 10);
|
|
painter->drawLine(x + 6, y + 10, x + 12, y + 4);
|
|
}
|
|
|
|
// 绘制文字
|
|
QRect textRect = rect.adjusted(checkboxSize + 10, 0, 0, 0);
|
|
painter->drawText(textRect, Qt::AlignVCenter, tr("选择"));
|
|
} else {
|
|
QHeaderView::paintSection(painter, rect, logicalIndex);
|
|
}
|
|
|
|
painter->restore();
|
|
}
|
|
|
|
void CheckBoxHeader::handleSectionClicked(int logicalIndex)
|
|
{
|
|
if (logicalIndex == 0 && !checkboxRect.isNull()) {
|
|
isChecked = !isChecked;
|
|
emit checkBoxClicked(isChecked);
|
|
updateSection(0);
|
|
}
|
|
}
|
|
|
|
MainWindow::MainWindow(QWidget* parent)
|
|
: QMainWindow(parent)
|
|
{
|
|
setWindowTitle(tr("表头复选框示例"));
|
|
resize(400, 300);
|
|
|
|
// 创建表格
|
|
table = new QTableWidget(5, 3, this);
|
|
setCentralWidget(table);
|
|
|
|
// 设置表头
|
|
auto header = new CheckBoxHeader(Qt::Horizontal, table);
|
|
table->setHorizontalHeader(header);
|
|
connect(header, &CheckBoxHeader::checkBoxClicked,
|
|
this, &MainWindow::onHeaderCheckBoxClicked);
|
|
|
|
// 设置表头标题
|
|
QStringList headers;
|
|
headers << tr("选择") << tr("列1") << tr("列2");
|
|
table->setHorizontalHeaderLabels(headers);
|
|
|
|
// 调整列宽
|
|
table->horizontalHeader()->setStretchLastSection(true);
|
|
table->horizontalHeader()->resizeSection(0, 80);
|
|
|
|
// 调整表格样式
|
|
table->setShowGrid(true);
|
|
table->setAlternatingRowColors(true);
|
|
|
|
// 填充表格数据
|
|
for (int row = 0; row < 5; ++row) {
|
|
// 添加复选框
|
|
auto checkboxItem = new QTableWidgetItem();
|
|
checkboxItem->setFlags(Qt::ItemIsUserCheckable | Qt::ItemIsEnabled);
|
|
checkboxItem->setCheckState(Qt::Unchecked);
|
|
table->setItem(row, 0, checkboxItem);
|
|
|
|
// 添加其他数据
|
|
table->setItem(row, 1, new QTableWidgetItem(
|
|
QString(tr("数据 %1-1")).arg(row + 1)));
|
|
table->setItem(row, 2, new QTableWidgetItem(
|
|
QString(tr("数据 %1-2")).arg(row + 1)));
|
|
}
|
|
}
|
|
|
|
void MainWindow::onHeaderCheckBoxClicked(bool checked)
|
|
{
|
|
for (int row = 0; row < table->rowCount(); ++row) {
|
|
if (QTableWidgetItem* item = table->item(row, 0)) {
|
|
item->setCheckState(checked ? Qt::Checked : Qt::Unchecked);
|
|
}
|
|
}
|
|
}
|
|
|
|
int main(int argc, char *argv[])
|
|
{
|
|
QApplication app(argc, argv);
|
|
|
|
// 设置编码
|
|
QTextCodec::setCodecForLocale(QTextCodec::codecForName("UTF-8"));
|
|
|
|
// 设置默认字体
|
|
QFont font("Microsoft YaHei"); // 使用微软雅黑字体
|
|
app.setFont(font);
|
|
|
|
MainWindow window;
|
|
window.show();
|
|
return app.exec();
|
|
}
|