Qt Table Widget常用操作
一、鼠标悬浮在item上 显示提示信息
1、在构造函数开启table Widget控件的鼠标捕获功能
// 开启鼠标捕获功能(实现table widget的悬浮功能) ui.tableWidget->setMouseTracking(true);
2、连接信号和槽
connect(ui.tableWidget, SIGNAL( cellEntered(int, int)), this, SLOT(slotCellEntered(int, int)));
3、编写槽函数
void test10292::slotCellEntered(int r, int c) { QTableWidgetItem* item = ui.tableWidget->item(r, c); if (item == nullptr) { return; } QToolTip::showText(QCursor::pos(), item->text()); }
二:点击表头实现排序
第二个是后实现的,按数值排序,因为Qt自带的排序是按 字段排序的
// 连接信号和槽 connect(ui.tableWidget->horizontalHeader(), SIGNAL(sectionClicked(int)), this, SLOT(sortHeader(int))); // 实现槽函数 void test10292::sortHeader(int index) { if (index == 2) { ui.tableWidget->sortItems(index, Qt::DescendingOrder); return; } ui.tableWidget->sortItems(index, Qt::AscendingOrder); }
void test10292::sortHeader(int index) { if (index == 0 || index == 2) { sortTableItem(index); return; } ui.tableWidget->sortItems(index, Qt::AscendingOrder); } // 表头排序 void test10292::sortTableItem(int index) { int rowCountNum = ui.tableWidget->rowCount(); for (int i = rowCountNum - 1; i >= 0; i--) { for (int j = 0; j < i; j++) { if (ui.tableWidget->item(j, index)->text().toInt() < ui.tableWidget->item(j + 1, index)->text().toInt()) { QString str1 = ui.tableWidget->item(j + 1, 0)->text(); QString str2 = ui.tableWidget->item(j + 1, 1)->text(); QString str3 = ui.tableWidget->item(j + 1, 2)->text(); ui.tableWidget->item(j + 1, 0)->setText(ui.tableWidget->item(j, 0)->text()); ui.tableWidget->item(j + 1, 1)->setText(ui.tableWidget->item(j, 1)->text()); ui.tableWidget->item(j + 1, 2)->setText(ui.tableWidget->item(j, 2)->text()); ui.tableWidget->item(j, 0)->setText(str1); ui.tableWidget->item(j, 1)->setText(str2); ui.tableWidget->item(j, 2)->setText(str3); } } } }
三:拖拽item 实现两个item内容的互换
需要写一个类MyQTableWidget继承自QTableWidget,然后重写mousePressEvent和dropEvent事件处理函数
MyQTableWidget.h 文件
#ifndef MYQTABLEWIDGET_H #define MYQTABLEWIDGET_H #include <QTableWidget> #include <QMouseEvent> #include <QDebug> class MyQTableWidget : public QTableWidget { Q_OBJECT public: MyQTableWidget(QWidget *parent); ~MyQTableWidget(); private: QString temp; QPoint m_spos, m_dpos; QTableWidgetItem* sitem; QTableWidgetItem* ditem; void mousePressEvent(QMouseEvent *event); void dropEvent(QDropEvent *event); }; #endif // MYQTABLEWIDGET_H
MyQTableWidget.cpp 文件
#include "MyQTableWidget.h" MyQTableWidget::MyQTableWidget(QWidget *parent) :QTableWidget(parent) { /******************** 实现表格控件拖拽item交换数据 ********************/ this->setDragDropMode(QAbstractItemView::DragDrop); this->setDragEnabled(true); this->setAcceptDrops(true); //选中一个Item this->setSelectionBehavior(QAbstractItemView::SelectItems); } MyQTableWidget::~MyQTableWidget() { } void MyQTableWidget::mousePressEvent(QMouseEvent *event) { m_spos = event->pos(); sitem = new QTableWidgetItem(); sitem = QTableWidget::itemAt(m_spos); if (sitem == NULL) { return; } temp = sitem->text(); QTableWidget::mousePressEvent(event); } void MyQTableWidget::dropEvent(QDropEvent *event) { m_dpos = event->pos(); ditem = new QTableWidgetItem(); ditem = QTableWidget::itemAt(m_dpos); if (ditem == NULL) { return; } sitem->setText(ditem->text()); ditem->setText(temp); }
四:在 item 里面添加CheckBox控件
1、添加选择功能
QTableWidgetItem* item = new QTableWidgetItem(); item = new QTableWidgetItem(); item->setCheckState(Qt::Unchecked); item->setText(QString::fromLocal8Bit("勾选")); item->setBackgroundColor(QColor(0x00, 0xFF, 0x00)); ui.tableWidget->setItem(row, 3, item);
2、复选框选择事件处理
// 连接信号和槽 connect(ui.tableWidget, SIGNAL(cellClicked(int, int)), this, SLOT(stateChecked(int, int))); // 实现槽函数 void test10292::stateChecked(int row, int column ) { if (column == 3) { QTableWidgetItem* item = ui.tableWidget->item(row, column); if (item != NULL) { if (item->checkState() == Qt::Checked) { // 选中红色 item->setBackgroundColor(QColor(0xFF, 0x00, 0x00)); } else if (item->checkState() == Qt::Unchecked) { // 没选中绿色 item->setBackgroundColor(QColor(0x00, 0xFF, 0x00)); } } } }
五:插入复选框的第二种方法
QCheckBox* cb = new QCheckBox(); cb->setText(QString::fromLocal8Bit("易语言")); ui.tableWidget->setCellWidget(row, 3, cb);
六:其中一个item是CheckBox控件,拖拽实现交换两个item(包括控件交换)
主要是通过 item 的关联数据来实现, 插入数据的时候如果是控件关联数据设置为一个值,不是控件设置为另外一个值。然后在dropEvent()事件函数里面处理。
void test10292::AddData(const QString& id, const QString& name, int score) { int row = ui.tableWidget->rowCount(); ui.tableWidget->insertRow(row); // ID QTableWidgetItem* item = new QTableWidgetItem(); item->setText(id); item->setData(Qt::ToolTip, ""); ui.tableWidget->setItem(row, 0, item); item->setTextAlignment(Qt::AlignHCenter|Qt::AlignVCenter); // name item = new QTableWidgetItem(); item->setText(name); item->setData(Qt::ToolTip, ""); ui.tableWidget->setItem(row, 1, item); item->setTextAlignment(Qt::AlignHCenter|Qt::AlignVCenter); // score item = new QTableWidgetItem(); item->setText(QString::number(score)); item->setData(Qt::ToolTip, ""); ui.tableWidget->setItem(row, 2, item); item->setTextAlignment(Qt::AlignHCenter|Qt::AlignVCenter); // 第四列插入复选框 /*QCheckBox* cb = new QCheckBox(); cb->setText(QString::fromLocal8Bit("易语言")); ui.tableWidget->setCellWidget(row, 3, cb);*/ item = new QTableWidgetItem(); item->setCheckState(Qt::Unchecked); item->setText(QString::fromLocal8Bit("勾选")); item->setBackgroundColor(QColor(0x00, 0xFF, 0x00)); item->setData(Qt::ToolTip, "this is tooltip"); ui.tableWidget->setItem(row, 3, item); }
#include "MyQTableWidget.h" MyQTableWidget::MyQTableWidget(QWidget *parent) :QTableWidget(parent) { /******************** 实现表格控件拖拽item交换数据 ********************/ this->setDragDropMode(QAbstractItemView::DragDrop); this->setDragEnabled(true); this->setAcceptDrops(true); //选中一个Item this->setSelectionBehavior(QAbstractItemView::SelectItems); } MyQTableWidget::~MyQTableWidget() { } void MyQTableWidget::mousePressEvent(QMouseEvent *event) { m_spos = event->pos(); sitem = new QTableWidgetItem(); sitem = QTableWidget::itemAt(m_spos); if (sitem == NULL) { return; } temp = sitem->text(); QTableWidget::mousePressEvent(event); } void MyQTableWidget::dropEvent(QDropEvent *event) { m_dpos = event->pos(); ditem = new QTableWidgetItem(); ditem = QTableWidget::itemAt(m_dpos); if (ditem == NULL) { return; } if (sitem->data(Qt::ToolTip).toString() == "this is tooltip" && ditem->data(Qt::ToolTip).toString() == "") { int r = sitem->row(); int c = sitem->column(); delete sitem; QTableWidgetItem* itemTemp = new QTableWidgetItem(); itemTemp->setText(ditem->text()); itemTemp->setData(Qt::ToolTip, ""); // 文本背景色设置为白色 itemTemp->setBackgroundColor(QColor(0xFF, 0xFF, 0xFF)); itemTemp->setTextAlignment(Qt::AlignHCenter|Qt::AlignVCenter); this->setItem(r, c, itemTemp); ditem->setText(temp); ditem->setCheckState(Qt::Unchecked); ditem->setData(Qt::ToolTip, "this is tooltip"); ditem->setBackgroundColor(QColor(0x00, 0xFF, 0x00)); } else if (sitem->data(Qt::ToolTip).toString() == "this is tooltip" && ditem->data(Qt::ToolTip).toString() == "this is tooltip") { sitem->setText(ditem->text()); ditem->setText(temp); } else if (sitem->data(Qt::ToolTip).toString() == "" && ditem->data(Qt::ToolTip).toString() == "this is tooltip") { sitem->setText(ditem->text()); sitem->setCheckState(Qt::Unchecked); sitem->setData(Qt::ToolTip, "this is tooltip"); sitem->setBackgroundColor(QColor(0x00, 0xFF, 0x00)); int r = ditem->row(); int c = ditem->column(); delete ditem; QTableWidgetItem* itemTemp = new QTableWidgetItem(); itemTemp->setText(temp); itemTemp->setData(Qt::ToolTip, ""); // 文本背景色设置为白色 itemTemp->setBackgroundColor(QColor(0xFF, 0xFF, 0xFF)); itemTemp->setTextAlignment(Qt::AlignHCenter|Qt::AlignVCenter); this->setItem(r, c, itemTemp); } QTableWidget::dropEvent(event); }