主要讲的是 QPair 和 QList的嵌套使用.
mywidget.h
#ifndef MYWIDGET_H
#define MYWIDGET_H
#include
#include
#include
#include
namespace Ui {
class MyWidget;
}
typedef QPair Data; //((坐标X,坐标Y),值)
typedef QList DataList;
typedef QList
mywidget.cpp
#include "mywidget.h"
#include "ui_mywidget.h"
#include
MyWidget::MyWidget(QWidget *parent) :
QWidget(parent),
ui(new Ui::MyWidget)
{
ui->setupUi(this);
setWindowFlags(Qt::Dialog|Qt::WindowCloseButtonHint); //去掉窗口问号
this->setWindowTitle(QString::fromLocal8Bit("评价大雄"));
QStringList header;
header<tableWidget,m_data,header);
}
MyWidget::~MyWidget()
{
delete ui;
}
void MyWidget::createData(DataTable &data)
{
data.clear();
DataList row1;
row1<clear();
table->setColumnCount(colCount);
table->setRowCount(rowCount);
table->horizontalHeader()->setDefaultSectionSize(100);
table->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch); //列宽自适应
table->verticalHeader()->setSectionResizeMode(QHeaderView::Stretch); //列宽自适应
table->setShowGrid(true);
table->setSelectionMode(QAbstractItemView::SingleSelection); //单选
table->setSelectionBehavior(QAbstractItemView::SelectRows); //设置选择行为时每次选择一行
table->setEditTriggers(QAbstractItemView::NoEditTriggers); //设置不可编辑
table->setContextMenuPolicy(Qt::CustomContextMenu);
table->setIconSize(QSize(100,25));
table->horizontalHeader()->setStretchLastSection(true); //自动调整最后一列使与table右边界对齐
table->horizontalHeader()->setVisible(false); //水平表头不可见
table->verticalHeader()->setVisible(false); //垂直表头不可见
QMap mapLvToName;
mapLvToName.clear();
mapLvToName[1] = QString::fromLocal8Bit("高");
mapLvToName[2] = QString::fromLocal8Bit("较高");
mapLvToName[3] = QString::fromLocal8Bit("一般");
mapLvToName[4] = QString::fromLocal8Bit("较低");
mapLvToName[5] = QString::fromLocal8Bit("低");
QMap mapGradeToSM;
for(int i = 1;i<=5;++i)
mapLvToName[i] = mapGradeToSM.contains(i)? mapGradeToSM[i] : mapLvToName[i];
table->setItem(2,0,new QTableWidgetItem(mapLvToName[1]));
table->setItem(3,0,new QTableWidgetItem(mapLvToName[2]));
table->setItem(4,0,new QTableWidgetItem(mapLvToName[3]));
table->setItem(5,0,new QTableWidgetItem(mapLvToName[4]));
table->setItem(6,0,new QTableWidgetItem(mapLvToName[5]));
for(int i=2;i<=6;++i)
{
table->item(i,0)->setTextAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
}
mapLvToName.clear();
mapLvToName[1] = QString::fromLocal8Bit("好");
mapLvToName[2] = QString::fromLocal8Bit("较好");
mapLvToName[3] = QString::fromLocal8Bit("一般");
mapLvToName[4] = QString::fromLocal8Bit("较差");
mapLvToName[5] = QString::fromLocal8Bit("差");
mapGradeToSM.clear();
for(int i = 1;i<=5;++i)
mapLvToName[i] = mapGradeToSM.contains(i)? mapGradeToSM[i] : mapLvToName[i];
table->setItem(1,1,new QTableWidgetItem(mapLvToName[1]));
table->setItem(1,2,new QTableWidgetItem(mapLvToName[2]));
table->setItem(1,3,new QTableWidgetItem(mapLvToName[3]));
table->setItem(1,4,new QTableWidgetItem(mapLvToName[4]));
table->setItem(1,5,new QTableWidgetItem(mapLvToName[5]));
for(int i=1;i<=5;++i)
{
table->item(1,i)->setTextAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
}
for(int i = 0 ; i setItem(i+2,k+1,new QTableWidgetItem(stext)); //设置到表格上
table->item(i+2,k+1)->setTextAlignment(Qt::AlignHCenter|Qt::AlignVCenter); //设置相应单元格字体位置居中
}
}
QString cheader = Header.first();
QString rheader= Header.last();
table->setSpan(0, 0, 2, 1);
QTableWidgetItem *item = new QTableWidgetItem;
item->setText(rheader);
QFont f; //设置两个标题的字体
f = item->font();
f.setBold(true);
f.setPixelSize(16);
item->setFont(f);
table->setItem(0,0,item);
table->item(0,0)->setTextAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
table->setSpan(0, 1, 1, 5);
QTableWidgetItem *item1 = new QTableWidgetItem;
item1->setText(cheader);
f = item1->font();
f.setBold(true);
f.setPixelSize(16);
item1->setFont(f);
table->setItem(0,1,item1);
table->item(0,1)->setTextAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
}
bool MyWidget::strat(DataTable data, QString Field1, QString Field2, QString Field3, QStringList mx, QStringList my)
{
QString sql;
QSqlQuery query(m_db_work); //此处与sql链接的代码我省略了...
for(int i = 0 ; i close();
}
效果图 :