QTableWidget是一种列表视图,与此类似的还有QTableView
QTableWidget是QTableView的子类,主要的区别是QTableView可以使用自定义的数据模型来显示内容(也就是先要通过setModel来绑定数据源),而QTableWidget则只能使用标准的数据模型,并且其单元格数据是QTableWidgetItem的对象来实现的(也就是不需要数据源,将逐个单元格内的信息填好即可)。这主要体现在QTableView类中有setModel成员函数,而到了QTableWidget类中,该成员函数变成了私有。使用QTableWidget就离不开QTableWidgetItem。QTableWidgetItem用来表示表格中的一个单元格,整个表格都需要用逐个单元格构建起来。
默认情况下,表格内容是可以编辑的,可以通过以下方式,禁止修改表格内容
tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);
/*
其它枚举选项如下
QAbstractItemView::NoEditTriggers 0 不能对表格内容进行修改
QAbstractItemView::CurrentChanged 1 任何时候都能对单元格修改
QAbstractItemView::DoubleClicked 2 双击单元格
QAbstractItemView::SelectedClicked 4 单击已选中的内容
QAbstractItemView::EditKeyPressed 8 编辑按钮按下
QAbstractItemView::AnyKeyPressed 16 按下任意键就能修改
QAbstractItemView::AllEditTriggers 31 以上条件全包括
*/
QStringList headerList;
headerList << "name" << "age" << "grade" << "score";
tableWidget->setColumnCount(headerList .size());
tableWidget->setHorizontalHeaderLabels(headerList );
默认情况下,表格是可以按照单元格选择的,可以通过以下方式设置正行选择
tableWidget->setSelectionBehavior(QAbstractItemView::SelectRows); //整行选中的方式
tableWidget->verticalHeader()->setVisible(true); //显示列表头
tableWidget->horizontalHeader()->setVisible(true); //显示行表头
tableWidget->verticalHeader()->setStyleSheet("QHeaderView::section{background:skyblue}");
tableWidget->verticalHeader()->setDefaultAlignment(Qt::AlignLeft);
tableWidget->horizontalHeader()->setStyleSheet("QHeaderView::section{background:skyblue}");
tableWidget->horizontalHeader()->setDefaultAlignment(Qt::AlignLeft);
tableWidget->setAlternatingRowColors(true);
tableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);
tableWidget->verticalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);
tableWidget->horizontalHeader()->setResizeMode(QHeaderView::Stretch); 自适应列宽
tableWidget->verticalHeader()->setResizeMode(QHeaderView::Stretch); 自适应行高
tableWidget->horizontalHeader()->setStretchLastSection(true);
int rowCnt = tableWidget->rowCount();
tableWidget->insertRow(rowCnt);
tableWidget->verticalScrollBar()->setStyleSheet("QScrollBar:vertical{" //垂直滑块整体
"background:#FFFFFF;" //背景色
"padding-top:20px;" //上预留位置(放置向上箭头)
"padding-bottom:20px;" //下预留位置(放置向下箭头)
"padding-left:3px;" //左预留位置(美观)
"padding-right:3px;" //右预留位置(美观)
"border-left:1px solid #d7d7d7;}"//左分割线
"QScrollBar::handle:vertical{"//滑块样式
"background:#dbdbdb;" //滑块颜色
"border-radius:6px;" //边角圆润
"min-height:80px;}" //滑块最小高度
"QScrollBar::handle:vertical:hover{"//鼠标触及滑块样式
"background:#d0d0d0;}" //滑块颜色
);
tableWidget->horizontalScrollBar()->setStyleSheet("QScrollBar:vertical{" //垂直滑块整体
"background:#FFFFFF;" //背景色
"padding-top:20px;" //上预留位置(放置向上箭头)
"padding-bottom:20px;" //下预留位置(放置向下箭头)
"padding-left:3px;" //左预留位置(美观)
"padding-right:3px;" //右预留位置(美观)
"border-left:1px solid #d7d7d7;}"//左分割线
"QScrollBar::handle:vertical{"//滑块样式
"background:#dbdbdb;" //滑块颜色
"border-radius:6px;" //边角圆润
"min-height:80px;}" //滑块最小高度
"QScrollBar::handle:vertical:hover{"//鼠标触及滑块样式
"background:#d0d0d0;}" //滑块颜色
);
QComboBox *comBox = new QComboBox();
comBox->addItem("Y");
comBox->addItem("N");
tableWidget->setCellWidget(row,column,comBox);
tableWidget->setContextMenuPolicy(Qt::CustomContextMenu);
connect(tableWidget, SIGNAL(customContextMenuRequested(const QPoint &)),this,
SLOT(showRightClickContextMenu(const QPoint &)));
showRightClickContextMenu即为显示右键菜单的函数,可在此函数中创建自定义的右键菜单
void showRightClickContextMenu(const QPoint &point){
QMenu tblRightClickMenu;
QAction *m1 = new QAction("xxx1", &tblRightClickMenu);
QAction *m2 = new QAction("xxx2", &tblRightClickMenu);
QAction *m3 = new QAction("xxx3", &tblRightClickMenu);
connect(m1 , SIGNAL(triggered(bool)),this, SLOT(xxx1Process()));
tblRightClickMenu.exec(QCursor::pos());
}
#ifndef WIDGET_H
#define WIDGET_H
#include
#include
#include
#include
#include
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = 0);
~Widget();
private:
QTableWidget *tableWidget;
QHBoxLayout* testLayout;
QModelIndex rightClickIndex;
private slots:
void showRightClickContextMenu(const QPoint&);
void action1RightClickSlot();
void action2RightClickSlot();
void action3RightClickSlot();
};
#endif // WIDGET_H
#include
#include
#include
#include
#include
#include
#include
#include
#include "widget.h"
Widget::Widget(QWidget *parent)
: QWidget(parent)
{
testLayout = new QHBoxLayout(this);
tableWidget = new QTableWidget(this);
QStringList headerList;
headerList << "name" << "sex" << "age" << "grade" << "score" << "combox";
tableWidget->setColumnCount(headerList .size());
tableWidget->setHorizontalHeaderLabels(headerList);
tableWidget->setStyleSheet(QStringLiteral("alternate-background-color:rgb(240, 255, 255)"));
tableWidget->setAlternatingRowColors(true);
tableWidget->setSelectionBehavior(QAbstractItemView::SelectRows);
// tableWidget->setStyleSheet(QStringLiteral("selection-background-color:rgb(240, 255, 255)"));
tableWidget->horizontalHeader()->setStyleSheet("QHeaderView::section{background:skyblue}");
tableWidget->horizontalHeader()->setDefaultAlignment(Qt::AlignLeft);
tableWidget->verticalHeader()->setStyleSheet("QHeaderView::section{background:skyblue}");
tableWidget->verticalHeader()->setDefaultAlignment(Qt::AlignLeft);
tableWidget->horizontalHeader()->setStretchLastSection(true);
tableWidget->setAlternatingRowColors(true);
tableWidget->verticalHeader()->setVisible(true); //显示列表头
tableWidget->horizontalHeader()->setVisible(true); //显示行表头
for(int i = 0; i < 100; i++)
{
tableWidget->insertRow(i);
tableWidget->setItem(i, 0, new QTableWidgetItem(QString("xxx") + QString::number(i)));
tableWidget->setItem(i, 1, new QTableWidgetItem(QString("yyy") + QString::number(i)));
tableWidget->setItem(i, 2, new QTableWidgetItem(QString("zzz") + QString::number(i)));
tableWidget->setItem(i, 3, new QTableWidgetItem(QString("kkk") + QString::number(i)));
tableWidget->setItem(i, 4, new QTableWidgetItem(QString("mmm") + QString::number(i)));
QComboBox *comBox = new QComboBox();
comBox->addItem("Y");
comBox->addItem("N");
tableWidget->setCellWidget(i,5,comBox);
}
tableWidget->verticalScrollBar()->setStyleSheet("QScrollBar:vertical{" //垂直滑块整体
"background:#FFFFFF;" //背景色
"padding-top:20px;" //上预留位置(放置向上箭头)
"padding-bottom:20px;" //下预留位置(放置向下箭头)
"padding-left:3px;" //左预留位置(美观)
"padding-right:3px;" //右预留位置(美观)
"border-left:1px solid #d7d7d7;}"//左分割线
"QScrollBar::handle:vertical{"//滑块样式
"background:#dbdbdb;" //滑块颜色
"border-radius:6px;" //边角圆润
"min-height:80px;}" //滑块最小高度
"QScrollBar::handle:vertical:hover{"//鼠标触及滑块样式
"background:#d0d0d0;}" //滑块颜色
);
tableWidget->horizontalScrollBar()->setStyleSheet("QScrollBar:horizontal{"
"background:#FFFFFF;"
"padding-top:3px;"
"padding-bottom:3px;"
"padding-left:20px;"
"padding-right:20px;}"
"QScrollBar::handle:horizontal{"
"background:#dbdbdb;"
"border-radius:6px;"
"min-width:80px;}"
"QScrollBar::handle:horizontal:hover{"
"background:#d0d0d0;}"
);
tableWidget->setContextMenuPolicy(Qt::CustomContextMenu);
connect(tableWidget, SIGNAL(customContextMenuRequested(const QPoint&)), this, SLOT(showRightClickContextMenu(const QPoint&)));
testLayout->addWidget(tableWidget);
setLayout(testLayout);
}
Widget::~Widget()
{
}
void Widget::showRightClickContextMenu(const QPoint &point)
{
QMenu menu;
QAction *action1 = new QAction(tr("信息1"), &menu);
QAction *action2 = new QAction(tr("信息2"), &menu);
QAction *action3 = new QAction(tr("信息3"), &menu);
menu.addAction(action1);
menu.addAction(action2);
menu.addAction(action3);
connect(action1, SIGNAL(triggered()), this, SLOT(action1RightClickSlot()));
connect(action2, SIGNAL(triggered()), this, SLOT(action2RightClickSlot()));
connect(action3, SIGNAL(triggered()), this, SLOT(action3RightClickSlot()));
rightClickIndex = tableWidget->indexAt(point);
if(rightClickIndex.isValid())
{
menu.exec(QCursor::pos());
}
}
void Widget::action1RightClickSlot()
{
int row = rightClickIndex.row();
int column = rightClickIndex.column();
qDebug() <<"row:" << row+1 << "column:" << column+1 << "action1 clicked!!";
}
void Widget::action2RightClickSlot()
{
int row = rightClickIndex.row();
int column = rightClickIndex.column();
qDebug() <<"row:" << row+1 << "column:" << column+1 << "action2 clicked!!";
}
void Widget::action3RightClickSlot()
{
int row = rightClickIndex.row();
int column = rightClickIndex.column();
qDebug() <<"row:" << row+1 << "column:" << column+1 << "action3 clicked!!";
}
剩下的以后使用中在摸索吧。