-
QTableWidget
table->setSelection
Mode(QAbstractItemView::SingleSelection)
nta
lHeader()->setResizeMode(QheaderView::Fixed);
nta
lHeader()->setStretchLastSection(true);
tTr
igger(QAbstractItemView::NoEditTriggers);
wGrid(false);
Widget
不能在
mainwindow
中随主窗口的大小变化?
解决:在表格外部添加布局。
代码:
tableWidget = new
QTableWidget;
tableWidget
->setObjectName(QString::fromUtf8(
QVBoxLayout
*verticalLayout;
verticalLayout->addWidget(tableWidget
);
2.
将表格变为禁止编辑:
tableWidget->se
tEditTriggers(QAbstractItemView::NoEditTriggers);
(参数
含义:
QAbstractItemView::NoEditTriggers--<
/p>
不能对表格内容进行修改
QAbstractItemView::CurrentC
hanged--
任何时候都能对单元格修改
QAbstractItemVi
ew::DoubleClicked--
双击单元格
QAbstractItemVi
ew::SelectedClicked--
单击已选中的内容
QAbstractItemView::EditKeyPressed--
QAbstractItemVi
ew::AnyKeyPressed--
按下任意键就能修改
QAbstractItemVi
ew::AllEditTriggers--
以上条件全包括)
3.
设置表格为整行选择
tableWidget->se
tSelectionBehavior(QAbstractItemView::SelectRows);
//
整行选中的方式
(参数
含义:
Items--
选中单个单元格
Rows--
选中一行
Columns--
选中一列)
.
4.
单个选中和多个选中的设置:
tableWidget->se
tSelectionMode(QAbstractItemView::ExtendedSelectio
n);
//
设置为可以选
中多个目标
p>
(参数含义:
ction--
不能选择
p>
Selection--
选中单个目标
election--
选中多个目标
edSelection/uousSelection
的区
别不
明
显,主要功能是正常情况下是单选,但按下
Ctrl
或
Shift
键后
,可以多选)
5.
表格表头的显示与隐藏
对于水平或垂直方法的表头,可以用以下方式进行
隐藏
/
显示
的设置:
tab
leWidget->verticalHeader()->setVisible(false);
//
隐藏列表头
tab
leWidget->horizontalHeader()->setVisible(false);
//
隐藏行表头
注意:需要
#include
6.
对表头文字的字体、颜色进行设置
QTableWidgetItem
*columnHeaderItem0
=
tableWidget->horizontalHeaderItem(0); <
/p>
//
获得水
平方向表头的
Item
对象
p>
columnHeaderItem0->setFont(QFont(
< br>设置字体
columnHeaderIte
m0->setBackgroundColor(QColor(0,60,10));
//
设置单元格背景颜色
col
umnHeaderItem0->setTextColor(QColor(200,111,30));
//
设置文字颜色
注意:需要
#include
7.
在单元格里加入控件:
QComboBox *comBox = new QComboBox();
comBox->addItem(
comBox->addItem(
tableWidget->setCellWidget(0,2,comBox);
8.
单元格中添加图片:
tableWidget->setItem(row,0,new
QTableWidgetItem
(QIcon
(
/new/
images/
9
设置单元格字体颜色、背景颜色和字体字符:
QTableWidgetItem *item = new
QTableWidgetItem(
item->setBackgroundColor(QColor(0,60,10));
item->setTextCo
lor(QColor(200,111,100));
item->setFont(QFont(
tableWidget->setItem(0,3,item);
另
p>
:
如
果
需
要
对
所
有
的
单
元
格
< br>都
使
用
这
种
字
体
,
则
可
以
使
用
p>
tableWidget->setFont(QFont(
10.
设置单元格内文字的对齐方式
水平对齐方式有:
Constant Value Description
Qt::AlignLeft
// 0x0001
Aligns with the left edge.
Qt::AlignRight
// 0x0002 Aligns with the right edge.
Qt::AlignHCenter
//
0x0004 Centers horizontally in the available
space.
Qt::AlignJustify
//
0x0008 Justifies the text in the available space.
垂直对齐方式:
Constant Value Description
Qt::AlignTop
//0x0020 Aligns with the top.
Qt::AlignBottom
//0x0040 Aligns with the bottom.
Qt::AlignVCenter
//0x0080 Centers vertically
in the available space.
如果两种都要设置,只要用
Center |
Center
的方式即可
11.
合并单元格:
tableWidget->setSpan(0, 0, 3, 1)
#
其参数为:
要改变单元格的
1
行数、
2
列数,要合
并
的
3
行数、
4
列数
-
-
-
-
-
-
-
-
-
上一篇:桥架规格及重量技术参数(市面上最齐全完整版)
下一篇:企业绩效管理【外文翻译】