环境:Win10-x64+VS2015+Qt5.9.7
首先创建一个Qt GUI Application,工程名为myTableWidget。
一、基本应用
头文件
myTableWidget.h
#pragma once
#include <QtWidgets/QMainWindow>
#include <QTableWidget>
#include <QGridLayout>
#include <QPushButton>
#include "ui_myTableWidget.h"
#pragma execution_character_set("utf-8")
class myTableWidget : public QMainWindow
{
Q_OBJECT
public:
myTableWidget(QWidget *parent = Q_NULLPTR);
private:
Ui::myTableWidgetClass ui;
QTableWidget *m_qTableWidget;
QPushButton *m_qStartPushButton;
QPushButton *m_qStopPushButton;
private slots:
void runPushButtonSlot();
};
源文件
myTableWidget.cpp
#include "myTableWidget.h"
myTableWidget::myTableWidget(QWidget *parent)
: QMainWindow(parent)
{
ui.setupUi(this);
QHBoxLayout *hlayout1 = new QHBoxLayout;
//创建一个TableWidget,并填充内容
QString parameters = "长度,宽度,高度,密度,颜色,比例,数量,距离,半径,周长";
QStringList parameterList = parameters.split(",");
m_qTableWidget = new QTableWidget;
hlayout1->addWidget(m_qTableWidget);
m_qTableWidget->setRowCount(parameterList.size());
m_qTableWidget->setColumnCount(2);
QStringList header;
header << "参数" << "数值";
m_qTableWidget->setHorizontalHeaderLabels(header);
for (size_t i = 0; i < parameterList.size(); i++)
{
m_qTableWidget->setItem(i, 0, new QTableWidgetItem(parameterList.at(i)));
m_qTableWidget->item(i, 0)->setFlags(Qt::NoItemFlags);
m_qTableWidget->item(i, 0)->setTextColor(QColor(0, 0, 255));
m_qTableWidget->setItem(i, 1, new QTableWidgetItem(QString::number(i*i)));
m_qTableWidget->item(i, 1)->setTextColor(QColor(100, 250, 0));
}
m_qTableWidget->setColumnWidth(0, 160);
m_qTableWidget->setColumnWidth(1, 90);
m_qTableWidget->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
m_qTableWidget->verticalHeader()->setVisible(false);//隐藏左边垂直
//下方创建两个按钮控件
QHBoxLayout *hlayout2 = new QHBoxLayout;
m_qStartPushButton = new QPushButton(tr("开始"));
m_qStartPushButton->setStyleSheet("background-color: rgb(0, 255, 127);");
QObject::connect(m_qStartPushButton, SIGNAL(clicked()), this, SLOT(runPushButtonSlot()));
m_qStopPushButton = new QPushButton(tr("停止"));
m_qStopPushButton->setStyleSheet("background-color: rgb(200, 80, 80);");
hlayout2->addWidget(m_qStartPushButton);
hlayout2->addWidget(m_qStopPushButton);
//定义布局结构
QGridLayout *vlayout = new QGridLayout;
vlayout->addLayout(hlayout1, 0, 0);
vlayout->addLayout(hlayout2, 1, 0);
QWidget *widget = new QWidget();
this->setCentralWidget(widget);
centralWidget()->setLayout(vlayout);
}
void myTableWidget::runPushButtonSlot()
{
m_qTableWidget->clear();//清除所有可见数据包括表头
}
效果如图:
点击“开始”后清除表格内容:
二、加载参数列表
需求:现在需要从txt文件中,获取一组参数列表,然后将参数列表映射到TableWidget中。
这里的做法是:先将txt文件中的内容读取出来,放到一个QMap中,然后再将QMap中的Key-Value映射到TableWidget中。
parameters.txt文件内容:
长度:0
宽度:1
高度:4
密度:9
颜色:16
比例:25
数量:36
距离:49
半径:64
周长:91
头文件
myTableWidget.h
#pragma once
#include <QtWidgets/QMainWindow>
#include <QTableWidget>
#include <QGridLayout>
#include <QPushButton>
#include <QFile>
#include <QTextCodec>
#include <QMessageBox>
#include "ui_myTableWidget.h"
#pragma execution_character_set("utf-8")
class myTableWidget : public QMainWindow
{
Q_OBJECT
public:
myTableWidget(QWidget *parent = Q_NULLPTR);
private:
Ui::myTableWidgetClass ui;
QTableWidget *m_qTableWidget;
QPushButton *m_qStartPushButton;
QPushButton *m_qStopPushButton;
QMap<QString, QString> m_qmTxtFileMap;
private slots:
void runPushButtonSlot();
};
源文件
myTableWidget.cpp
#include "myTableWidget.h"
myTableWidget::myTableWidget(QWidget *parent)
: QMainWindow(parent)
{
ui.setupUi(this);
QHBoxLayout *hlayout1 = new QHBoxLayout;
m_qTableWidget = new QTableWidget;
hlayout1->addWidget(m_qTableWidget);
QHBoxLayout *hlayout2 = new QHBoxLayout;
m_qStartPushButton = new QPushButton(tr("开始"));
m_qStartPushButton->setStyleSheet("background-color: rgb(0, 255, 127);");
QObject::connect(m_qStartPushButton, SIGNAL(clicked()), this, SLOT(runPushButtonSlot()));
m_qStopPushButton = new QPushButton(tr("停止"));
m_qStopPushButton->setStyleSheet("background-color: rgb(200, 80, 80);");
hlayout2->addWidget(m_qStartPushButton);
hlayout2->addWidget(m_qStopPushButton);
QGridLayout *vlayout = new QGridLayout;
vlayout->addLayout(hlayout1, 0, 0);
vlayout->addLayout(hlayout2, 1, 0);
QWidget *widget = new QWidget();
this->setCentralWidget(widget);
centralWidget()->setLayout(vlayout);
}
void myTableWidget::runPushButtonSlot()
{
QString filePath = "parameters.txt";
QFile file(filePath);
QStringList m_qlComboBoxList;
//--------------------------将txt文件内参数列表映射到QMap中--------------------------
m_qlComboBoxList.clear();
m_qmTxtFileMap.clear();
if (file.open(QIODevice::ReadOnly | QIODevice::Text))
{
while (!file.atEnd())
{
QTextCodec *codec = QTextCodec::codecForName("GB2312");
QString tmp = codec->toUnicode(file.readLine());
if (tmp.right(1) == "\n")
{
tmp.remove(tmp.length() - 1, tmp.length());
m_qmTxtFileMap.insert(tmp.split(':').at(0), tmp.split(':').at(1));
}
else
{
m_qmTxtFileMap.insert(tmp.split(':').at(0), tmp.split(':').at(1));
}
}
file.close();
}
else
{
QMessageBox::warning(this, tr("加载参数"), tr("参数加载失败!"));
}
//--------------------------将QMap中的参数列表填充到TableWidget--------------------------
if (m_qmTxtFileMap.size() == 0)
{
return;
}
QStringList parameterList;
parameterList.clear();
parameterList = m_qmTxtFileMap.keys();
m_qTableWidget->setRowCount(parameterList.size());
m_qTableWidget->setColumnCount(2);
QStringList header;
header << "参数" << "数值";
m_qTableWidget->setHorizontalHeaderLabels(header);
for (size_t i = 0; i < parameterList.size(); i++)
{
m_qTableWidget->setItem(i, 0, new QTableWidgetItem(parameterList.at(i)));
m_qTableWidget->item(i, 0)->setFlags(Qt::NoItemFlags);
m_qTableWidget->item(i, 0)->setTextColor(QColor(0, 0, 255));
m_qTableWidget->setItem(i, 1, new QTableWidgetItem(m_qmTxtFileMap.find(parameterList.at(i)).value()));
m_qTableWidget->item(i, 1)->setTextColor(QColor(100, 250, 0));
}
m_qTableWidget->setColumnWidth(0, 160);
m_qTableWidget->setColumnWidth(1, 90);
m_qTableWidget->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
m_qTableWidget->verticalHeader()->setVisible(false);//隐藏左边垂直
}
效果:
点击“开始”按钮加载参数列表: