注意:这只是一个demo,项目中用到利用QTableWidget创建表,表中包含各种控件,最后保存表中的内容到doc中,此demo完成了QTableWidget中表的创建,以及将表中内容转为字符串格式输出,字符串便于存储为doc。

关键点:1、读取列表中不同控件的内容,并转为字符串。首先获取QWidget类型,用QWidget->children().at(x)找到你这个QWidget中实际控件的位置,注意x的取值取决于你创建表的时候,往QWidget里边放置的内容顺序。

              2、然后你要获取的控件是什么类型你就把1中获取的QWidget强转成什么类型,如:QDateEdit *dataedit=(QDateEdit*)(widget->children().at(1));

此时就可以将获取的控件转为字符串了。

基本功能介绍:

 

demo里创建了两个QTableWidget控件,左侧我测试添加单一类型QLineEdit,点击add即增加一行,点击save即输出列表内容,也就是以字符串的形式获取QTableWidget表中的内容(方便输出为doc文件)。

右侧我测试添加各种类型的数据,包括QDateEdit QComboBox等类型,同样点击增加增加一行,点击保存即输出表中内容字符串。

点击“add”或“增加”:

 

点击“save”或“保存”:

 

 源码:

mymainwindow.h
 1 #ifndef MYMAINWINDOW_H
 2 #define MYMAINWINDOW_H
 3 
 4 #include <QMainWindow>
 5 #include <QComboBox>
 6 #include <QSpinBox>
 7 #include <QDateEdit>
 8 #include <QDate>
 9 #include<QDebug>
10 #include<QLineEdit>
11 namespace Ui {
12 class MyMainWindow;
13 }
14 
15 class MyMainWindow : public QMainWindow
16 {
17     Q_OBJECT
18 
19 public:
20     explicit MyMainWindow(QWidget *parent = 0);
21     ~MyMainWindow();
22 
23 private slots:
24     void on_pushButton_add_clicked();
25 
26     void on_pushButton_save_clicked();
27 
28     void on_pushButton_add2_clicked();
29 
30     void on_pushButton_save2_clicked();
31 
32 private:
33     Ui::MyMainWindow *ui;
34 void setplainWidget(int rowIndex, int colIndex);
35     void setDateWidget(int rowIndex, int colIndex);
36     void setComboxWidget(int rowIndex, int colIndex, QStringList& lstStr);
37     void setSpinBox(int rowIndex, int colIndex);
38 };
39 
40 #endif // MYMAINWINDOW_H
mymainwindow.cpp
#include "mymainwindow.h"
#include "ui_mymainwindow.h"

MyMainWindow::MyMainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MyMainWindow)
{
    ui->setupUi(this);
}

MyMainWindow::~MyMainWindow()
{
    delete ui;
}
void MyMainWindow::setDateWidget(int rowIndex, int colIndex)
{
    QWidget *widget;
    QHBoxLayout *hLayout;
    QDateEdit *dateEdit;
    dateEdit = new QDateEdit();
    dateEdit->setObjectName("qdateedit");
    dateEdit->setAlignment(Qt::AlignHCenter);
    dateEdit->setDisplayFormat("yyyy-MM-dd");
    dateEdit->setDate(QDate::currentDate());
    dateEdit->setCalendarPopup(true);
    hLayout = new QHBoxLayout();
    widget = new QWidget(ui->taskTableWidget);
    hLayout->addWidget(dateEdit);
    hLayout->setMargin(0);
    widget->setLayout(hLayout);
    ui->taskTableWidget->setCellWidget(rowIndex, colIndex, widget);
}


void MyMainWindow::setComboxWidget(int rowIndex, int colIndex, QStringList& lstStr)
{
    QWidget *widget;
    QHBoxLayout *hLayout;
    QComboBox *cmb;
    cmb = new QComboBox();
    cmb->setObjectName("qcombobox");
    cmb->addItems(lstStr);
    hLayout = new QHBoxLayout();
    widget = new QWidget(ui->taskTableWidget);
    hLayout->addWidget(cmb);
    hLayout->setMargin(0);
    widget->setLayout(hLayout);
    ui->taskTableWidget->setCellWidget(rowIndex, colIndex, widget);
}

