qt 表格类QTableWidget实现表格操作

需求是有一堆数据(非数据库,如果是数据库可以用其他的)需要显示出来,并支持修改。

1、新增行;

2、新增列;

3、修改单个值;

4、获得总行数和总列数;

5、获得选中的坐标和值。

比较喜欢先用creator先画一个大概,全部用代码实现也可以,但是新手。。

实现效果(暂时):

qt 表格类QTableWidget实现表格操作_第1张图片

以下的关键代码:

声明:

   1:  
   2: public slots:
   3:     void getTotalColumn();
   4:     void getTotalRow();
   5:     void getCurrentRow(QTableWidgetItem* item);
   6:     void getCurrentColumn(QTableWidgetItem* item);
   7:     void getCurrentValue(QTableWidgetItem* item);
   8:     void getCurrentItem(QTableWidgetItem* item);
   9:     void addRow();
  10:     void addColumn();
  11:     void delRow();
  12:     void delColumn();
  13: };

 

信号槽(参数表中并不需要带上形参):

   1: connect(ui->pushButton_total, SIGNAL(clicked(bool)), this, SLOT(getTotalColumn()));
   2: connect(ui->pushButton_total, SIGNAL(clicked(bool)), this, SLOT(getTotalRow()));
   3: connect(ui->tableWidget, SIGNAL(itemChanged(QTableWidgetItem*)), this, SLOT(getCurrentItem(QTableWidgetItem*)));
   4: connect(ui->pushButton_addRow, SIGNAL(clicked(bool)), this, SLOT(addRow()));
   5: connect(ui->pushButton_addColumn, SIGNAL(clicked(bool)), this, SLOT(addColumn()));
   6: connect(ui->pushButton_delRow, SIGNAL(clicked(bool)), this, SLOT(delRow()));
   7: connect(ui->pushButton_delColumn, SIGNAL(clicked(bool)), this, SLOT(delColumn()));
 
实现:
   1:  
   2: void MainWindow::getTotalColumn(){
   3:     QString totalColumn = QString::number(ui->tableWidget->columnCount(), 10);
   4:     ui->lineEdit_totalColumn->setText(totalColumn);
   5:     qDebug() << "total column::" << totalColumn;
   6: }
   7:  
   8: void MainWindow::getTotalRow(){
   9:     QString totalRow = QString::number(ui->tableWidget->rowCount(), 10);
  10:     ui->lineEdit_totalRow->setText(totalRow);
  11:     qDebug() << "total row::" << totalRow;
  12: }
  13:  
  14: void MainWindow::getCurrentItem(QTableWidgetItem* item){
  15:     getCurrentColumn(item);
  16:     getCurrentRow(item);
  17:     getCurrentValue(item);
  18: }
  19:  
  20: void MainWindow::getCurrentRow(QTableWidgetItem* item){
  21:     QString currentRow = QString::number(item->row(), 10);
  22:     ui->lineEdit_CurrentRow->setText(currentRow);
  23:     qDebug() << "current row::" << currentRow;
  24: }
  25:  
  26: void MainWindow::getCurrentColumn(QTableWidgetItem *item){
  27:     QString currentColumn = QString::number(item->column(), 10);
  28:     ui->lineEdit_CurrentColumn->setText(currentColumn);
  29:     qDebug() << "current column::" << currentColumn;
  30: }
  31:  
  32: void MainWindow::getCurrentValue(QTableWidgetItem *item){
  33:     ui->lineEdit_CurrentValue->setText(item->text());
  34:     qDebug() << "current value::" << item->text();
  35: }
  36:  
  37: void MainWindow::addRow(){
  38:     int totalRow = ui->tableWidget->rowCount();
  39:     //int totalColumn = ui->tableWidget->columnCount();
  40:  
  41:     ui->tableWidget->setRowCount(totalRow + 1);
  42:  
  43:     // 默认自增
  44:     //QTableWidgetItem *newHead = new QTableWidgetItem(QString("%1").arg(totalRow+1));
  45:     //ui->tableWidget->setVerticalHeaderItem(totalRow, newHead);
  46:  
  47:     /*
  48:     QTableWidgetItem *item = new QTableWidgetItem;
  49:     //item->setText(QString("%1").arg(5));
  50:     for(int i = 0; i < totalColumn; i++){
  51:         ui->tableWidget->setItem(totalRow, i, item);
  52:     }*/
  53: }
  54:  
  55: void MainWindow::addColumn(){
  56:     int totalColumn = ui->tableWidget->columnCount();
  57:     ui->tableWidget->setColumnCount(totalColumn + 1);
  58: }
  59:  
  60: void MainWindow::delRow(){
  61:     ui->tableWidget->removeRow(ui->tableWidget->currentRow());
  62: }
  63:  
  64: void MainWindow::delColumn(){
  65:     ui->tableWidget->removeColumn(ui->tableWidget->currentColumn());
  66: }

代码地址:https://coding.net/u/htzy/p/testTable/git

别问我为什么不是osc。。。求放过。。

下面为项目建立过程,仅供新手看:

新建项目:

qt 表格类QTableWidget实现表格操作_第2张图片

填一个项目名:

qt 表格类QTableWidget实现表格操作_第3张图片

qt 表格类QTableWidget实现表格操作_第4张图片

qt 表格类QTableWidget实现表格操作_第5张图片

qt 表格类QTableWidget实现表格操作_第6张图片

新建项目完毕:

qt 表格类QTableWidget实现表格操作_第7张图片

打开mainwindow.ui

设计并完成命名(命名按自己习惯):

qt 表格类QTableWidget实现表格操作_第8张图片

