如何在QTreeView列中禁用复选框,但保持整行可选。

huangapple go评论107阅读模式
英文:

How to disable checkboxes in QTreeView columns but keep the entire row selectable

问题

I'm using a QTreeView and a custom model derived from QAbstractItemModel to display rows with 3 checkboxes per row.

The selection behaviour is set to select entire rows.
The checkboxes are added via returning a Qt::CheckState in the model's data()-function.

The model's flags()-function returns Qt::ItemIsUserCheckable for all model indices that have a checkbox. Depending on the state of the third checkbox, checkbox 1 and 2 shall be enabled or disabled.

  1. Qt::ItemFlags MyModel::flags(QModelIndex const& index) const {
  2. Qt::ItemFlags flags = Qt::ItemIsSelectable | Qt::ItemIsEnabled;
  3. if(index.column() > 0) {
  4. flags = flags | Qt::ItemIsUserCheckable;
  5. }
  6. if(thirdCheckboxChecked(index) && (index.column() == 1 || index.column() == 2) ) {
  7. flags = flags & ~Qt::ItemIsEnabled;
  8. }
  9. return flags;
  10. }

I have tried to disable the checkboxes via the model's flags()-function (by not returning Qt::ItemIsEnabled if the third checkbox is checked) but this will disable the entire item, disabling the selection for the entire item / cell resulting in this behaviour:

如何在QTreeView列中禁用复选框,但保持整行可选。

How can I access the checkboxes and disable them without disabling the entire items?

英文:

I'm using a QTreeView and a custom model derived from QAbstractItemModel to display rows with 3 checkboxes per row.

The selection behaviour is set to select entire rows.
The checkboxes are added via returning a Qt::CheckState in the model's data()-function.

The model's flags()-function returns Qt::ItemIsUserCheckable for all model indices that have a checkbox. Depending on the state of the third checkbox, checkbox 1 and 2 shall be enabled or disabled.

  1. Qt::ItemFlags MyModel::flags(QModelIndex const& index) const {
  2. Qt::ItemFlags flags = Qt::ItemIsSelectable | Qt::ItemIsEnabled;
  3. if(index.column() > 0) {
  4. flags = flags | Qt::ItemIsUserCheckable;
  5. }
  6. if(thirdCheckboxChecked(index) && (index.column() == 1 || index.column() == 2) ) {
  7. flags = flags & ~Qt::ItemIsEnabled;
  8. }
  9. return flags;
  10. }

I have tried to disable the checkboxes via the model's flags()-function (by not returning Qt::ItemIsEnabled if the third checkbox is checked) but this will disable the entire item, disabling the selection for the entire item / cell resulting in this behaviour:

如何在QTreeView列中禁用复选框,但保持整行可选。

How can I access the checkboxes and disable them without disabling the entire items?

答案1

得分: 0

以下是代码的翻译部分:

  1. 尝试使用代理?仅使复选框变灰,不使单元格变灰:
  2. #include <QtWidgets/QApplication>
  3. #include <qt_windows.h>
  4. #include <QTableView>
  5. #include <QAbstractTableModel>
  6. #include <QStyledItemDelegate>
  7. #include <QPainter>
  8. int checkstat[4] = { Qt::Checked,Qt::Checked,Qt::Unchecked,Qt::Unchecked };
  9. int checkenable[4] = { 1,1,0,0 };
  10. class delegate :public QStyledItemDelegate
  11. {
  12. Q_OBJECT
  13. public:
  14. virtual void paint(QPainter* painter, const QStyleOptionViewItem& option
  15. , const QModelIndex& index) const override
  16. {
  17. QStyledItemDelegate::paint(painter, option, index);
  18. if (index.column() == 1)
  19. {
  20. QStyle* style = QApplication::style();
  21. QStyleOptionButton butOpt;
  22. butOpt.rect.setX(option.rect.x() + option.rect.width() / 2);
  23. butOpt.rect.setY(option.rect.y());
  24. butOpt.rect.setWidth(option.rect.width() / 4);
  25. butOpt.rect.setHeight(option.rect.height());
  26. if (checkenable[index.row()]) butOpt.state = QStyle::State_Enabled;
  27. butOpt.state |= checkstat[index.row()] ? QStyle::State_On : QStyle::State_Off;
  28. style->drawControl(QStyle::CE_CheckBox, &butOpt, painter);
  29. }
  30. }
  31. };
  32. class tablemodel :public QAbstractTableModel
  33. {
  34. Q_OBJECT
  35. public:
  36. Q_INVOKABLE virtual int rowCount(const QModelIndex& parent = QModelIndex()) const
  37. {
  38. return 4;
  39. }
  40. Q_INVOKABLE virtual int columnCount(const QModelIndex& parent = QModelIndex()) const
  41. {
  42. return 2;
  43. }
  44. Q_INVOKABLE virtual QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const
  45. {
  46. if (index.column() == 0 && role == Qt::DisplayRole)
  47. {
  48. return QString::number(index.row()) + '-' + QString::number(index.column());
  49. }
  50. return QVariant();
  51. }
  52. Qt::ItemFlags flags(const QModelIndex& index) const override
  53. {
  54. return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
  55. }
  56. };
  57. int main(int argc, char *argv[])
  58. {
  59. QApplication a(argc, argv);
  60. QTableView view;
  61. tablemodel m;
  62. delegate d;
  63. view.setItemDelegate(&d);
  64. view.setModel(&m);
  65. view.show();
  66. //QtWidgetsApplication1 w;
  67. //w.show();
  68. return a.exec();
  69. }
  70. #include "main.moc"

