通过界面拖拽,创建ui->tableWidget
设置样式表
QTableWidget{background-color:white; border:none;}
QTableView::item {border-bottom: 1px solid #d6d9dc;}
QTableWidget::item:hover{background-color: rgb(151, 226, 226);}
QTableWidget::item:selected{background-color:#E6E6FA}
QHeaderView::section, QTableCornerButton:section{
padding:3px;
margin:0px;
color:#000000; /*字体颜色*/
border:1px solid #b3beb9;
border-left-width:0px;
border-right-width:1px;
border-top-width:0px;
border-bottom-width:1px;
/*设置表头真正背景色*/
background-color: rgb(195, 195, 195);
}
/*设置表头样式*/
QHeaderView {
color: rgb(0, 103, 0);
font: bold 10pt;
background-color: rgb(195, 195, 195);
border: 0px solid rgb(220, 220, 220);
border-left-color: rgba(255, 255, 255, 0);
border-top-color: rgba(255, 255, 255, 0);
border-radius:0px;
min-height:29px;
}
初始化 ui->tableWidget
void Managerform::init()
{
//ItemWaitSendForm *win = new ItemWaitSendForm();
//QModelIndex index = ui->tableWidget->model()->index(0,0);
//ui->tableWidget->setIndexWidget(index, win);
ui->tableWidget->setRowCount(0);
ui->tableWidget->setColumnCount(7);
QStringList header;
header<<QStringLiteral("用户")<<QStringLiteral("收件人")<<QStringLiteral("地址")
<<QStringLiteral("产品")<<QStringLiteral("实付")<<QStringLiteral("快递单号")<<QStringLiteral("操作");
//设置表头标签
ui->tableWidget->setHorizontalHeaderLabels(header);
QFontMetrics fmenu(ui->tableWidget->font());
int fontwidth = fmenu.width(tr("中"));
ui->tableWidget->setFocusPolicy(Qt::NoFocus); //去掉单元格虚线
ui->tableWidget->horizontalHeader()->setMinimumSectionSize(3*fontwidth); //设置最小列宽
ui->tableWidget->verticalHeader()->setDefaultSectionSize(3.5*fontwidth); //默认行高
ui->tableWidget->setSelectionBehavior(QAbstractItemView::SelectRows);//将表格设置为整行选择
ui->tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers); //表格设置为禁止编辑
ui->tableWidget->resizeRowsToContents(); //行高 根据内容自动设置
ui->tableWidget->resizeColumnsToContents(); //列宽 根据内容自动设置
ui->tableWidget->setShowGrid(false); //不显示网格线
ui->tableWidget->setWordWrap(true); //允许换行
ui->tableWidget->setColumnWidth(0, 6*fontwidth);
ui->tableWidget->setColumnWidth(1, 5*fontwidth);
ui->tableWidget->setColumnWidth(4, 5*fontwidth);
ui->tableWidget->setColumnWidth(5, 8*fontwidth);
ui->tableWidget->setColumnWidth(6, 7*fontwidth);
ui->tableWidget->horizontalHeader()->setSectionResizeMode(2, QHeaderView::Stretch);//剩余列宽自动填充第2列
ui->tableWidget->horizontalHeader()->setSectionResizeMode(3, QHeaderView::Stretch);//剩余列宽自动填充第3列
//ui->tableWidget->horizontalHeader()->setStretchLastSection(true); //表格自动填充满
ui->tableWidget->setFrameShape(QFrame::NoFrame); //设置边框
ui->tableWidget->verticalHeader()->setVisible(false);//设置垂直头不可见
//ui->tableWidget->horizontalHeader()->setVisible(false); //设置水平头不可见
//ui->tableWidget->setSpan(0, 0, 1, 7); //合并单元格
// 对表头文字的字体、颜色进行设置
// 获得水平方向表头的item对象
// QTableWidgetItem *columnHeaderItem = ui->tableWidget->horizontalHeaderItem(1);
// columnHeaderItem->setFont(QFont("Helvetica")); //设置字体
// columnHeaderItem->setBackgroundColor(QColor(0,60,10)); //设置单元格背景颜色
// columnHeaderItem->setTextColor(QColor(200,111,30)); //设置文字颜色
//设置交替行颜色选项(默认是false)
ui->tableWidget->setAlternatingRowColors(true); //否则无法交替显示颜色
ui->tableWidget->setPalette(QPalette(Qt::lightGray)); //设置隔行变色的颜色 Qt::gray灰色
}
每行添加元素
void Managerform::onAddItemToWaitSend(TBOrderInfo data)
{
int row = ui->tableWidget->rowCount();
ui->tableWidget->insertRow(row);
QModelIndex index = ui->tableWidget->model()->index(row,0);
CheckItemForm *widget = new CheckItemForm(ui->tableWidget);
widget->setConnect(this);
widget->setText(QStringLiteral("yz多喝水"));
ui->tableWidget->setCellWidget(row, 0, widget);
QTableWidgetItem* pName = new QTableWidgetItem(QStringLiteral("蔡牛牛"));
pName->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
//pName->setIcon(QIcon(":/image/wang.png"));
ui->tableWidget->setItem(row, 1, pName);
QTableWidgetItem* pAddress = new QTableWidgetItem(QStringLiteral("1398****984,广西壮族自治区 梧州市 藤县 和平镇 *****门 ,000000"));
pAddress->setTextAlignment( Qt::AlignLeft | Qt::AlignVCenter);
ui->tableWidget->setItem(row, 2, pAddress);
QTableWidgetItem* pProduct = new QTableWidgetItem(QStringLiteral("5瓶装【洗涤剂】 * 2\n5瓶装【洗涤剂】 * 2\n5瓶装【洗涤剂】 * 2"));
pProduct->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
ui->tableWidget->setItem(row, 3, pProduct);
QTableWidgetItem* pActualFee = new QTableWidgetItem(QStringLiteral("176.00元"));
pActualFee->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
pActualFee->setBackgroundColor(QColor(0,60,10));//1、单元格设置字体颜色、背景颜色、字体字符
pActualFee->setTextColor(QColor(200,111,100)); //1、单元格设置字体颜色、背景颜色、字体字符
pActualFee->setFont(QFont("Helvetica")); //1、单元格设置字体颜色、背景颜色、字体字符
ui->tableWidget->setItem(row, 4, pActualFee);
QTableWidgetItem* pMailNo = new QTableWidgetItem(data.logistic.mailNo);
pMailNo->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
ui->tableWidget->setItem(row, 5, pMailNo);
ItemWaitSendForm *operate = new ItemWaitSendForm();
operate->setConnect(this);
//operate->setData(data);
ui->tableWidget->setCellWidget(row, 6, operate);
//对所有单元格设置字体
//ui->tableWidget->setFont(QFont("Courier"));
ui->tableWidget->setFont(QFont("Helvetica"));
}
选中某行,这里处理方法比较特别,值得借鉴
void Managerform::onCheckedItem(const bool flag)
{
//获取当前是哪个object发送的新号,
CheckItemForm *senderObj = qobject_cast<CheckItemForm*>(sender());
QModelIndex idx = ui->tableWidget->indexAt(QPoint(senderObj->frameGeometry().x(), senderObj->frameGeometry().y()));
bool enable = senderObj->isChecked();
int row = idx.row();
if(enable)
qDebug()<<QStringLiteral("第%1行 选中").arg(row+1);
else
qDebug()<<QStringLiteral("第%1行 取消选中").arg(row+1);
}