Qt中的QTableWidget
2012-05-21 20:32 Rollen Holt 阅读(10755) 评论(0) 编辑 收藏 举报最近天气真热啊,下午的话,基本没什么效率,热死人了。今天吃饭的时候看了一下Qt中的QTableWidget的API。官方文档是:
http://qt-project.org/doc/qt-4.8/QTableWidget.html
QTableWidget ( QWidget * parent = 0 ) |
|
QTableWidget ( int rows, int columns, QWidget * parent = 0 ) | |
~QTableWidget () | |
QWidget * | cellWidget ( int row, int column ) const |
void | closePersistentEditor ( QTableWidgetItem * item ) |
int | column ( const QTableWidgetItem * item ) const |
int | columnCount () const |
int | currentColumn () const |
QTableWidgetItem * | currentItem () const |
int | currentRow () const |
void | editItem ( QTableWidgetItem * item ) |
QList<QTableWidgetItem *> | findItems ( const QString & text, Qt::MatchFlags flags ) const |
QTableWidgetItem * | horizontalHeaderItem ( int column ) const |
QTableWidgetItem * | item ( int row, int column ) const |
QTableWidgetItem * | itemAt ( const QPoint & point ) const |
QTableWidgetItem * | itemAt ( int ax, int ay ) const |
const QTableWidgetItem * | itemPrototype () const |
void | openPersistentEditor ( QTableWidgetItem * item ) |
void | removeCellWidget ( int row, int column ) |
int | row ( const QTableWidgetItem * item ) const |
int | rowCount () const |
QList<QTableWidgetItem *> | selectedItems () |
QList<QTableWidgetSelectionRange> | selectedRanges () const |
void | setCellWidget ( int row, int column, QWidget * widget ) |
void | setColumnCount ( int columns ) |
void | setCurrentCell ( int row, int column ) |
void | setCurrentCell ( int row, int column, QItemSelectionModel::SelectionFlags command ) |
void | setCurrentItem ( QTableWidgetItem * item ) |
void | setCurrentItem ( QTableWidgetItem * item, QItemSelectionModel::SelectionFlags command ) |
void | setHorizontalHeaderItem ( int column, QTableWidgetItem * item ) |
void | setHorizontalHeaderLabels ( const QStringList & labels ) |
void | setItem ( int row, int column, QTableWidgetItem * item ) |
void | setItemPrototype ( const QTableWidgetItem * item ) |
void | setRangeSelected ( const QTableWidgetSelectionRange & range, bool select ) |
void | setRowCount ( int rows ) |
void | setVerticalHeaderItem ( int row, QTableWidgetItem * item ) |
void | setVerticalHeaderLabels ( const QStringList & labels ) |
void | sortItems ( int column, Qt::SortOrder order = Qt::AscendingOrder ) |
QTableWidgetItem * | takeHorizontalHeaderItem ( int column ) |
QTableWidgetItem * | takeItem ( int row, int column ) |
QTableWidgetItem * | takeVerticalHeaderItem ( int row ) |
QTableWidgetItem * | verticalHeaderItem ( int row ) const |
int | visualColumn ( int logicalColumn ) const |
QRect | visualItemRect ( const QTableWidgetItem * item ) const |
int | visualRow ( int logicalRow ) const |
- 34 public functions inherited from QTableView
- 57 public functions inherited from QAbstractItemView
- 17 public functions inherited from QAbstractScrollArea
- 14 public functions inherited from QFrame
- 221 public functions inherited from QWidget
- 29 public functions inherited from QObject
- 13 public functions inherited from QPaintDevice
Public Slots
void | clear () |
void | clearContents () |
void | insertColumn ( int column ) |
void | insertRow ( int row ) |
void | removeColumn ( int column ) |
void | removeRow ( int row ) |
void | scrollToItem ( const QTableWidgetItem * item, QAbstractItemView::ScrollHint hint = EnsureVisible ) |
- 11 public slots inherited from QTableView
- 9 public slots inherited from QAbstractItemView
- 19 public slots inherited from QWidget
- 1 public slot inherited from QObject
Signals
void | cellActivated ( int row, int column ) |
void | cellChanged ( int row, int column ) |
void | cellClicked ( int row, int column ) |
void | cellDoubleClicked ( int row, int column ) |
void | cellEntered ( int row, int column ) |
void | cellPressed ( int row, int column ) |
void | currentCellChanged ( int currentRow, int currentColumn, int previousRow, int previousColumn ) |
void | currentItemChanged ( QTableWidgetItem * current, QTableWidgetItem * previous ) |
void | itemActivated ( QTableWidgetItem * item ) |
void | itemChanged ( QTableWidgetItem * item ) |
void | itemClicked ( QTableWidgetItem * item ) |
void | itemDoubleClicked ( QTableWidgetItem * item ) |
void | itemEntered ( QTableWidgetItem * item ) |
void | itemPressed ( QTableWidgetItem * item ) |
void | itemSelectionChanged () |
- 6 signals inherited from QAbstractItemView
- 1 signal inherited from QWidget
- 1 signal inherited from QObject
Protected Functions
virtual bool | dropMimeData ( int row, int column, const QMimeData * data, Qt::DropAction action ) |
QModelIndex | indexFromItem ( QTableWidgetItem * item ) const |
QTableWidgetItem * | itemFromIndex ( const QModelIndex & index ) const |
QList<QTableWidgetItem *> | items ( const QMimeData * data ) const |
virtual QMimeData * | mimeData ( const QList<QTableWidgetItem *> items ) const |
virtual QStringList | mimeTypes () const |
virtual Qt::DropActions | supportedDropActions () const |
Reimplemented Protected Functions
virtual void | dropEvent ( QDropEvent * event ) |
virtual bool | event ( QEvent * e ) |
- 14 protected functions inherited from QTableView
- 36 protected functions inherited from QAbstractItemView
- 18 protected functions inherited from QAbstractScrollArea
- 3 protected functions inherited from QFrame
- 37 protected functions inherited from QWidget
- 8 protected functions inherited from QObject
- 1 protected function inherited from QPaintDevice
Additional Inherited Members
- 4 static public members inherited from QWidget
- 7 static public members inherited from QObject
- 6 protected slots inherited from QTableView
- 9 protected slots inherited from QAbstractItemView
- 1 protected slot inherited from QAbstractScrollArea
- 1 protected slot inherited from QWidget
1. 将表格变为禁止编辑
在默认情况下,表格里的字符是可以更改的,比如 双击一个单元格,就可以修改原来的内容,如果想禁止用户的这种操作,让这个表格对用户只读,可以这样:
tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);
QAbstractItemView.NoEditTriggers 是QAbstractItemView.EditTrigger枚举中的一个,都是触发修改单元格内容的条件:
QAbstractItemView.NoEditTriggers
0
No editing possible. 不能对表格内容进行修改
QAbstractItemView.CurrentChanged
1
Editing start whenever current item changes.任何时候都能对单元格修改
QAbstractItemView.DoubleClicked
2
Editing starts when an item is double clicked.双击单元格
QAbstractItemView.SelectedClicked
4
Editing starts when clicking on an already selected item.单击已选中的内容
QAbstractItemView.EditKeyPressed
8
Editing starts when the platform. edit key has been pressed over an item.
QAbstractItemView.AnyKeyPressed
16
Editing starts when any key is pressed over an item.按下任意键就能修改
QAbstractItemView.AllEditTriggers
31
Editing starts for all above actions.以上条件全包括
2. 设置表格为整行选择
tableWidget->setSelectionBehavior(QAbstractItemView::SelectRows); //整行选中的方式
QAbstractItemView.SelectionBehavior枚举还有如下类型
Constant
Value
Description
QAbstractItemView.SelectItems
0
Selecting single items.选中单个单元格
QAbstractItemView.SelectRows
1
Selecting only rows.选中一行
QAbstractItemView.SelectColumns
2
Selecting only columns.选中一列
3.单个选中和多个选中的设置:
tableWidget->setSelectionMode(QAbstractItemView::ExtendedSelection); //设置为可以选中多个目标
该函数的参数还可以是:
QAbstractItemView.NoSelection 不能选择
QAbstractItemView.SingleSelection 选中单个目标
QAbstractItemView.MultiSelection 选中多个目标
QAbstractItemView.ExtendedSelection QAbstractItemView.ContiguousSelection 的区别不明显,主要功能是正常情况下是单选,但按下Ctrl或Shift键后,可以多选
4. 表格表头的显示与隐藏
对 于水平或垂直方法的表头,可以用以下方式进行 隐藏/显示 的设置:
view plain
tableWidget->verticalHeader()->setVisible(false); //隐藏列表头
tableWidget->horizontalHeader()->setVisible(false); //隐藏行表头
注意:需要 #include <QHeaderView>
5. 对表头文字的字体、颜色进行设置
view plain
QTableWidgetItem *columnHeaderItem0 = tableWidget->horizontalHeaderItem(0); //获得水平方向表头的Item对象
columnHeaderItem0->setFont(QFont("Helvetica")); //设置字体
columnHeaderItem0->setBackgroundColor(QColor(0,60,10)); //设置单元格背景颜色
columnHeaderItem0->setTextColor(QColor(200,111,30)); //设置文字颜色
注意:需要 #include <QHeaderView>
6. 在单元格里加入控件:
QTableWidget不仅允许把文字加到单元格,还允许把控件也放到单元格中。比如,把一个下拉框加入单元格, 可以这么做:
view plain
QComboBox *comBox = new QComboBox();
comBox->addItem("Y");
comBox->addItem("N");
tableWidget->setCellWidget(0,2,comBox);
二. 对单元格的进行设置
1. 单元格设置字体颜色和背景颜色 及字体字符
view plain
QTableWidgetItem *item = new QTableWidgetItem("Apple");
item->setBackgroundColor(QColor(0,60,10));
item->setTextColor(QColor(200,111,100));
item->setFont(QFont("Helvetica"));
tableWidget->setItem(0,3,item);
另:如果需要对所有的单元格都使用这种字体,则 可以使用 tableWidget->setFont(QFont("Helvetica"));
2. 设置单元格内文字的对齐方式
这个比较简单,使用newItem.setTextAlignment()函数即可,该函数的参数为单元格内 的对齐方式,和字符输入顺序是自左相右还是自右向左。
水平对齐方式有:
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.
如果两种都要设置,只要用 Qt.AlignHCenter | Qt.AlignVCenter 的方式即可
3. 合并单元格效果的实现:
tableWidget->setSpan(0, 0, 3, 1) # 其参数为: 要改变单元格的 1行数 2列数 要合并的 3行数 4列数
4. 设置单元格的大小
首先,可以指定 某个行或者列的大小
view plain
tableWidget->setColumnWidth(3,200);
tableWidget->setRowHeight(3,60);
还可以将行和列的大小设为与内容相匹配
view plain
tableWidget->resizeColumnsToContents();
tableWidget->resizeRowsToContents();
5. 获得单击单元格的内容
通过实现 itemClicked (QTableWidgetItem *) 信号的槽函数,就可以获得鼠标单击到的单元格指针,进而获得其中的文字信息
connect(tableWidget,SIGNAL(itemDoubleClicked(QTreeWidgetItem*,int)),this, SLOT( getItem(QTreeWidgetItem*,int)) );
//将itemClicked信号与函数 getItem绑定
QTableWidget *table = new QTableWIdget(this);
table->setColumnCount(5); //设置列数
table->setRowCount(3); //设置行数/
/*设置列名*/
QStringList headers;
headers<<"列名1"<<"列名2"<<"列名3";
table->setHorizontalHeaderLabels(headers);
/*给单元格添加内容*/
void addItemContent(int row, int column, QString content)
{
QTableWidgetItem *item = new QTableWidgetItem (content);
table->setItem(row, column, item);
}
/*给单元格中增加图标*/
QTableWidgetItem *item = new QTableWidgetItem(QIcon("myImage.jpg"), NULL); //只加入图标,没有字符串
QTableWidgetItem *item = new QTableWidgetItem(QIcon("myImage.jpg"), myString); //加入图标和字符串
table->setItem(row, column, item);
/*插入一行*/
int row = table->rowCount();
table->insertRow(row);
/*插入一列*/
int column = table->columnCount();
table->insertColumn(column);
//使行列头自适应宽度,最后一列将会填充空白部分
table->horizontalHeader()->setStretchLastSection(true);
//使行列头自适应宽度,所有列平均分来填充空白部分
table->horizontalHeader()->setResizeMode(QHeaderView::Strtch);
//使行自适应高度,假如行很多的话,行的高度不会一直减小,当达到一定值时会自动生成一个QScrollBar
table->verticalHeader()->setResizeMode(QHeaderView::Strtch);
//设置单击选择一行
table->setSelectionBehuavior(QAbstractItemView::SelectRows);
//设置每行内容不可编辑
table->setEditTriggers(QAbstractItemView::NoEditTriggers);
//设置只能选择一行,不能选择多行
table->setSelectionMode(QAbstractItemView::SingleSelection);
/*去掉每行的行号*/
QHeaderView *headerView = table->verticalHeader();
headerView->setHidden(true);
/*设置让某个单元格或某行选中*/
选中单元格:table->setCurrentCell(row, column, QItemSelectionModel::Select);
选中一行:table->setCurrentCell(row, QItemSelectionModel::Select);(注意此处的列没有值)
==============================================================================
本博客已经废弃,不在维护。新博客地址:http://wenchao.ren
我喜欢程序员,他们单纯、固执、容易体会到成就感;面对压力,能够挑灯夜战不眠不休;面对困难,能够迎难而上挑战自我。他
们也会感到困惑与傍徨,但每个程序员的心中都有一个比尔盖茨或是乔布斯的梦想“用智慧开创属于自己的事业”。我想说的是,其
实我是一个程序员
==============================================================================