Qt之QTableWidget的使用及常用设置
2017-06-29 21:27
696 查看
各种软件设计中,表格的使用非常频繁,下面介绍的是关于QTableWidget的使用以及常用属性的设置。
(1)创建一个新的表格控件
[cpp] view plain copy print?QTableWidget *table=new QTableWidget();
(2)设置列数
[cpp] view plain copy print?table->setColumnCount(3);//设置3列
(3)设置行数
[cpp] view plain copy print?table->setRowCount(5);//设置5行
(4)设置表头内容
[cpp] view plain copy print?QStringList header; //QString类型的List容器
header<<”学号”<<“姓名”<<“性别”;//设置内容是学号、姓名、性别
table->setHorizontalHeaderLabels(header);//设置表头内容
[cpp] view plain copy print?QTableWidgetItem *name=new QTableWidgetItem();//创建一个Item
name->setText(”桃花庵”);//设置内容
table->setItem(0,1,name);//把这个Item加到第一行第二列中
(6)可设置表格单元显示QComBox、QSpinBox、QLable、QDateTimeEdit等
[cpp] view plain copy print?QDateTimeEdit *dateTimeEdit= new QDateTimeEdit(); //创建一个QDateTimeEdit
dateTimeEdit->setDateTime(QDateTime::currentDateTime());//设置为当前时间
dateTimeEdit->setDisplayFormat(”dd/M/yyyy”);//设置显示格式
dateTimeEdit->setCalendarPopup(true);//设置可以打开日历
table->setCellWidget (1,2,dateTimeEdit);//加到第二行第三列中
(7)设置表头的字体属性
[cpp] view plain copy print?QFont font ;//定义一个字体变量 font.setBold(true); //设置粗体 table->horizontalHeader()->setFont(font);//把字体变量属性加进表头中
(8)设置表头的内容充满整个表格的宽度
[cpp] view plain copy print?table->horizontalHeader()->setStretchLastSection(true); //设置表头充满表格的宽度
(9)设置表头的高度
[cpp] view plain copy print?table->horizontalHeader()->setFixedHeight(25); //设置表头的高度为25
(10)设置表头的列宽
[cpp] view plain copy print?table->horizontalHeader()->resizeSection(0,180); //设置表头第1列的宽度为180
(11)设置表格的行高
[cpp] view plain copy print?table->verticalHeader()->setDefaultSectionSize(10); //设置行高为10
(12)消除表格控件的边框
[cpp] view plain copy print?table->setFrameShape(QFrame::NoFrame); //设置无边框
(13)设置表格不显示格子线
[cpp] view plain copy print?table->setShowGrid(false); //设置不显示格子线
(14)去除每一行的左边序号
[cpp] view plain copy print?table->verticalHeader()->setVisible(false); //设置垂直头不可见,即左边序号不见
(15)设置选择行为时每次一行
[cpp] view plain copy print?table->setSelectionBehavior(QAbstractItemView::SelectRows); //设置选择行为时每次一行
(16)设置对行可进行多选
[cpp] view plain copy print?table->setSelectionMode(QAbstractItemView::ExtendedSelection); //可多选(Ctrl、Shift、Ctrl+A都可以)
(17)设置表格内容不能编辑
[cpp] view plain copy print?table->setEditTriggers(QAbstractItemView::NoEditTriggers); //设置表格内容不可编辑
(18)设置选中行的背景色
[cpp] view plain copy print?table->setStyleSheet(“selection-background-color:rgb(34, 170, 75);”); //设置选中行的背景色
(19)设置表头的背景色
[cpp] view plain copy print?table->horizontalHeader()->setStyleSheet(“QHeaderView::section{background:skyblue;}”); //设置表头的背景色
(20)设置水平滚动条样式
[cpp] view plain copy print?table->horizontalScrollBar()->setStyleSheet(“QScrollBar{background:transparent; height:12px;}”
”QScrollBar::handle{background:lightgray; border:2px solid transparent; border-radius:5px;}”
”QScrollBar::handle:hover{background:gray;}”
”QScrollBar::sub-line{background:transparent;}”
”QScrollBar::add-line{background:transparent;}”);
(21)设置垂直滚动条的样式
[cpp] view plain copy print?table->verticalScrollBar()->setStyleSheet(“QScrollBar{background:transparent; width: 12px;}”
”QScrollBar::handle{background:lightgray; border:2px solid transparent; border-radius:5px;}”
”QScrollBar::handle:hover{background:gray;}”
”QScrollBar::sub-line{background:transparent;}”
”QScrollBar::add-line{background:transparent;}”);
(1)创建一个新的表格控件
[cpp] view plain copy print?QTableWidget *table=new QTableWidget();
QTableWidget *table=new QTableWidget();
(2)设置列数
[cpp] view plain copy print?table->setColumnCount(3);//设置3列
table->setColumnCount(3);//设置3列注意:如果内容超过3列,则无法显示超过3列的内容,所以需要看情况重新设置列数
(3)设置行数
[cpp] view plain copy print?table->setRowCount(5);//设置5行
table->setRowCount(5);//设置5行注意:如果内容超过5行,则无法显示超过3行的内容,所以需要看情况重新设置行数
(4)设置表头内容
[cpp] view plain copy print?QStringList header; //QString类型的List容器
header<<”学号”<<“姓名”<<“性别”;//设置内容是学号、姓名、性别
table->setHorizontalHeaderLabels(header);//设置表头内容
QStringList header; //QString类型的List容器 header<<"学号"<<"姓名"<<"性别";//设置内容是学号、姓名、性别 table->setHorizontalHeaderLabels(header);//设置表头内容(5)设置表格文字内容
[cpp] view plain copy print?QTableWidgetItem *name=new QTableWidgetItem();//创建一个Item
name->setText(”桃花庵”);//设置内容
table->setItem(0,1,name);//把这个Item加到第一行第二列中
QTableWidgetItem *name=new QTableWidgetItem();//创建一个Item name->setText("桃花庵");//设置内容 table->setItem(0,1,name);//把这个Item加到第一行第二列中
(6)可设置表格单元显示QComBox、QSpinBox、QLable、QDateTimeEdit等
[cpp] view plain copy print?QDateTimeEdit *dateTimeEdit= new QDateTimeEdit(); //创建一个QDateTimeEdit
dateTimeEdit->setDateTime(QDateTime::currentDateTime());//设置为当前时间
dateTimeEdit->setDisplayFormat(”dd/M/yyyy”);//设置显示格式
dateTimeEdit->setCalendarPopup(true);//设置可以打开日历
table->setCellWidget (1,2,dateTimeEdit);//加到第二行第三列中
QDateTimeEdit *dateTimeEdit= new QDateTimeEdit(); //创建一个QDateTimeEdit dateTimeEdit->setDateTime(QDateTime::currentDateTime());//设置为当前时间 dateTimeEdit->setDisplayFormat("dd/M/yyyy");//设置显示格式 dateTimeEdit->setCalendarPopup(true);//设置可以打开日历 table->setCellWidget (1,2,dateTimeEdit);//加到第二行第三列中
(7)设置表头的字体属性
[cpp] view plain copy print?QFont font ;//定义一个字体变量 font.setBold(true); //设置粗体 table->horizontalHeader()->setFont(font);//把字体变量属性加进表头中
QFont font ;//定义一个字体变量 font.setBold(true); //设置粗体 table->horizontalHeader()->setFont(font);//把字体变量属性加进表头中
(8)设置表头的内容充满整个表格的宽度
[cpp] view plain copy print?table->horizontalHeader()->setStretchLastSection(true); //设置表头充满表格的宽度
table->horizontalHeader()->setStretchLastSection(true); //设置表头充满表格的宽度
(9)设置表头的高度
[cpp] view plain copy print?table->horizontalHeader()->setFixedHeight(25); //设置表头的高度为25
table->horizontalHeader()->setFixedHeight(25); //设 db6d 置表头的高度为25
(10)设置表头的列宽
[cpp] view plain copy print?table->horizontalHeader()->resizeSection(0,180); //设置表头第1列的宽度为180
table->horizontalHeader()->resizeSection(0,180); //设置表头第1列的宽度为180
(11)设置表格的行高
[cpp] view plain copy print?table->verticalHeader()->setDefaultSectionSize(10); //设置行高为10
table->verticalHeader()->setDefaultSectionSize(10); //设置行高为10
(12)消除表格控件的边框
[cpp] view plain copy print?table->setFrameShape(QFrame::NoFrame); //设置无边框
table->setFrameShape(QFrame::NoFrame); //设置无边框
(13)设置表格不显示格子线
[cpp] view plain copy print?table->setShowGrid(false); //设置不显示格子线
table->setShowGrid(false); //设置不显示格子线
(14)去除每一行的左边序号
[cpp] view plain copy print?table->verticalHeader()->setVisible(false); //设置垂直头不可见,即左边序号不见
table->verticalHeader()->setVisible(false); //设置垂直头不可见,即左边序号不见
(15)设置选择行为时每次一行
[cpp] view plain copy print?table->setSelectionBehavior(QAbstractItemView::SelectRows); //设置选择行为时每次一行
table->setSelectionBehavior(QAbstractItemView::SelectRows); //设置选择行为时每次一行
(16)设置对行可进行多选
[cpp] view plain copy print?table->setSelectionMode(QAbstractItemView::ExtendedSelection); //可多选(Ctrl、Shift、Ctrl+A都可以)
table->setSelectionMode(QAbstractItemView::ExtendedSelection); //可多选(Ctrl、Shift、Ctrl+A都可以)
(17)设置表格内容不能编辑
[cpp] view plain copy print?table->setEditTriggers(QAbstractItemView::NoEditTriggers); //设置表格内容不可编辑
table->setEditTriggers(QAbstractItemView::NoEditTriggers); //设置表格内容不可编辑
(18)设置选中行的背景色
[cpp] view plain copy print?table->setStyleSheet(“selection-background-color:rgb(34, 170, 75);”); //设置选中行的背景色
table->setStyleSheet("selection-background-color:rgb(34, 170, 75);"); //设置选中行的背景色
(19)设置表头的背景色
[cpp] view plain copy print?table->horizontalHeader()->setStyleSheet(“QHeaderView::section{background:skyblue;}”); //设置表头的背景色
table->horizontalHeader()->setStyleSheet("QHeaderView::section{background:skyblue;}"); //设置表头的背景色
(20)设置水平滚动条样式
[cpp] view plain copy print?table->horizontalScrollBar()->setStyleSheet(“QScrollBar{background:transparent; height:12px;}”
”QScrollBar::handle{background:lightgray; border:2px solid transparent; border-radius:5px;}”
”QScrollBar::handle:hover{background:gray;}”
”QScrollBar::sub-line{background:transparent;}”
”QScrollBar::add-line{background:transparent;}”);
table->horizontalScrollBar()->setStyleSheet("QScrollBar{background:transparent; height:12px;}" "QScrollBar::handle{background:lightgray; border:2px solid transparent; border-radius:5px;}" "QScrollBar::handle:hover{background:gray;}" "QScrollBar::sub-line{background:transparent;}" "QScrollBar::add-line{background:transparent;}");
(21)设置垂直滚动条的样式
[cpp] view plain copy print?table->verticalScrollBar()->setStyleSheet(“QScrollBar{background:transparent; width: 12px;}”
”QScrollBar::handle{background:lightgray; border:2px solid transparent; border-radius:5px;}”
”QScrollBar::handle:hover{background:gray;}”
”QScrollBar::sub-line{background:transparent;}”
”QScrollBar::add-line{background:transparent;}”);
table->verticalScrollBar()->setStyleSheet("QScrollBar{background:transparent; width: 12px;}" "QScrollBar::handle{background:lightgray; border:2px solid transparent; border-radius:5px;}" "QScrollBar::handle:hover{background:gray;}" "QScrollBar::sub-line{background:transparent;}" "QScrollBar::add-line{background:transparent;}");
相关文章推荐
- Qt之QTableWidget的使用及常用设置
- Qt之QTableWidget的使用及常用设置
- Qt中设置widget背景颜色/图片的注意事项(使用样式表 setStyleSheet())
- Qt4 设置QTableWidget鼠标滑过的颜色
- Qt中设置widget背景颜色/图片的注意事项(使用样式表 setStyleSheet())
- Qt中设置widget背景颜色/图片的注意事项(使用样式表 setStyleSheet())
- QT:给Widget设置背景图片——设置Widget的调色板,调色板使用图片和背景色
- Qt中设置widget背景颜色/图片的注意事项(使用样式表 setStyleSheet())
- Qt中设置widget背景颜色/图片的注意事项(使用样式表 setStyleSheet())
- Qt中设置widget背景颜色/图片的注意事项(使用样式表 setStyleSheet())
- Qt中设置widget背景颜色/图片的注意事项(使用样式表 setStyleSheet())
- Qt中设置widget背景颜色/图片的注意事项(使用样式表 setStyleSheet())
- Qt中设置widget背景颜色/图片的注意事项(使用样式表 setStyleSheet())
- Qt中设置widget背景颜色/图片的注意事项(使用样式表 setStyleSheet())
- QT中QTableWidget的使用
- Qt中设置widget背景颜色/图片的注意事项(使用样式表 setStyleSheet())
- Qt中设置widget背景颜色/图片的注意事项(使用样式表 setStyleSheet())
- QT QTableWidget使用
- Qt之QTableWidget详细使用
- QT 设置QTableWidget背景为透明