QT表格列添加多个按钮

1. 示例代码:

void ParamSetting::add_operate_button(QTableWidget *table, int row)
{
    QPushButton* delBtn = new QPushButton;
     QPushButton* edtBtn = new QPushButton;
    QHBoxLayout *h_box_layout = new QHBoxLayout(); //添加水平布局控件
    MyProperty myProperty; //自定义类,后面有解释
    memset(&myProperty, 0, sizeof(MyProperty));
    myProperty.row = row;
    myProperty.table = table;
    QVariant v;
    v.setValue(myProperty);
    QWidget *widget = new QWidget(); //添加部件
    
    delBtn->setIcon(QIcon(":/res/button/delete.png"));
    delBtn->setToolTip("删除");
    delBtn->setProperty("sender", v);
    QObject::connect(delBtn, SIGNAL(clicked()), this, SLOT(slot_remove_row())); //设置删除动作槽函数
          
    edtBtn->setIcon(QIcon(":/res/button/edit.png"));
    edtBtn->setToolTip("修改");
    edtBtn->setProperty("sender", v);
    QObject::connect(edtBtn, SIGNAL(clicked()), this, SLOT(slot_edit_row())); 设置编辑动作槽函数
        
    h_box_layout->addWidget(edtBtn);
    h_box_layout->addWidget(delBtn);
    h_box_layout->setContentsMargins(0, 0, 0, 0);
    h_box_layout->setSpacing(1);
    widget->setLayout(h_box_layout);
    table->setCellWidget(row, table->columnCount()-1, widget); 
}

其中 MyProperty类定义如下:

struct MyProperty
{
    QTableWidget *table;
    int row;
};
Q_DECLARE_METATYPE(MyProperty)  //注意声明

这个一定要声明,这种方式传复杂参数的时候很好用

2. 效果图:

QT表格列添加多个按钮_第1张图片

你可能感兴趣的:(QT,table,表格按钮)