image.png

1 代码

  1. Widget::Widget(QWidget *parent)
  2. : QWidget(parent)
  3. , ui(new Ui::Widget)
  4. {
  5. ui->setupUi(this);
  6. //----------------设置列数---------------//
  7. QStringList list;
  8. list << "姓名" << "性别"<< "年龄";
  9. ui->tableWidget->setColumnCount(list.size());
  10. //----------------设置水平头-------------//
  11. ui->tableWidget->setHorizontalHeaderLabels(list);
  12. //----------------设置行数--------------//
  13. ui->tableWidget->setRowCount(5);
  14. //----------------设置正文--------------//
  15. QStringList nameList,sexList;
  16. nameList << "吕蒙" << "东方曜" << "东方镜" << "蒙犽" << "李白";
  17. sexList << "男" << "男" << "女" << "男" << "男";
  18. for (int i=0;i<5;i++) {
  19. int col = 0;
  20. ui->tableWidget->setItem(i,col++,new QTableWidgetItem(nameList[i]));
  21. ui->tableWidget->setItem(i,col++,new QTableWidgetItem(sexList[i]));
  22. ui->tableWidget->setItem(i,col++,new QTableWidgetItem(QString::number(i+18))); // int转QString
  23. }
  24. // 点击按钮 添加赵云
  25. connect(ui->BTN_Add,&QPushButton::clicked,[=](){
  26. // 先判断有没有 赵云
  27. bool isEmpty = ui->tableWidget->findItems("赵云",Qt::MatchExactly).empty();
  28. if(isEmpty) {
  29. ui->tableWidget->insertRow(0);
  30. ui->tableWidget->setItem(0,0,new QTableWidgetItem("赵云"));
  31. ui->tableWidget->setItem(0,1,new QTableWidgetItem("男"));
  32. ui->tableWidget->setItem(0,2,new QTableWidgetItem("18"));
  33. }
  34. else {
  35. QMessageBox::warning(this,"警告","赵云有了");
  36. }
  37. });
  38. // 点击按钮 删除赵云
  39. connect(ui->BTN_Del,&QPushButton::clicked,[=](){
  40. bool isEmpty = ui->tableWidget->findItems("赵云",Qt::MatchExactly).empty();
  41. if(isEmpty) {
  42. QMessageBox::warning(this,"警告","赵云没有");
  43. }
  44. else {
  45. int row = ui->tableWidget->findItems("赵云",Qt::MatchExactly).first()->row();
  46. ui->tableWidget->removeRow(row);
  47. }
  48. });
  49. }

2 相关信号槽

(1)连接表头的信号和槽

  1. connect(horizontalHeader(), SIGNAL(sectionClicked(int)), this, SLOT(onHeaderClicked(int)));
  2. void TableWidget::onHeaderClicked(int column) {
  3. //column为所点击的表头的某列
  4. }

3 表格右键菜单

(1)创建菜单、菜单项

  1. void TableWidget::createActions()
  2. {
  3. //创建菜单项
  4. pop_menu = new QMenu();
  5. action_name = new QAction(this);
  6. action_size = new QAction(this);
  7. action_type = new QAction(this);
  8. action_date = new QAction(this);
  9. action_open = new QAction(this);
  10. action_download = new QAction(this);
  11. action_flush = new QAction(this);
  12. action_delete = new QAction(this);
  13. action_rename = new QAction(this);
  14. action_create_folder = new QAction(this);
  15. action_open->setText(QString("打开"));
  16. action_download->setText(QString("下载"));
  17. action_flush->setText(QString("刷新"));
  18. action_delete->setText(QString("删除"));
  19. action_rename->setText(QString("重命名"));
  20. action_create_folder->setText(QString("新建文件夹"));
  21. action_name->setText(QString("名称"));
  22. action_size->setText(QString("大小"));
  23. action_type->setText(QString("项目类型"));
  24. action_date->setText(QString("修改日期"));
  25. //设置快捷键
  26. action_flush->setShortcut(QKeySequence::Refresh);
  27. //设置文件夹图标
  28. action_create_folder->setIcon(icon);
  29. QObject::connect(action_create_folder, SIGNAL(triggered()), this, SLOT(createFolder()));
  30. }

(2)重新实现contextMenuEvent

  1. void TableWidget::contextMenuEvent(QContextMenuEvent *event)
  2. {
  3. pop_menu->clear(); //清除原有菜单
  4. QPoint point = event->pos(); //得到窗口坐标
  5. QTableWidgetItem *item = this->itemAt(point);
  6. if(item != NULL)
  7. {
  8. pop_menu->addAction(action_download);
  9. pop_menu->addAction(action_flush);
  10. pop_menu->addSeparator();
  11. pop_menu->addAction(action_delete);
  12. pop_menu->addAction(action_rename);
  13. pop_menu->addSeparator();
  14. pop_menu->addAction(action_create_folder);
  15. sort_style = pop_menu->addMenu("排序");
  16. sort_style->addAction(action_name);
  17. sort_style->addAction(action_size);
  18. sort_style->addAction(action_type);
  19. sort_style->addAction(action_date);
  20. //菜单出现的位置为当前鼠标的位置
  21. pop_menu->exec(QCursor::pos());
  22. event->accept();
  23. }
  24. }

使用 QTableWidget 时会发现,当外部代码去更新表中的数据,却发现界面没有自动更新。
即使在更新数据之后添加了ui->tableWidget->repaint();或者ui->tableWidget->update(),界面依然没有自动更新。
只有当鼠标点击界面时,数据才被刷新。
update()是更新这个控件
viewport()->update()是更新这个控件里面的内容
所以要修改为ui->tbeConsole->viewport()->update();