Qt-使QTable中的复选框居中
Qt - Centering a checkbox in a QTable
在QtCreater中,我为我的项目添加了一个表。在我的代码中,我正在生成一些要输出到表中的数据。我想在每行中添加一个QCheckbox
,以允许选择该行。表中的所有内容都向左对齐,如何使每行第一列中的这些复选框与中心对齐?
我正在使用添加QCheckbox
ui->data_table->setCellWidget(rowCount,0, new QCheckBox);
为巴里·马文竖起大拇指!你甚至不需要划分子类。
一行。。。
pCheckBox->setStyleSheet("margin-left:50%; margin-right:50%;");
完成!!
我通常使用布局和容器小部件。这是一个丑陋的解决方案,但它有效:
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);
所以基本上你会有:
Table Cell -> Widget -> Layout -> Checkbox
如果您需要通过表访问复选框,则必须考虑它。
这是一篇老文章,但实际上有一种更简单、更轻松的方法可以实现这一点,只需将QCheckBox
子类化并将stylesheet
设置为
margin-left:50%;
margin-right:50%;
它对我有效,但我的复选框没有完全显示。
要获得小部件的完整视图,请删除布局中的边距:
l->setContentsMargins(0,0,0,0);
如Stack Overflow的类似问题所述,它目前是一个开放的BUG:
https://bugreports.qt-project.org/browse/QTBUG-5368
如果想添加更多自定义,也可以使用布局来居中
// 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%;");
#if QT_VERSION < 0x046000
#include <QCommonStyle>
class MyStyle : public QCommonStyle {
public:
QRect subElementRect(SubElement subElement, const QStyleOption *option, const QWidget *widget = 0) const {
switch(subElement) {
case QStyle::SE_CheckBoxIndicator: {
QRect r = QCommonStyle::subElementRect(subElement, option, widget);
r.setRect( (widget->width() - r.width())/2, r.top(), r.width(), r.height());
return QRect(r);
}
default: return QCommonStyle::subElementRect(subElement, option, widget);
}
}
};
#else
#include <QProxyStyle>
#include <QStyleFactory>
class MyStyle: public QProxyStyle {
public:
MyStyle():QProxyStyle(QStyleFactory::create("Fusion")) {}
QRect subElementRect(SubElement subElement, const QStyleOption *option, const QWidget *widget = 0) const {
switch(subElement) {
case QStyle::SE_CheckBoxIndicator: {
QRect r = QProxyStyle::subElementRect(subElement, option, widget);
r.setRect( (widget->width() - r.width())/2, r.top(), r.width(), r.height());
return QRect(r);
}
default: return QProxyStyle::subElementRect(subElement, option, widget);
}
}
};
#endif
QCheckBox *box = new QCheckBox();
box->setStyle(new MyStyle());
对于规则的数据行,您可以调整模型,测试数据方法中的角色。这似乎对我有效:
if(role == Qt::TextAlignmentRole)
{
return(Qt::AlignHCenter); // or AlignCenter - either one works I beleive
}
然而,这并不能为标头解决——因此,如果您有一个带有复选框的标头(如全选),则无法解决。但这至少解决了对齐的数据问题。
相关文章:
- 根据变量使Qt UI中的复选框为已选中/未选中
- "动态创建的事件处理程序"复选框
- 如何删除Qt设计器中自定义插件的QString属性的"translatable"复选框?
- 调整QT创建者的复选框
- QT:选中复选框时忽略关键事件
- 如何从 QT 中的.cpp访问 QML 中的一个复选框
- 在使用Qt修改任何复选框状态时执行操作
- 从 QT 复选框到 Postgresql 选择星期几
- Qt复选框勾选所有按钮
- QTableView 中的 Qt 复选框
- Qt 中的多重选择和非多重选择复选框
- Qt捕获复选框的信号以执行操作
- Qt QStandardItem setCheckable(true) 不保存复选框状态
- Qt c++ QToolBox:如何添加复选框/按钮标题
- QListView或QListWidget中的Qt c++字体列表,并将它们表示为复选框
- Qt委托设置鼠标悬停状态的复选框
- Qt-访问表中的复选框
- Qt-使QTable中的复选框居中
- Qt QTableView实现切换按钮和复选框委托
- Qt复选框/工具按钮,带有自定义/不同的检查/未选中的图标