Qt - 在QTable中置一个复选框 [英] Qt - Centering a checkbox in a QTable

查看:144
本文介绍了Qt - 在QTable中置一个复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在QtCreater中,我向我的项目添加了一个表。在我的代码中,我生成一些数据输出到表中。我要在每一行中添加 QCheckbox 以允许选择该行。表格的所有内容都对齐左边,我如何使每行的第一列中的这些复选框与中心对齐?



我添加 QCheckbox 使用:

  ui-> data_table-> setCellWidget ,0,new QCheckBox); 


解决方案

我通常使用布局和容器小部件。这是一个丑陋的解决方案,但它的工作原理:

  QWidget * w = new QWidget 
QHBoxLayout * l = new QHBoxLayout();
1-> setAlignment(Qt :: AlignCenter);
l-> addWidget(<在此添加您的复选框>);
w-> setLayout(l);
ui-> data_table-> setCellWidget(rowCount,0,w);

所以基本上你会有:

 表单元格 - >小部件 - >布局 - >复选框

如果您需要通过表格访问复选框,则必须考虑。 / p>

In QtCreater I added a table to my project. in my code I am generating some data to output into the table. I want to add a QCheckbox into each row to allow the row to be selected. All the content of the table is aligned left, how do I make only these checkboxes in the first column of every row align to the center?

I'm adding the QCheckbox using:

ui->data_table->setCellWidget(rowCount,0, new QCheckBox);

解决方案

I usually use a layout and a container widget for this. It is an ugly solution, but it works:

QWidget * w = new QWidget();
QHBoxLayout *l = new QHBoxLayout();
l->setAlignment( Qt::AlignCenter );
l->addWidget( <add your checkbox here> );
w->setLayout( l );
ui->data_table->setCellWidget(rowCount,0, w);

So basically you will have:

Table Cell -> Widget -> Layout -> Checkbox

you'll have to consider it if you will need to access the checkbox through the table.

这篇关于Qt - 在QTable中置一个复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