C++ Qt-在QTable中居中复选框

C++ Qt-在QTable中居中复选框,c++,qt,qt-creator,qtablewidgetitem,qcheckbox,C++,Qt,Qt Creator,Qtablewidgetitem,Qcheckbox,在QtCreater中,我向我的项目添加了一个表。在我的代码中,我生成一些数据输出到表中。我想在每一行中添加一个QCheckbox,以允许选择该行。表格的所有内容都是左对齐的,如何使每行第一列中的复选框仅与中心对齐 我使用以下方法添加QCheckbox: ui->data_table->setCellWidget(rowCount,0, new QCheckBox); 为此,我通常使用布局和容器小部件。这是一个丑陋的解决方案,但它是有效的: QWidget * w = new QW

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

我使用以下方法添加
QCheckbox

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

为此,我通常使用布局和容器小部件。这是一个丑陋的解决方案,但它是有效的:

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);

<>你必须考虑它,如果你需要通过表访问复选框。

,我通常为此使用布局和容器小部件。这是一个丑陋的解决方案,但它是有效的:

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);

如果您需要通过表访问复选框,就必须考虑它。

< p>它对我起作用,但我的复选框没有完全显示。

要获得小部件的完整视图,请删除布局中的边距:


l->setContentsMargins(0,0,0,0)

它对我有效,但我的复选框没有完全显示出来

要获得小部件的完整视图,请删除布局中的边距:


l->setContentsMargins(0,0,0,0)

正如在关于堆栈溢出的类似问题中所述,它目前是一个开放的BUG:


正如在关于堆栈溢出的类似问题中所述,它目前是一个开放的BUG:


这是一篇老文章,但实际上有一种更简单、更轻松的方法来实现这一点,只需将
QCheckBox
子类化,并将
样式表设置为

margin-left:50%;
margin-right:50%;

这是一篇老文章,但实际上有一种更简单、更轻松的方法来实现这一点,只需将
QCheckBox
子类化,并将
样式表设置为

margin-left:50%;
margin-right:50%;

为巴里·马文竖起两个大拇指!你甚至不需要子类

一行

pCheckBox->setStyleSheet("margin-left:50%; margin-right:50%;");

完成

为巴里·马文竖起两个大拇指!你甚至不需要子类

一行

pCheckBox->setStyleSheet("margin-left:50%; margin-right:50%;");
完成

