Qt 6.11 is out! See what's new in the release
blog
Remove the textfield near the checkable QTableView cells
-
There is a sample of my code :
QStandardItemModel *model = new QStandardItemModel(3, 3, this); model->setHeaderData(0, Qt::Horizontal, tr("Select")); model->setHeaderData(1, Qt::Horizontal, tr("Column 1")); model->setHeaderData(2, Qt::Horizontal, tr("Column 2")); for (int row = 0; row < model->rowCount(); row++) { QStandardItem *checkItem = new QStandardItem(); checkItem->setCheckable(true); checkItem->setEditable(false); checkItem->setCheckState(Qt::Unchecked); model->setItem(row, 0, checkItem); model->setData(model->index(row, 1), tr("Row %1 Column 1").arg(row+1)); model->setData(model->index(row, 2), tr("Row %1 Column 2").arg(row+1)); } ui->tableView->setModel(model); ui->tableView->setColumnWidth(0, 20);You have to use a custom QStyledItemDelegate: https://wiki.qt.io/Center_a_QCheckBox_or_Decoration_in_an_Itemview
-
Hi,
How did you put your checkbox there ?
-
There is a sample of my code :
QStandardItemModel *model = new QStandardItemModel(3, 3, this); model->setHeaderData(0, Qt::Horizontal, tr("Select")); model->setHeaderData(1, Qt::Horizontal, tr("Column 1")); model->setHeaderData(2, Qt::Horizontal, tr("Column 2")); for (int row = 0; row < model->rowCount(); row++) { QStandardItem *checkItem = new QStandardItem(); checkItem->setCheckable(true); checkItem->setEditable(false); checkItem->setCheckState(Qt::Unchecked); model->setItem(row, 0, checkItem); model->setData(model->index(row, 1), tr("Row %1 Column 1").arg(row+1)); model->setData(model->index(row, 2), tr("Row %1 Column 2").arg(row+1)); } ui->tableView->setModel(model); ui->tableView->setColumnWidth(0, 20); -
There is a sample of my code :
QStandardItemModel *model = new QStandardItemModel(3, 3, this); model->setHeaderData(0, Qt::Horizontal, tr("Select")); model->setHeaderData(1, Qt::Horizontal, tr("Column 1")); model->setHeaderData(2, Qt::Horizontal, tr("Column 2")); for (int row = 0; row < model->rowCount(); row++) { QStandardItem *checkItem = new QStandardItem(); checkItem->setCheckable(true); checkItem->setEditable(false); checkItem->setCheckState(Qt::Unchecked); model->setItem(row, 0, checkItem); model->setData(model->index(row, 1), tr("Row %1 Column 1").arg(row+1)); model->setData(model->index(row, 2), tr("Row %1 Column 2").arg(row+1)); } ui->tableView->setModel(model); ui->tableView->setColumnWidth(0, 20);You have to use a custom QStyledItemDelegate: https://wiki.qt.io/Center_a_QCheckBox_or_Decoration_in_an_Itemview
-
You have to use a custom QStyledItemDelegate: https://wiki.qt.io/Center_a_QCheckBox_or_Decoration_in_an_Itemview
It's working.
But, I have 2 questions:
1- I'm not used to work with delegate. So Is is a good solution?
2- How can I minimize the column width to fit the checkbox?
-
L lmofallis has marked this topic as solved on
