#include "devproppage.h" #include "ui_devproppage.h" #include #include #include #include #include DevPropPage::DevPropPage(QWidget *parent) : QWidget(parent), ui(new Ui::DevPropPage) { ui->setupUi(this); InitializeUI(); } DevPropPage::~DevPropPage() { delete ui; } void DevPropPage::InitializeUI() { tableView =new QTableView(this); button = new QPushButton("Click Me", this); // 主布局 QVBoxLayout *mainLayout = new QVBoxLayout(this); // 表格视图 SimpleTableModel* model = new SimpleTableModel(this); //model->setHorizontalHeaderLabels({"Column 1", "Column 2", "Column 3", "Column 4", "Column 5", "Column 6"}); tableView->setModel(model); // 设置QTableView的列宽度根据总宽度平均分配 QHeaderView *header = tableView->horizontalHeader(); header->setSectionResizeMode(QHeaderView::Stretch); // 自动调整列宽 //connect(tableView->horizontalScrollBar(), &QScrollBar::valueChanged, this, &DevPropPage::adjustColumnWidths); // 添加到主布局 mainLayout->addWidget(tableView); // 按钮容器布局 QWidget *buttonContainer = new QWidget(this); QHBoxLayout *buttonLayout = new QHBoxLayout(buttonContainer); buttonLayout->addStretch(); // 使按钮保持在右侧 buttonLayout->addWidget(button); buttonLayout->setContentsMargins(0, 0, 0, 0); // 取消按钮的边距 mainLayout->addWidget(buttonContainer); // 设置按钮的固定大小 button->setFixedSize(100, 30); // 连接信号和槽 connect(tableView, &QTableView::doubleClicked, this, &DevPropPage::onTableViewDoubleClicked); connect(button, &QPushButton::clicked, this, &DevPropPage::onButtonClicked); // 设置主布局的边距和间距 mainLayout->setContentsMargins(0, 0, 0, 0); mainLayout->setSpacing(0); setLayout(mainLayout); } void DevPropPage::onTableViewDoubleClicked(const QModelIndex &index) { // 处理双击事件 qDebug() << "Cell double-clicked at row:" << index.row() << "column:" << index.column(); } void DevPropPage::onButtonClicked() { // 处理按钮点击事件 qDebug() << "Button clicked!"; } void DevPropPage::adjustColumnWidths() { int columnCount = tableView->model()->columnCount(); int totalWidth = tableView->viewport()->width(); int columnWidth = totalWidth / columnCount; for (int column = 0; column < columnCount; ++column) { tableView->setColumnWidth(column, columnWidth); } }