gpt4 book ai didi

c++ - QTableWidget,居中cellWidgets

转载 作者:太空狗 更新时间:2023-10-29 21:34:27 24 4
gpt4 key购买 nike

有没有办法将 QCheckBox 作为 QTableWidget 的单元格小部件放置在单元格的中心,而不是左侧,而不需要额外的 QWidget 并将复选框添加到它的布局中?

最佳答案

使用setCellWidget 将QCheckBox 添加到表中:

QWidget *checkBoxWidget = new QWidget(); //create QWidget
QCheckBox *checkBox = new QCheckBox(); //create QCheckBox
QHBoxLayout *layoutCheckBox = new QHBoxLayout(checkBoxWidget); //create QHBoxLayout
layoutCheckBox->addWidget(checkBox); //add QCheckBox to layout
layoutCheckBox->setAlignment(Qt::AlignCenter); //set Alignment layout
layoutCheckBox->setContentsMargins(0,0,0,0);

ui->tableWidget->setCellWidget(0,0, checkBoxWidget);

screenshot

也可以使用这些行来调整内容的大小:

ui->tableWidget->resizeRowsToContents();
ui->tableWidget->resizeColumnsToContents();

setCellWidget: Sets the given widget to be displayed in the cell in the given row and column, passing the ownership of the widget to the table.

引用:https://evileg.com/en/post/79/

关于c++ - QTableWidget,居中cellWidgets,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46434577/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com