请注意,我已将代码部分翻译成中文。

英文:

try using a delegate? gray out only checkbox, doesnot gray out cell:如何在QTreeView列中禁用复选框,但保持整行可选。

  1. #include &lt;QtWidgets/QApplication&gt;
  2. #include&lt;qt_windows.h&gt;
  3. #include &lt;QTableView&gt;
  4. #include &lt;QAbstractTableModel&gt;
  5. #include &lt;QStyledItemDelegate&gt;
  6. #include &lt;QPainter&gt;
  7. int checkstat[4] = { Qt::Checked,Qt::Checked,Qt::Unchecked,Qt::Unchecked };
  8. int checkenable[4] = { 1,1,0,0 };
  9. class delegate :public QStyledItemDelegate
  10. {
  11. Q_OBJECT
  12. public:
  13. virtual void paint(QPainter* painter, const QStyleOptionViewItem&amp; option
  14. , const QModelIndex&amp; index) const override
  15. {
  16. QStyledItemDelegate::paint(painter, option, index);
  17. if (index.column() == 1)
  18. {
  19. QStyle* style = QApplication::style();
  20. QStyleOptionButton butOpt;
  21. butOpt.rect.setX(option.rect.x()+ option.rect.width()/2);
  22. butOpt.rect.setY(option.rect.y());
  23. butOpt.rect.setWidth(option.rect.width()/4);
  24. butOpt.rect.setHeight(option.rect.height());
  25. if(checkenable[index.row()])butOpt.state = QStyle::State_Enabled;
  26. butOpt.state |= checkstat[index.row()] ? QStyle::State_On : QStyle::State_Off;
  27. style-&gt;drawControl(QStyle::CE_CheckBox, &amp;butOpt, painter);
  28. }
  29. }
  30. };
  31. class tablemodel :public QAbstractTableModel
  32. {
  33. Q_OBJECT
  34. public:
  35. Q_INVOKABLE virtual int rowCount(const QModelIndex&amp; parent = QModelIndex()) const
  36. {
  37. return 4;
  38. }
  39. Q_INVOKABLE virtual int columnCount(const QModelIndex&amp; parent = QModelIndex()) const
  40. {
  41. return 2;
  42. }
  43. Q_INVOKABLE virtual QVariant data(const QModelIndex&amp; index, int role = Qt::DisplayRole) const
  44. {
  45. if (index.column() == 0&amp;&amp; role==Qt::DisplayRole)
  46. {
  47. return QString::number(index.row()) + &#39;-&#39; + QString::number(index.column());
  48. }
  49. return QVariant();
  50. }
  51. Qt::ItemFlags flags(const QModelIndex&amp; index) const override
  52. {
  53. return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
  54. }
  55. };
  56. int main(int argc, char *argv[])
  57. {
  58. QApplication a(argc, argv);
  59. QTableView view;
  60. tablemodel m;
  61. delegate d;
  62. view.setItemDelegate(&amp;d);
  63. view.setModel(&amp;m);
  64. view.show();
  65. //QtWidgetsApplication1 w;
  66. //w.show();
  67. return a.exec();
  68. }
  69. #include&quot;main.moc&quot;

huangapple
  • 本文由 发表于 2023年5月22日 18:22:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/76305195.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定