html表格加复选框,TableView添加复选框Checkbox,实现选择

具体思路:自定义Checkbox类,在表格数据类添加Checkbox属性,绑定表格checkbox列。

1.定义Checkbox类

public class Checkbox

{

CheckBox checkbox=new CheckBox();

public ObservableValue getCheckBox()

{

return new ObservableValue() {

@Override

public void addListener(ChangeListener super CheckBox> listener) {

}

@Override

public void removeListener(ChangeListener super CheckBox> listener) {

}

@Override

public CheckBox getValue() {

return checkbox;

}

@Override

public void addListener(InvalidationListener listener) {

}

@Override

public void removeListener(InvalidationListener listener) {

}

};

}

public Boolean isSelected()

{

return checkbox.isSelected();

}

}

2.表格数据模型TestDevice添加Checkbox属性

public class TestDevice

{

public Checkbox cb = new Checkbox();

}

3.声明表格checkbox列

@FXML

TableColumn actionColumn

绑定数据

actionColumn.setCellValueFactory(cellData ->cellData.getValue().cb.getCheckBox());

4.获得表格checkbox状态。只要获取TableView数据集合,遍历checkbox即可

public void check() {

ObservableList list = tableView.getItems();

for (TestDevice o : list )

{

if (o.cb.isSelected())

{

System.out.println(o.deviceName.get());

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 在Qt中使用QTableView添加框可以通过自定义委托实现。首先,我们需要创建一个自定义的委托类来设置单元格的编辑控件为框。 ```cpp class CheckBoxDelegate : public QItemDelegate { public: void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override { QStyleOptionButton checkboxStyle; checkboxStyle.state |= QStyle::State_Enabled | QStyle::State_Active; if (index.data(Qt::CheckStateRole).toBool()) checkboxStyle.state |= QStyle::State_On; else checkboxStyle.state |= QStyle::State_Off; checkboxStyle.rect = option.rect.adjusted(0, 0, -1, -1); QApplication::style()->drawControl(QStyle::CE_CheckBox, &checkboxStyle, painter); } QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const override { QCheckBox *editor = new QCheckBox(parent); editor->setAttribute(Qt::WA_TransparentForMouseEvents); editor->setChecked(index.data(Qt::CheckStateRole).toBool()); connect(editor, SIGNAL(clicked(bool)), this, SLOT(commitAndCloseEditor())); return editor; } void setEditorData(QWidget *editor, const QModelIndex &index) const override { QCheckBox *checkBox = static_cast<QCheckBox*>(editor); checkBox->setChecked(index.data(Qt::CheckStateRole).toBool()); } void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const override { QCheckBox *checkBox = static_cast<QCheckBox*>(editor); model->setData(index, checkBox->isChecked(), Qt::CheckStateRole); } void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const override { editor->setGeometry(option.rect); } private slots: void commitAndCloseEditor() { QCheckBox *editor = static_cast<QCheckBox*>(sender()); emit commitData(editor); emit closeEditor(editor); } }; ``` 接下来,在创建QTableView并设置model之后,我们可以为需要添加框的列设置自定义委托。 ```cpp QTableView tableView; QStandardItemModel model(10, 3); // 例如,创建一个10行3列的模型 tableView.setModel(&model); CheckBoxDelegate delegate; tableView.setItemDelegateForColumn(2, &delegate); // 为第3列设置自定义委托 ``` 通过这样的方式,我们可以在QTableView中的指定列上显示框,并实现相应的操作。 ### 回答2: Qt的TableView通过使用QCheckBox作为模型元素来添加框。可以通过实现自定义的委托类来启用这个功能。 首先,需要创建一个新的委托类,继承QItemDelegate。在这个委托类中,重写createEditor()和setEditorData()方法,并使用QCheckBox作为编辑器。 以下是一个示例: ```cpp #include <QtWidgets> class CheckBoxDelegate : public QItemDelegate { public: CheckBoxDelegate(QObject *parent = nullptr) : QItemDelegate(parent) { } QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const override { Q_UNUSED(option) Q_UNUSED(index) QCheckBox *editor = new QCheckBox(parent); return editor; } void setEditorData(QWidget *editor, const QModelIndex &index) const override { QCheckBox *checkBox = qobject_cast<QCheckBox*>(editor); if (checkBox) { bool checked = index.model()->data(index, Qt::DisplayRole).toBool(); checkBox->setChecked(checked); } } void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const override { QCheckBox *checkBox = qobject_cast<QCheckBox*>(editor); if (checkBox) { model->setData(index, checkBox->isChecked(), Qt::EditRole); } } }; ``` 接下来,在TableView中使用这个自定义委托类。例如: ```cpp int main(int argc, char *argv[]) { QApplication app(argc, argv); QStandardItemModel model(4, 2); QTableView tableView; CheckBoxDelegate checkBoxDelegate; tableView.setItemDelegateForColumn(0, &checkBoxDelegate); tableView.setModel(&model); tableView.show(); return app.exec(); } ``` 这样,TableView的第一列将显示框。您可以通过在模型中设置数据来更改框的状态,例如: ```cpp QStandardItem *item = new QStandardItem(); item->setCheckable(true); item->setData(Qt::Unchecked, Qt::CheckStateRole); model->setItem(row, 0, item); ``` 这样,当您运行应用程序时,您应该能够在TableView中看到具有框的行。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值