1、不能对表格内容进行修改
ui->tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);
设置某一列不可编辑
QTableWidgetItem *item1 = new QTableWidgetItem(“xxx”); //设置第1列不可编辑
item1->setFlags(item1->flags() & (~Qt::ItemIsEditable));
ui->tableWidget->setItem(i, 0, item1); //第0列不可编辑
2、设置表格为整行选择
ui->tableWidget->setSelectionBehavior(QAbstractItemView::SelectRows);
3、设置隐藏表头和列头
ui->tableWidget->verticalHeader()->setVisible(false); //列
ui->tableWidget->horizontalHeader()->setVisible(false); //行
隐藏列
ui->tableWidget->setColumnHidden(i, true); // i表示第几列
4、加入控件
ui->tableWidget->setShowGrid(false); //关闭网格
ui->tableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);//选中一行
5、列可扩展
ui->tableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch); // 列可扩展
6、单元格字体居中
ui->tableWidget->item(i,j)->setTextAlignment(Qt::AlignHCenter|Qt::AlignVCenter); // i代表行,j代表列
void Mymethod::setTableAligCenter(QTableWidget *tw)
{
int rowCount = tw->rowCount();
int columCount = tw->columnCount();
for(int row=0;row<rowCount;row++)
{
for(int colum=0;colum<columCount;colum++)
{
QTableWidgetItem* item = tw->item(row, colum);
if(item != nullptr)
{
item->setTextAlignment(Qt::AlignCenter);//感觉效率不高
}
}
}
}
7、获取行数和列数
ui->tableWidget->rowCount();
ui->tableWidget->columnCount();
8、右键使能,在给widget加菜单时,需要加入此行代码
ui->tableWidget->setContextMenuPolicy(Qt::CustomContextMenu);
9、给QTableWidget添加控件
ui->tableWidget->setCellWidget(i, j , widget); //其中i代表行号,j代表列号,widget代表控件
例1:
QPushButton *pBtn = new QPushButton("查看");
ui->tableWidget->setCellWidget(0,0,pBtn);
例2:
QWidget *widget = new QWidget;
widget->setMaximumSize(ui->tableWidget->width(),ui->tableWidget->height());
QPushButton *pBtn = new QPushButton("查看");
QPushButton *pBtn1 = new QPushButton("修改");
QPushButton *pBtn2 = new QPushButton("删除");
QHBoxLayout *hbox = new QHBoxLayout(widget);
hbox->addWidget(pBtn);
hbox->addStretch(10);
hbox->addWidget(pBtn1);
hbox->addStretch(10);
hbox->addWidget(pBtn2);
hbox->setMargin(0);
widget->setLayout(hbox);
ui->tableWidget->setCellWidget(0, 1, widget);
10、获取当前点击的控件在表的那个位置
//一个单元格内单个控件,以pushButton为例
QPushButton *btn = (QPushButton *)sender();
int x = btn ->mapToParent(QPoint(0,0)).x();
int y = btn ->mapToParent(QPoint(0,0)).y();
QModelIndex index = ui->tableWidget->indexAt(QPoint(x,y));
int row = index.row();
int column = index.column();
//一个单元格内多个控件
QPushButton *btn = (QPushButton *)sender();
QWidget *w = (QWidget *)btn->parent();
int x = w->mapToParent(QPoint(0,0)).x();
int y = w->mapToParent(QPoint(0,0)).y();
QModelIndex index = ui->tableWidget->indexAt(QPoint(x,y));
int row = index.row();
int column = index.column();
11、设置单元格内字体颜色
QTableWidgetItem *item = new QTableWidgetItem;
item->setText("哈哈");
item->setTextColor(QColor("red")); // 设置字体透明1
//item->setForeground(QBrush(QColor("red"))); // 设置字体透明2
ui->tableWidget->setItem(0,0,item); // 在第0行0列的地方插入颜色是红色的"哈哈"两字
12、 设置单元格的颜色
ui->tableWidget->setRowCount(5);
for(int i = 0; i< ui->tableWidget->rowCount(); i++){
QTableWidgetItem *item = new QTableWidgetItem;
item->setText("哈哈");
item->setBackground(QBrush(QColor("red"))); // 设置单元格颜色
ui->tableWidget->setItem(i,0,item);
}
13、设置tablewidget背景透明
QPalette pll = m_tableWidget->palette();
pll.setBrush(QPalette::Base,QBrush(QColor(255,255,255,0)));
m_tableWidget->setPalette(pll);
或者
ui->tableWidget->setStyleSheet("background-color: transparent;");
14、例:
.h文件
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QMenu>
#include <QAction>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = nullptr);
~MainWindow();
void initTableWidget();
private slots:
void MenuSlot(QAction *action);
void showMenuPos(QPoint);
private:
Ui::MainWindow *ui;
QMenu *menu;
QAction *addColumn, *reColumn, *addRow, *reRow;
};
#endif // MAINWINDOW_H
.cpp文件
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QString>
#include <QDebug>
#include <QPoint>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
initTableWidget();
QObject::connect(ui->tableWidget, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(showMenuPos(QPoint)));
QObject::connect(menu, SIGNAL(triggered(QAction*)), this, SLOT(MenuSlot(QAction*)));
}
MainWindow::~MainWindow()
{
delete ui;
}
/*****************************************************************/
//作者:
//函数名称:initTableWidget
//函数参数:NULL
//函数返回值:NULL
//函数作用:初始化表格
//备注:NULL
/*****************************************************************/
void MainWindow::initTableWidget()
{
ui->tableWidget->setRowCount(3);
ui->tableWidget->setColumnCount(2);
QStringList strList ;
strList << "时间" << "年限";
ui->tableWidget->setHorizontalHeaderLabels(strList);
ui->tableWidget->verticalHeader()->setVisible(false);
ui->tableWidget->setContextMenuPolicy(Qt::CustomContextMenu); // 右键使能
ui->tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);
ui->tableWidget->setItem(0,0,new QTableWidgetItem("张飞"));
ui->tableWidget->setItem(1,0,new QTableWidgetItem("关羽"));
ui->tableWidget->setItem(2,0,new QTableWidgetItem("吕布"));
ui->tableWidget->setItem(0,1,new QTableWidgetItem("35"));
ui->tableWidget->setItem(1,1,new QTableWidgetItem("36"));
ui->tableWidget->setItem(2,1,new QTableWidgetItem("32"));
menu = new QMenu(ui->tableWidget);
addColumn = new QAction(menu);
addColumn->setText("新增一列");
reColumn = new QAction(menu);
reColumn->setText("删除一列");
addRow = new QAction(menu);
addRow->setText("新增一行");
reRow = new QAction(menu);
reRow->setText("删除一行");
menu->addAction(addRow);
menu->addAction(addColumn);
menu->addAction(reRow);
menu->addAction(reColumn);
int column = ui->tableWidget->columnCount();
for(int i = 0; i<column;i++)
{
ui->comboBox->addItem(ui->tableWidget->horizontalHeaderItem(i)->text());
}
}
/*****************************************************************/
//作者:
//函数名称:MenuSlot
//函数参数:action
//函数返回值:NULL
//函数作用:根据行为判断当前所做的事
//备注:NULL
/*****************************************************************/
void MainWindow::MenuSlot(QAction *action)
{
int column = ui->tableWidget->columnCount();
int row = ui->tableWidget->rowCount();
if(action == addRow)
{
ui->tableWidget->insertRow(row);
for(int i = 0; i<row;i++)
{
ui->tableWidget->setItem(row,i,new QTableWidgetItem("张飞"));
}
}
else if(action == addColumn)
{
if(!ui->lineEdit->text().isEmpty())
{
ui->tableWidget->insertColumn(column);
ui->tableWidget->setHorizontalHeaderItem(column, new QTableWidgetItem(ui->lineEdit->text()));
ui->comboBox->addItem(ui->lineEdit->text());
}
else {
qDebug() << "添加失败!!!";
return ;
}
}
else if (action == reRow) {
ui->tableWidget->removeRow(ui->tableWidget->currentRow());
}
else if (action == reColumn) {
for(int i = 0; i<column;i++)
{
if(ui->tableWidget->horizontalHeaderItem(i)->text() == ui->comboBox->currentText())
{
ui->tableWidget->removeColumn(i);
ui->comboBox->removeItem(i);
}
}
}
}
/*****************************************************************/
//作者:
//函数名称:showMenuPos
//函数参数:NULL
//函数返回值:NULL
//函数作用:右键菜单显示位置
//备注:NULL
/*****************************************************************/
void MainWindow::showMenuPos(QPoint)
{
menu->exec(QCursor::pos());
}
.ui
运行结果:
原文链接:QT tableWidget的简单使用-Qt开发中文网
版权归原作者 Qt开发老杰 所有, 如有侵权,请联系我们删除。