QStringList head; head << "ID" << "生产订单号" << "开胶工单号" << "物料编码" << "生产总重" << "版本号" << "绑定机台" << "操作员" << "开始时间" << "结束时间"; QScroller *pScroller = QScroller::scroller(ui->tWidgetOrder); pScroller->grabGesture(ui->tWidgetOrder,QScroller::LeftMouseButtonGesture); ui->tWidgetOrder->setVerticalScrollMode(QListWidget::ScrollPerPixel); ui->tWidgetOrder->setColumnCount(head.count()); ui->tWidgetOrder->setHorizontalHeaderLabels(head); ui->tWidgetOrder->horizontalHeader()->setAutoScroll(true); ui->tWidgetOrder->setEditTriggers(QAbstractItemView::NoEditTriggers); ui->tWidgetOrder->setSelectionMode(QAbstractItemView::SingleSelection); ui->tWidgetOrder->setSelectionBehavior(QAbstractItemView::SelectRows); ui->tWidgetOrder->setColumnHidden(0, true);点击行数获取到id的值
时间: 2023-06-14 09:06:19 浏览: 113
基于QT的串口助手程序
您可以在`QListWidget`的`itemSelectionChanged`信号中获取所选行的`QListWidgetItem`对象,然后通过`QListWidgetItem`的`data`方法获取该行的ID值。示例代码如下:
```cpp
connect(ui->tWidgetOrder, &QListWidget::itemSelectionChanged, this, [this]() {
QListWidgetItem *item = ui->tWidgetOrder->currentItem();
if (item) {
int id = item->data(Qt::UserRole).toInt();
qDebug() << "Selected ID:" << id;
}
});
```
在设置行数据时,您可以使用`QListWidgetItem`的`setData`方法将ID值设置为`Qt::UserRole`属性,示例代码如下:
```cpp
QListWidgetItem *item = new QListWidgetItem();
item->setData(Qt::UserRole, id);
//设置其他列的数据
ui->tWidgetOrder->addItem(item);
```
阅读全文