//设置列数
ui->tableWidget->setColumnCount(4);
//设置行数
ui->tableWidget->setRowCount(5);
//去除选中虚线框
ui->tableWidget->setFocusPolicy(Qt::NoFocus);
//表头标题用QStringList来表示
QStringList headerText;
headerText<<"姓 名"<<"性 别"<<"出生日期"<<"国 籍";
ui->tableWidget->setHorizontalHeaderLabels(headerText);
//设置列宽
ui->tableWidget->setColumnWidth(0,200);
ui->tableWidget->setColumnWidth(1,200);
ui->tableWidget->setColumnWidth(2,200);
ui->tableWidget->setColumnWidth(3,200);
//需要打开右键菜单属性,则必须设置
ui->tableWidget->setContextMenuPolicy(Qt::CustomContextMenu);
//设置最后一栏自适应长度
ui->tableWidget->horizontalHeader()->setStretchLastSection(true);
//设置列内容自适应宽度
//ui->tableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);
//设置为可以选中多个目标,按ctrl键
ui->tableWidget->setSelectionMode(QAbstractItemView::ExtendedSelection);
//开启交替行背景色,在设置style为交替颜色时必须开启
ui->tableWidget->setAlternatingRowColors(true);
//清除表格数据区的所有内容,但是不清除表头。
// ui->tableWidget->clearContents();
//清除表格数据区的所有内容,包括表头。
// //ui->tableWidget->clear();
自动调整行高和列宽
QTableWidget 有几个函数自动调整表格的行高和列宽,分别如下:
设置表格内容是否可编辑
设置行表头、列表头是否显示
选择模式
这里添加三个string,和一个自定义的combobox
QComboBox *combox1 = new QComboBox(ui->tableWidget);
combox1->addItem("man");
combox1->addItem("woman");
QComboBox *combox2 = new QComboBox(ui->tableWidget);
combox2->addItem("man");
combox2->addItem("woman");
QComboBox *combox3 = new QComboBox(ui->tableWidget);
combox3->addItem("man");
combox3->addItem("woman");
QComboBox *combox4 = new QComboBox(ui->tableWidget);
combox4->addItem("man");
combox4->addItem("woman");
QComboBox *combox5 = new QComboBox(ui->tableWidget);
combox5->addItem("man");
combox5->addItem("woman");
//添加
ui->tableWidget->setItem(0,0,new QTableWidgetItem("Tom"));
//ui->tableWidget->setItem(0,1,new QTableWidgetItem("man"));
ui->tableWidget->setItem(0,2,new QTableWidgetItem("1999.11.11"));
ui->tableWidget->setItem(0,3,new QTableWidgetItem("England"));
ui->tableWidget->setItem(1,0,new QTableWidgetItem("Jery"));
//ui->tableWidget->setItem(1,1,new QTableWidgetItem("man"));
ui->tableWidget->setItem(1,2,new QTableWidgetItem("1997.08.12"));
ui->tableWidget->setItem(1,3,new QTableWidgetItem("England"));
ui->tableWidget->setItem(2,0,new QTableWidgetItem("Mary"));
//ui->tableWidget->setItem(2,1,new QTableWidgetItem("woman"));
ui->tableWidget->setItem(2,2,new QTableWidgetItem("1998.03.12"));
ui->tableWidget->setItem(2,3,new QTableWidgetItem("England"));
ui->tableWidget->setItem(3,0,new QTableWidgetItem("Jessy"));
//ui->tableWidget->setItem(3,1,new QTableWidgetItem("woman"));
ui->tableWidget->setItem(3,2,new QTableWidgetItem("2000.04.17"));
ui->tableWidget->setItem(3,3,new QTableWidgetItem("England"));
ui->tableWidget->setItem(4,0,new QTableWidgetItem("Jim"));
//ui->tableWidget->setItem(4,1,new QTableWidgetItem("man"));
ui->tableWidget->setItem(4,2,new QTableWidgetItem("1997.09.28"));
ui->tableWidget->setItem(4,3,new QTableWidgetItem("England"));
ui->tableWidget->setCellWidget(0,1,combox1);
ui->tableWidget->setCellWidget(1,1,combox2);
ui->tableWidget->setCellWidget(2,1,combox3);
ui->tableWidget->setCellWidget(3,1,combox4);
ui->tableWidget->setCellWidget(4,1,combox5);
//设置单个item属性
//对单个item进行设置
QTableWidgetItem *item = new QTableWidgetItem("item");
//获取原有字体设置
QFont font = item->font();
//设置为粗体
font.setBold(true);
//字体大小
font.setPointSize(12);
//字体颜色
item->setTextColor(Qt::red);
//设置字体
item->setFont(font);
//文本对齐格式
item->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
这里添加一个右键菜单,有两个action,一个删除 一个添加
m_menu = new QMenu(this);
m_actionAdd = new QAction("添加",m_menu);
connect(m_actionAdd,&QAction::triggered,this,&Form::slotAdd);
m_actionDel = new QAction("删除",m_menu);
connect(m_actionDel,&QAction::triggered,this,&Form::slotDel);
m_menu->addAction(m_actionAdd);
m_menu->addAction(m_actionDel);
connect(ui->tableWidget,&QTableWidget::customContextMenuRequested,this,&Form::slotPopMenu);
void Form::slotAdd()
{
int row = ui->tableWidget->currentRow();
ui->tableWidget->insertRow(row);
}
void Form::slotDel()
{
int row = ui->tableWidget->currentRow();
ui->tableWidget->removeRow(row);
}
void Form::slotPopMenu(const QPoint &pos)
{
qDebug()<height() / 2 );
m_menu->exec(ui->tableWidget->mapToGlobal(p));
}
这里简单的设置了一下颜色,头部样式,仅供参考。
const QString styles = "QTableView\
{\
selection-background-color: qlineargradient(x1: 0, y1: 0, x2: 0.5, y2: 0.5,\
stop: 0 #616161, stop: 1 505050);\
/*alternate-background-color:blue;*/\
}";
const QString headerStyle = "QHeaderView::section\
{\
background-color: qlineargradient(x1:0, y1:0, x2:0, y2:1,\
stop:0 #616161, stop: 0.5 #505050,\
stop: 0.6 #434343, stop:1 #656565);\
color: white;\
padding-left: 4px;\
border: 1px solid #6c6c6c;\
}\
/*QHeaderView::section:checked\
{\
background-color: red;\
}*/";
ui->tableWidget->setStyleSheet(styles);
ui->tableWidget->horizontalHeader()->setStyleSheet(headerStyle);
clear() 和 removeRow方法都会delete项数据,不必担心内存泄露。
//清除表格数据区的所有内容,但是不清除表头。
ui->tableWidget->clearContents();
//ui->tableWidget->clear();
int nCount = ui->tableWidget->rowCount();
for(int i=0;itableWidget->removeRow(0);
}
QTableWidget详解_qtablewidget-C/C++文档类资源-CSDN下载
QTreeView详解https://blog.csdn.net/wzz953200463/article/details/123643823?spm=1001.2014.3001.5502QTreeWidget详解https://blog.csdn.net/wzz953200463/article/details/109996501
QTableView详解https://blog.csdn.net/wzz953200463/article/details/123645541?spm=1001.2014.3001.5502
QListWidget详解https://blog.csdn.net/wzz953200463/article/details/109648247
QListView详解https://blog.csdn.net/wzz953200463/article/details/123646413?spm=1001.2014.3001.5502