void MyMainWindow::setSpinBox(int rowIndex, int colIndex)
{
    QWidget *widget;
    QHBoxLayout *hLayout;
    QSpinBox *spinbox;
    spinbox = new QSpinBox();
    spinbox->setObjectName("qspinbox");
    spinbox->setMinimum(0);
    spinbox->setMaximum(999999999);
    spinbox->setSingleStep(1);
    hLayout = new QHBoxLayout();
    widget = new QWidget(ui->taskTableWidget);
    hLayout->addWidget(spinbox);
    hLayout->setMargin(0);
    widget->setLayout(hLayout);
    ui->taskTableWidget->setCellWidget(rowIndex, colIndex, widget);
}
void MyMainWindow::setplainWidget(int rowIndex, int colIndex)
{
    QWidget *widget;
    QLineEdit *line;
    line = new QLineEdit();
    line->setText("lstStr");
    widget = new QWidget(ui->taskTableWidget2);
    ui->taskTableWidget2->setCellWidget(rowIndex, colIndex, line);
}
void MyMainWindow::on_pushButton_add_clicked()
{
    int rowIndex = ui->taskTableWidget->rowCount();
    ui->taskTableWidget->setRowCount(rowIndex + 1);//总行数增加1
    //资料开始时间
    setDateWidget(rowIndex, 0);
    //资料结束时间
    setDateWidget(rowIndex, 1);
    QStringList lstStrYears;
    for (int i = 0; i <= 15; i++)
    {
        QDateTime curDateTime = QDateTime::currentDateTime();
        int curYear = curDateTime.date().year();
        lstStrYears.push_back(QString::number(curYear + i));
    }
    lstStrYears.push_front("");
    setComboxWidget(rowIndex, 2, lstStrYears);
    //涉外情况
    QStringList lstStrForegion;
    lstStrForegion.push_back(QString::fromLocal8Bit(""));
    lstStrForegion.push_back(QString::fromLocal8Bit(""));
    lstStrForegion.push_back(QString::fromLocal8Bit(""));
    setComboxWidget(rowIndex, 3, lstStrForegion);
    //合同
    setComboxWidget(rowIndex,4, lstStrForegion);
    //使行列头自适应宽度,最后一列将会填充空白部分
    ui->taskTableWidget->horizontalHeader()->setStretchLastSection(true);
    ui->taskTableWidget->resizeColumnsToContents();
}

void MyMainWindow::on_pushButton_save_clicked()
{
    int row = ui->taskTableWidget->rowCount();
    int column = ui->taskTableWidget->columnCount();
    QString msg;
    for(int i=0;i<row;++i)
    {
        for(int j=0;j<column;++j)
        {
            //第一个日期类型
            QWidget *widget=ui->taskTableWidget->cellWidget(i,j);
//            qDebug()<<widget->children().at(1);
            if ((widget->children().at(1))->inherits("QDateEdit"))
            {
                QDateEdit *dataedit=(QDateEdit*)(widget->children().at(1));
                msg=msg.append(dataedit->date().toString("yyyy-MM-dd"))+" ";
            }
            //第二个QComboBox类型
            else if((widget->children().at(1))->inherits("QComboBox"))
            {
                QComboBox *combox=(QComboBox*)(widget->children().at(1));
                msg=msg.append(combox->currentText())+" ";
            }
        }
    }

    qDebug()<<msg<<"\n";
}

void MyMainWindow::on_pushButton_add2_clicked()
{
    int rowIndex = ui->taskTableWidget2->rowCount();
    ui->taskTableWidget2->setRowCount(rowIndex + 1);//总行数增加1
    setplainWidget(rowIndex,0);
    setplainWidget(rowIndex,1);
    setplainWidget(rowIndex,2);
    setplainWidget(rowIndex,3);
    setplainWidget(rowIndex,4);
    //使行列头自适应宽度,最后一列将会填充空白部分
    ui->taskTableWidget2->horizontalHeader()->setStretchLastSection(true);
    ui->taskTableWidget2->resizeColumnsToContents();
}

void MyMainWindow::on_pushButton_save2_clicked()
{
    int row = ui->taskTableWidget2->rowCount();
    int column = ui->taskTableWidget2->columnCount();
    QString msg;
    for (int i = 0; i<row; i++)
    {
        for (int j = 0; j<column; j++)
        {

            QWidget *widget=ui->taskTableWidget2->cellWidget(i,j);
            QLineEdit *combox=(QLineEdit*)widget;
            msg=msg.append(combox->text())+" ";

            //            QString str =ui->taskTableWidget2->item(i, j)->data(Qt::DisplayRole).toString();
        }
    }
    qDebug()<<msg;
}

 

如有疑惑可进行沟通指教。

 

版权声明:本文为rswss原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/rswss/p/12340418.html