QT::TestQFormLayout操作
前言:
本文操作均为在vs2015+QT5.9.5版本中执行
头文件:testqformlayout.h
#pragma once
#include <QtWidgets/QWidget>
#include "ui_testqformlayout.h"
class TestQFormLayout : public QWidget
{
Q_OBJECT
public:
TestQFormLayout(QWidget *parent = Q_NULLPTR);
public slots:
void Seve();
void Clear();
private:
Ui::TestQFormLayoutClass ui;
};
UI文件:ui_testqformlayout.h
#pragma once
#include <QtWidgets/QWidget>
#include "ui_testqformlayout.h"
class TestQFormLayout : public QWidget
{
Q_OBJECT
public:
TestQFormLayout(QWidget *parent = Q_NULLPTR);
public slots:
void Seve();
void Clear();
private:
Ui::TestQFormLayoutClass ui;
};
主函数:main.cpp
#include "testqformlayout.h"
#include <QtWidgets/QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
TestQFormLayout w;
w.show();
return a.exec();
}
实现文件:testqformlayout.cpp
#include "testqformlayout.h"
#include <QFormLayout>
#include <QLineEdit>
#include <QLabel>
#include <QPushButton>
#include <QDebug>
TestQFormLayout::TestQFormLayout(QWidget *parent)
: QWidget(parent)
{
ui.setupUi(this);
//创建一个表单布局
QFormLayout *lay = new QFormLayout(this);
//创建一个QLineEdit添加到lay中
//(PS:"&Name:"其中的&是一个快捷键:ALT+N则是移动到Name这一行)
QLineEdit *name = new QLineEdit();
/*
void addRow(标签名字,控件名);
void addRow(const QString &labelText, QWidget *field);
*/
lay->addRow("&Name:",name);
//创建一个QLineEdit添加到lay中
//(PS:"&Email:"其中的&是一个快捷键:ALT+E则是移动到Email这一行)
QLineEdit *email = new QLineEdit();
lay->addRow("&Email:", email);
//创建一个QLineEdit插入lay中第一行
//(PS:"&Age:"其中的&是一个快捷键:ALT+A则是移动到Age这一行)
QLineEdit *age = new QLineEdit();
/*
void insertRow(行号,标签名字,控件名);
void insertRow(int row,QString &labelText, QWidget *field);
*/
lay->insertRow(1,"&Age:", age);
//创建一个QLabel插入lay中第一行
QLabel *info = new QLabel();
//设置样式表
info->setStyleSheet("color:black;");
lay->insertRow(0, "info", info);
//添加Seve and Clear按钮
QPushButton *seve = new QPushButton("Seve");
QPushButton *clear = new QPushButton("Clear");
//添加平行布局
QHBoxLayout *hlay = new QHBoxLayout();
//将按钮seve添加到布局中
hlay->addWidget(seve);
//将按钮clear添加到布局中
hlay->addWidget(clear);
//将布局(hlay)添加到表单布局(lay)中
lay->addRow(hlay);
//信号与槽链接
connect(seve, SIGNAL(clicked()), this, SLOT(Seve()));//链接保存事件
connect(clear, SIGNAL(clicked()), this, SLOT(Clear()));//链接清除事件
}
void TestQFormLayout::Seve()
{
/*
为什么能获取到layout?
因为一个QWidget只能有一个子类QFormLayout
所以通过this里的layout()函数方法可以获取到子类QFormLayout里所有内容
PS:子类里的孙子节点是无法获取的,需要通过别的方法
*/
//创建一个QFormLayout(lay)来获取layout里所有内容
QFormLayout *lay = (QFormLayout *)this->layout();
//显示错误信息
QLabel *label = (QLabel*)lay->itemAt(0, QFormLayout::LabelRole)->widget();
//存放报错信息
QString error = "";
//存放QLineEdit中的文本信息
QString E_text = "";
//遍历QFormLayout所有行数(rowCount())
for (int i = 0; i < lay->rowCount(); i++)
{
//获取labelText
//先获取到QLayoutItem
QLayoutItem *layItem = lay->itemAt(i, QFormLayout::LabelRole);
if (!layItem)continue;//没有则跳过
//转换获取到QLayoutItem为QLabel类型,然后获取到控件
QLabel *titleLabel = (QLabel*)layItem->widget();
if (!titleLabel)continue;//没有则跳过
//获取控件中taxt,用QString存下来
QString title = titleLabel->text();
//获取QLineEdit
QLayoutItem *item = lay->itemAt(i, QFormLayout::FieldRole);
QLineEdit *e = (QLineEdit*)item->widget();//转换QLayoutItem中的(QLineEdit控件)为QLineEdit类型
if (!e)continue;
QString cname = e->metaObject()->className();//获取控件名字
if (cname == "QLineEdit")
{
//trimmed()去掉前后空格
if (e->text().trimmed() == ""){//判断QLineEdit里面是否有内容
e->setFocus();//将光标移动到没有编辑的
error += title;//获取标题名字
error += "is empty\n";//提示QLineEdit里没有内容
}
else {//获取内容到E_text
E_text += e->text();//获取QLineEdit里的文本信息,并拼接
E_text += "\n";//换行处理
}
}
}
//修改QLabel里的text
label->setText(E_text + error);
}
void TestQFormLayout::Clear()
{
//创建一个QFormLayout(lay)来获取layout里所有内容
QFormLayout *lay = (QFormLayout *)this->layout();
//遍历所有的QLineEdit
//遍历QFormLayout所有元素
for (int i = 0; i < lay->rowCount(); i++)
{
//获取QLineEdit
QLayoutItem *item = lay->itemAt(i, QFormLayout::FieldRole);
QLineEdit *e = (QLineEdit*)item->widget();//转换QLayoutItem中的(QLineEdit控件)为QLineEdit类型
if (!e)continue;//转换失败的跳过
QString cname = e->metaObject()->className();//获取控件类型名字
if (cname == "QLineEdit")//判断是否为QLineEdit类型控件
{
e->setText("");//清空内容
}
}
}