#ifndef TABLEWIDGET_H
#define TABLEWIDGET_H
#include <QDialog>
#include <QTableWidget>
#include <QHeaderView>
#include <QPushButton>
#include <QVBoxLayout>
#include <QComboBox>
#include <QLabel>
#include <QFrame>
#include <QGroupBox>
#include <QDebug>
class Table : public QTableWidget{
Q_OBJECT
public:
explicit Table(Qt::Orientation ori, QStringList labels, QWidget *parent = nullptr);
~Table();
public:
void clearItems();
bool addItems(int itemCount);
bool addItem();
bool delItem();
signals:
public slots:
private:
void setupUi();
Qt::Orientation mOri;
};
class TableFrame : public QFrame{
Q_OBJECT
public:
explicit TableFrame(Qt::Orientation ori, QStringList labels, QWidget *parent = nullptr);
~TableFrame();
void addItems(QStringList &items);
void addItem(QString &item);
signals:
public slots:
private:
void setupUi(Qt::Orientation ori, QStringList &labels);
QPushButton *mAddButton;
QPushButton *mDelButton;
QComboBox *mTypeCbx;
Table *mTable;
Qt::Orientation mOri;
};
class TableWidget : public QDialog
{
Q_OBJECT
public:
explicit TableWidget(QWidget *parent = nullptr);
~TableWidget();
signals:
public slots:
};
#endif // TABLEWIDGET_H
//源文件
#include "tablewidget.h"
#pragma execution_character_set("utf-8")
Table::Table(Qt::Orientation ori, QStringList labels, QWidget *parent)
: QTableWidget(parent)
, mOri(ori)
{
switch (mOri) {
case Qt::Horizontal:
{
setColumnCount(labels.size());
setHorizontalHeaderLabels(labels);
}
break;
case Qt::Vertical:
{
setRowCount(labels.size());
setVerticalHeaderLabels(labels);
}
break;
default:
break;
}
setupUi();
}
Table::~Table()
{
}
void Table::clearItems()
{
clearContents();
viewport()->update();
}
bool Table::addItems(int itemCount)
{
if(mOri == Qt::Horizontal) {
setRowCount(itemCount);
return true;
}else {
setColumnCount(itemCount);
return true;
}
return false;
}
bool Table::addItem()
{
switch (mOri) {
case Qt::Horizontal:
{
int rowCnt = this->rowCount();
if(rowCnt < 0) {return false;}
this->insertRow(rowCnt);
}
break;
case Qt::Vertical:
{
int colCnt = this->columnCount();
if(colCnt < 0) {return false;}
this->insertColumn(colCnt);
}
break;
}
return true;
}
bool Table::delItem()
{
switch (mOri) {
case Qt::Horizontal:
{
int rowCnt = this->rowCount();
if(rowCnt <= 0) {return false;}
this->removeRow(rowCnt - 1);
}
break;
case Qt::Vertical:
{
int colCnt = this->columnCount();
if(colCnt <= 0) {return false;}
this->removeColumn(colCnt - 1);
}
break;
}
return true;
}
void Table::setupUi()
{
this->horizontalHeader()->setStretchLastSection(true); //最后一列铺满
this->horizontalHeader()->setDefaultSectionSize(90);
this->horizontalHeader()->setMinimumSectionSize(80);
}
TableFrame::TableFrame(Qt::Orientation ori, QStringList labels, QWidget *parent)
: QFrame(parent)
, mOri(ori)
{
setupUi(mOri, labels);
connect(mAddButton, &QPushButton::clicked, this, [=]() {
mTable->addItem();
});
connect(mDelButton, &QPushButton::clicked, this, [=]() {
mTable->delItem();
});
connect(mTypeCbx, &QComboBox::currentTextChanged, this, [=](QString text){
int itemCount = text.toInt();
mTable->clearItems();
mTable->addItems(itemCount);
});
}
TableFrame::~TableFrame()
{
}
void TableFrame::addItems(QStringList &items)
{
mTypeCbx->addItems(items);
}
void TableFrame::addItem(QString &item)
{
mTypeCbx->addItem(item);
}
void TableFrame::setupUi(Qt::Orientation ori, QStringList &labels)
{
QLabel *typeLbl = new QLabel(tr("类型:"), this);
mTypeCbx = new QComboBox(this);
mAddButton = new QPushButton(tr("添加"), this);
mDelButton = new QPushButton(tr("删除"), this);
QBoxLayout *btnLayout = new QHBoxLayout;
btnLayout->addWidget(typeLbl);
btnLayout->addWidget(mTypeCbx);
btnLayout->addStretch();
btnLayout->addWidget(mAddButton);
btnLayout->addWidget(mDelButton);
mTable = new Table(ori, labels, this);
QBoxLayout *mainLayout = new QVBoxLayout;
mainLayout->addLayout(btnLayout);
mainLayout->addWidget(mTable);
setLayout(mainLayout);
}
TableWidget::TableWidget(QWidget *parent)
: QDialog(parent)
{
setWindowTitle(tr("表格组件1"));
resize(1800,500);
QStringList labels;
labels <<tr("上限")<<tr("典型")<<tr("下限");
TableFrame *tableFrm1 = new TableFrame(Qt::Horizontal, labels, this);
QBoxLayout *layou1 = new QVBoxLayout;
layou1->addWidget(tableFrm1);
QStringList items1;
items1<<tr("2")<<tr("4")<<tr("1")<<tr("3")<<tr("5");
tableFrm1->addItems(items1);
TableFrame *tableFrm2 = new TableFrame(Qt::Horizontal, labels, this);
QBoxLayout *layou2 = new QVBoxLayout;
layou2->addWidget(tableFrm2);
QStringList items2;
items2<<tr("2")<<tr("4")<<tr("1")<<tr("3")<<tr("5");
tableFrm2->addItems(items2);
QStringList labels1;
labels1 <<tr("上限")<<tr("典型")<<tr("下限")<<tr("对应关系");
TableFrame *tableFrm3 = new TableFrame(Qt::Horizontal, labels1, this);
QBoxLayout *layou3 = new QVBoxLayout;
layou3->addWidget(tableFrm3);
QStringList items3;
items3<<tr("2")<<tr("4")<<tr("1")<<tr("3")<<tr("5");
tableFrm3->addItems(items3);
TableFrame *tableFrm4 = new TableFrame(Qt::Vertical, labels, this);
QBoxLayout *layou4 = new QVBoxLayout;
layou4->addWidget(tableFrm4);
QStringList items4;
items4<<tr("2")<<tr("4")<<tr("1")<<tr("3")<<tr("5");
tableFrm4->addItems(items4);
QGroupBox *group1 = new QGroupBox(tr("组1"), this);
group1->setLayout(layou1);
QGroupBox *group2 = new QGroupBox(tr("组2"), this);
group2->setLayout(layou2);
QGroupBox *group3 = new QGroupBox(tr("组3"), this);
group3->setLayout(layou3);
QGroupBox *group4 = new QGroupBox(tr("组4"), this);
group4->setLayout(layou4);
QBoxLayout *mainlayout = new QHBoxLayout;
mainlayout->addWidget(group1);
mainlayout->addWidget(group2);
mainlayout->addWidget(group3);
mainlayout->addWidget(group4);
setLayout(mainlayout);
}
TableWidget::~TableWidget()
{
}
07-28
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交