QTableWidget的基本使用

void init()
{

 setRowCount(1);//设置一行
 setColumnCount(2);  //设置两列

 int row = rowCount();
 removeRow(row);     //清除已有的行列
 setShowGrid(true);  //显示表格线
 verticalHeader()->setVisible(false);          //隐藏左边垂直
 QHeaderView *headerView = horizontalHeader();
 headerView->setMovable(false);                //去除表头的移动
 headerView->resizeSection(0,284);//设置第一列宽
 headerView->resizeSection(1,127);//设置第二列宽
 headerView->setResizeMode(QHeaderView::Fixed);//列表不能移动
 headerView->setClickable(false);//不响应鼠标单击
 setEditTriggers(QTableWidget::NoEditTriggers); //不能编辑
 setSelectionBehavior(QTableWidget::SelectRows);//一次选中一行
 setSelectionMode(QAbstractItemView::SingleSelection);//只能单选
 /*QScrollBar *scrollBar = horizontalScrollBar();
 scrollBar->hide();*/
 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);   //去掉水平滚动条
 setVerticalScrollMode(QAbstractItemView::ScrollPerItem);//垂直滚动条按项移动
 setAutoScroll(false);                    //去掉自动滚动


 QStringList list1;
 list1.append(tr("1"));
 list1.append(tr("2"));
 setHorizontalHeaderLabels(list1);//设置表头内容

QHeaderView* headerView = this->verticalHeader();
headerView->setHidden(true);            //去掉自带行号
}

二.添加指定项

void CTableWidget::addItem(QString text)
{
 QTableWidgetItem *operateItem = new QTableWidgetItem(text);
 int row = rowCount();
 QDateTime dataTime = QDateTime::currentDateTime();
 QTableWidgetItem *timeItem = new QTableWidgetItem(dataTime.toString());
 insertRow(row);
 setItem(row, 0, operateItem);
 setItem(row, 1, timeItem);
}

三.删除

void CTableWidget::clearSlot()
{
 int count = rowCount();
 int index = count - 1;
 for (int i=0; i<count; i++)
 {
  removeRow(index);
  index--;
 }

 

四,设置列宽 自动填满框体大小

  table->horizontalHeader()->setStretchLastSection(true);  //就是这个地方

 根据内容自动调整列宽。

根据内容自动调整某列的列宽
void QTableView::resizeColumnToContents ( int column ) [slot]

Resizes the given column based on the size hints of the delegate used to render each item in the column.


根据内容自动调整所有列的列宽
void QTableView::resizeColumnsToContents() [slot]

Resizes all columns based on the size hints of the delegate used to render each item in the columns.

根据内容自动调整某一行的行高
void QTableView::resizeRowToContents ( int row ) [slot]

Resizes the given row based on the size hints of the delegate used to render each item in the row.

根据内容自动调整所有行的行高。
void QTableView::resizeRowsToContents() [slot]

Resizes all rows based on the size hints of the delegate used to render each item in the rows.



}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值