#如果QT_版本<0x046000
#包括
类MyStyle:publicqcommonstyle{
公众:
QRect SubElement rect(SubElement SubElement,const QStyleOption*option,const QWidget*widget=0)const{
开关(子元素){
案例QStyle::SE_复选框指示器:{
QRect r=QCommonStyle::subElementRect(子元素、选项、小部件);
r、 setRect((widget->width()-r.width())/2,r.top(),r.width(),r.height());
返回QRect(r);
}
默认值:返回QCommonStyle::subElementRect(子元素、选项、小部件);
}
}
};
#否则
#包括
#包括
类MyStyle:公共QProxyStyle{
公众:
MyStyle():QProxyStyle(QStyleFactory::create(“Fusion”){}
QRect SubElement rect(SubElement SubElement,const QStyleOption*option,const QWidget*widget=0)const{
开关(子元素){
案例QStyle::SE_复选框指示器:{
QRect r=QProxyStyle::subElementRect(子元素、选项、小部件);
r、 setRect((widget->width()-r.width())/2,r.top(),r.width(),r.height());
返回QRect(r);
}
默认值:返回QProxyStyle::subElementRect(子元素、选项、小部件);
}
}
};
#恩迪夫
QCheckBox*box=新的QCheckBox();
box->setStyle(新MyStyle());
#如果QT_版本<0x046000
#包括
类MyStyle:publicqcommonstyle{
公众:
QRect SubElement rect(SubElement SubElement,const QStyleOption*option,const QWidget*widget=0)const{
开关(子元素){
案例QStyle::SE_复选框指示器:{
QRect r=QCommonStyle::subElementRect(子元素、选项、小部件);
r、 setRect((widget->width()-r.width())/2,r.top(),r.width(),r.height());
返回QRect(r);
}
默认值:返回QCommonStyle::subElementRect(子元素、选项、小部件);
}
}
};
#否则
#包括
#包括
类MyStyle:公共QProxyStyle{
公众:
MyStyle():QProxyStyle(QStyleFactory::create(“Fusion”){}
QRect SubElement rect(SubElement SubElement,const QStyleOption*option,const QWidget*widget=0)const{
开关(子元素){
案例QStyle::SE_复选框指示器:{
QRect r=QProxyStyle::subElementRect(子元素、选项、小部件);
r、 setRect((widget->width()-r.width())/2,r.top(),r.width(),r.height());
返回QRect(r);
}
默认值:返回QProxyStyle::subElementRect(子元素、选项、小部件);
}
}
};
#恩迪夫
QCheckBox*box=新的QCheckBox();
box->setStyle(新MyStyle());

如果要添加更多自定义项,也可以使用布局将中心设置为这样

// Create a widget that will contain a checkbox
 QWidget *checkBoxWidget = new QWidget();
 QCheckBox *checkBox = new QCheckBox();      // We declare and initialize the checkbox
 QHBoxLayout *layoutCheckBox = new QHBoxLayout(checkBoxWidget); // create a layer with reference to the widget
 layoutCheckBox->addWidget(checkBox);            // Set the checkbox in the layer
 layoutCheckBox->setAlignment(Qt::AlignCenter);  // Center the checkbox
 layoutCheckBox->setContentsMargins(0,0,0,0);    // Set the zero padding

ui->my_table_view->setCellWidget(row_number,column_number, checkBoxWidget);  // set cell widget
或者简单地添加左右边距

checkBox->setStyleSheet("margin-left:50%; margin-right:50%;");

如果要添加更多自定义项,也可以使用布局将中心设置为这样

// Create a widget that will contain a checkbox
 QWidget *checkBoxWidget = new QWidget();
 QCheckBox *checkBox = new QCheckBox();      // We declare and initialize the checkbox
 QHBoxLayout *layoutCheckBox = new QHBoxLayout(checkBoxWidget); // create a layer with reference to the widget
 layoutCheckBox->addWidget(checkBox);            // Set the checkbox in the layer
 layoutCheckBox->setAlignment(Qt::AlignCenter);  // Center the checkbox
 layoutCheckBox->setContentsMargins(0,0,0,0);    // Set the zero padding

ui->my_table_view->setCellWidget(row_number,column_number, checkBoxWidget);  // set cell widget
或者简单地添加左右边距

checkBox->setStyleSheet("margin-left:50%; margin-right:50%;");

这似乎比那容易多了。我花了很长一段时间浏览文档,试图找到一个函数或满足我需要的东西。但是你发布的内容非常有效,不过我自己永远也想不到。谢谢@首先,如何获取复选框的checkstatus?看起来应该比这简单得多。我花了很长一段时间浏览文档,试图找到一个函数或满足我需要的东西。但是你发布的内容非常有效,不过我自己永远也想不到。谢谢@SingerOfTheFall,如何获取复选框的checkstatus?在PyQt4中为Im,这仅在设置初始值时有效。也就是说,如果我调整列的大小,它不会继续使复选框居中,它只会在第一次创建时居中。在PyQt4中使用Im,这仅在设置初始值时有效。也就是说,如果我调整列的大小,它不会继续使复选框居中,它只会在第一次创建时居中;保证金权利:50%使我在列标题方面偏离中心。任何想法。我最终使用了第一种方法,它在没有偏移的情况下工作正常。出于某种原因
左边距:50%;保证金权利:50%使我在列标题方面偏离中心。有什么想法吗?我最后用了第一个m