在头文件mainwindow.h中新增槽函数

   1: #ifndef MAINWINDOW_H
   2: #define MAINWINDOW_H
   3:  
   4: #include <QMainWindow>
   5: #include <QMessageBox>
   6: #include <QDebug>
   7: #include <QTableWidgetItem>
   8:  
   9: namespace Ui {
  10: class MainWindow;
  11: }
  12:  
  13: class MainWindow : public QMainWindow
  14: {
  15:     Q_OBJECT
  16:  
  17: public:
  18:     explicit MainWindow(QWidget *parent = 0);
  19:     ~MainWindow();
  20:  
  21: private:
  22:     Ui::MainWindow *ui;
  23:  
  24: public slots:
  25:     void getTotalColumn();
  26:     void getTotalRow();
  27:     void getCurrentRow(QTableWidgetItem* item);
  28:     void getCurrentColumn(QTableWidgetItem* item);
  29:     void getCurrentValue(QTableWidgetItem* item);
  30:     void getCurrentItem(QTableWidgetItem* item);
  31:     void addRow();
  32:     void addColumn();
  33:     void delRow();
  34:     void delColumn();
  35: };
  36:  
  37: #endif // MAINWINDOW_H

在mainwindow.cpp中定义:

   1: #include "mainwindow.h"
   2: #include "ui_mainwindow.h"
   3:  
   4: MainWindow::MainWindow(QWidget *parent) :
   5:     QMainWindow(parent),
   6:     ui(new Ui::MainWindow){
   7:     ui->setupUi(this);
   8:     connect(ui->pushButton_total, SIGNAL(clicked(bool)), this, SLOT(getTotalColumn()));
   9:     connect(ui->pushButton_total, SIGNAL(clicked(bool)), this, SLOT(getTotalRow()));
  10:     connect(ui->tableWidget, SIGNAL(itemChanged(QTableWidgetItem*)), this, SLOT(getCurrentItem(QTableWidgetItem*)));
  11:     connect(ui->pushButton_addRow, SIGNAL(clicked(bool)), this, SLOT(addRow()));
  12:     connect(ui->pushButton_addColumn, SIGNAL(clicked(bool)), this, SLOT(addColumn()));
  13:     connect(ui->pushButton_delRow, SIGNAL(clicked(bool)), this, SLOT(delRow()));
  14:     connect(ui->pushButton_delColumn, SIGNAL(clicked(bool)), this, SLOT(delColumn()));
  15: }
  16:  
  17: MainWindow::~MainWindow(){
  18:     delete ui;
  19: }
  20: // 获得总列数
  21: void MainWindow::getTotalColumn(){
  22:     // 将int按十进制转成QString
  23:     QString totalColumn = QString::number(ui->tableWidget->columnCount(), 10);
  24:     ui->lineEdit_totalColumn->setText(totalColumn);
  25:     qDebug() << "total column::" << totalColumn;
  26: }
  27: // 获得总行数
  28: void MainWindow::getTotalRow(){
  29:     QString totalRow = QString::number(ui->tableWidget->rowCount(), 10);
  30:     ui->lineEdit_totalRow->setText(totalRow);
  31:     qDebug() << "total row::" << totalRow;
  32: }
  33:  
  34: // 当某一项修改时(包括新增时),获得当前项
  35: void MainWindow::getCurrentItem(QTableWidgetItem* item){
  36:     getCurrentColumn(item);
  37:     getCurrentRow(item);
  38:     getCurrentValue(item);
  39: }
  40: // 获得当前行
  41: void MainWindow::getCurrentRow(QTableWidgetItem* item){
  42:     QString currentRow = QString::number(item->row(), 10);
  43:     ui->lineEdit_CurrentRow->setText(currentRow);
  44:     qDebug() << "current row::" << currentRow;
  45: }
  46: // 获得当前列
  47: void MainWindow::getCurrentColumn(QTableWidgetItem *item){
  48:     QString currentColumn = QString::number(item->column(), 10);
  49:     ui->lineEdit_CurrentColumn->setText(currentColumn);
  50:     qDebug() << "current column::" << currentColumn;
  51: }
  52: // 获得当前值
  53: void MainWindow::getCurrentValue(QTableWidgetItem *item){
  54:     ui->lineEdit_CurrentValue->setText(item->text());
  55:     qDebug() << "current value::" << item->text();
  56: }
  57:  
  58: // 增加一行
  59: void MainWindow::addRow(){
  60:     int totalRow = ui->tableWidget->rowCount();
  61:     //int totalColumn = ui->tableWidget->columnCount();
  62:  
  63:     ui->tableWidget->setRowCount(totalRow + 1);
  64:  
  65:     // 默认自增
  66:     //QTableWidgetItem *newHead = new QTableWidgetItem(QString("%1").arg(totalRow+1));
  67:     //ui->tableWidget->setVerticalHeaderItem(totalRow, newHead);
  68:  
  69:     /*
  70:     QTableWidgetItem *item = new QTableWidgetItem;
  71:     //item->setText(QString("%1").arg(5));
  72:     for(int i = 0; i < totalColumn; i++){
  73:         ui->tableWidget->setItem(totalRow, i, item);
  74:     }*/
  75: }
  76:  
  77: // 增加一列
  78: void MainWindow::addColumn(){
  79:     int totalColumn = ui->tableWidget->columnCount();
  80:     ui->tableWidget->setColumnCount(totalColumn + 1);
  81: }
  82:  
  83: // 删除选中的行(一定要选中并且总列数大于0)
  84: void MainWindow::delRow(){
  85:     ui->tableWidget->removeRow(ui->tableWidget->currentRow());
  86: }
  87:  
  88: // 删除选中的列(一定要选中并且总行数大于0)
  89: void MainWindow::delColumn(){
  90:     ui->tableWidget->removeColumn(ui->tableWidget->currentColumn());
  91: }

你可能感兴趣的:(qt 表格类QTableWidget实现表格操作)