QT学习笔记----day04

若看本节有困难,请先看前面的内容,如下:
day01
day02
day03

七、实例七:布局管理器

最终要实现如下界面效果:
姓名:可填写
性别:可选择,互斥(单选按钮)
身高:可填写,可以手动上加或者下减 (浮点型的分量框)
生日:可选择(有日期选择框)
学历:可选择,下拉列表
爱好:可选择,可多选(多选按钮)
其他的不做赘述
在这里插入图片描述
接下来看看代码:
main.cpp

#include "layoutgui.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    LayoutGui w;
    w.show();

    return a.exec();
}

layoutgui.h

#include "layoutgui.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    LayoutGui w;
    w.show();

    return a.exec();
}

layoutgui.cpp

#include "layoutgui.h"

LayoutGui::LayoutGui(QWidget *parent)
    : QDialog(parent)
{
    this->setWindowTitle("布局管理器示例");

    QLabel *name_label = new QLabel("姓 名");
    QLineEdit *name_edit = new QLineEdit;
//    QString name = name_edit->text();

    QLabel *sex_label = new QLabel("性 别");
    QRadioButton *sex_button_1 = new QRadioButton("男");
    sex_button_1->setCheckable(true);   //可编辑
    sex_button_1->setChecked(true);     //设置选中
    QRadioButton *sex_button_2 = new QRadioButton("女");
    sex_button_2->setCheckable(true);
//    QString sex = "";
//    if(sex_button_1->isChecked())
//        sex = sex_button_1->text();
//    else if(sex_button_2->isChecked())
//        sex = sex_button_2->text();

    //1.单选按钮互斥
    QButtonGroup *sex_group = new QButtonGroup;
    sex_group->addButton(sex_button_1);
    sex_group->addButton(sex_button_2);

    //2.不能直接把两个按钮放入一个单元格,必须放入一个水平布局
    QHBoxLayout *sex_layout = new QHBoxLayout;
    sex_layout->addWidget(sex_button_1);
    sex_layout->addWidget(sex_button_2);

    QLabel *height_label = new QLabel("身 高");
    QDoubleSpinBox *height_box = new QDoubleSpinBox;
    height_box->setRange(100,300);
    height_box->setSingleStep(1);
    height_box->setValue(150);
//    double height = height_box->value();

    QLabel *bday_label = new QLabel("生 日");
    QDateEdit *bday_edit = new QDateEdit;
    bday_edit->setDate(QDate(2000,1,1));
    bday_edit->setDisplayFormat("yyyy-M-d");
    bday_edit->setCalendarPopup(true);
//    QDate date = bday_edit->date();

    QLabel *edurec_label = new QLabel("学 历");
    QComboBox *edurec_box = new QComboBox;
    edurec_box->addItem("大 专");
    edurec_box->addItem("本 科");
    edurec_box->addItem("硕 士");
    edurec_box->addItem("博 士");
//    edurec_box->currentIndex();
//    edurec_box->currentText();

    QLabel *like_label = new QLabel("爱 好");
    QCheckBox *like_box_1 = new QCheckBox("足 球");
    like_box_1->setChecked(true);
    QCheckBox *like_box_2 = new QCheckBox("游 戏");
    QCheckBox *like_box_3 = new QCheckBox("看 书");
    QCheckBox *like_box_4 = new QCheckBox("内 核");
//    QString like = "";
//    if(like_box_1->isChecked())
//        like += "足球";
//    if(like_box_2->isChecked())
//        like += "游戏";

    QHBoxLayout *like_layout = new QHBoxLayout;
    like_layout->addWidget(like_box_1);
    like_layout->addWidget(like_box_2);
    like_layout->addWidget(like_box_3);
    like_layout->addWidget(like_box_4);

    QLabel *dept_label = new QLabel("部门信息");
    QTextEdit *dept_edit = new QTextEdit;
//    QString text = dept_edit->toPlainText();

    QLabel *remark_label = new QLabel("备 注");
    remark_label->setFrameStyle(QFrame::Panel | QFrame::Sunken);
    QGridLayout *left_layout = new QGridLayout;
    left_layout->addWidget(name_label,0,0);
    left_layout->addWidget(name_edit,0,1);

    left_layout->addWidget(sex_label,1,0);
    left_layout->addLayout(sex_layout,1,1);
    left_layout->addWidget(height_label,2,0);
    left_layout->addWidget(height_box,2,1);

    left_layout->addWidget(bday_label,3,0);
    left_layout->addWidget(bday_edit,3,1);
    left_layout->addWidget(edurec_label,4,0);
    left_layout->addWidget(edurec_box,4,1);

    left_layout->addWidget(like_label,5,0);
    left_layout->addLayout(like_layout,5,1);
    left_layout->addWidget(dept_label,6,0);
    left_layout->addWidget(dept_edit,6,1);
    left_layout->addWidget(remark_label,7,0,1,2);

    QLabel *label_1 = new QLabel("头 像");
    head_label = new QLabel;
    head_label->setPixmap(QPixmap("://images/icon.png"));
    QPushButton *head_button = new QPushButton("修改头像");
    this->connect(head_button,SIGNAL(clicked(bool)),this,SLOT(slotChangeHead()));
    QHBoxLayout *head_layout = new QHBoxLayout;
    head_layout->addWidget(label_1);
    head_layout->addStretch();
    head_layout->addWidget(head_label);
    head_layout->addStretch();
    head_layout->addWidget(head_button);

    QLabel *indiv_label = new QLabel("个人详细信息");
    QTextEdit *indiv_edit = new QTextEdit;
    QVBoxLayout *right_layout = new QVBoxLayout;
    right_layout->addLayout(head_layout);
    right_layout->addWidget(indiv_label);
    right_layout->addWidget(indiv_edit);

    QHBoxLayout *up_layout = new QHBoxLayout;
    up_layout->addLayout(left_layout);
    up_layout->addLayout(right_layout);

    QPushButton *ok_button = new QPushButton("确 认");
    this->connect(ok_button,SIGNAL(clicked(bool)),this,SLOT(slotGetContent()));
    QPushButton *cancel_button = new QPushButton("取 消");
    this->connect(cancel_button,SIGNAL(clicked(bool)),this,SLOT(close()));
    QHBoxLayout *down_layout = new QHBoxLayout;
    down_layout->addStretch();
    down_layout->addWidget(ok_button);
    down_layout->addWidget(cancel_button);

    QVBoxLayout *main_layout = new QVBoxLayout(this);
    main_layout->addLayout(up_layout);
    main_layout->addLayout(down_layout);
}

void LayoutGui::slotGetContent()
{

}

void LayoutGui::slotChangeHead()
{
    QString path = QFileDialog::getOpenFileName(this,"选择头像",":","头像图片(*.png);;所有文件(*.*)");
    if(!path.trimmed().isEmpty())
        head_label->setPixmap(QPixmap(path));
}

LayoutGui::~LayoutGui()
{

}

上述实例包含多种布局嵌套:网格布局(QGridLayout)、水平布局(QHBoxLayout)和垂直布局(QVBoxLayout),包含我们之前讲过熟悉的QLabel(标签)、QLineEdit(单行文本编辑框)、QButtonGroup(按钮组)、QPushButton(普通按钮)、QDebug(QDebug()输出所在类)、QFileDialog(文件对话框)。这里就不对他们进行讲解了。

我们来看看下面几个新类:
QRadioButton:单选按钮
QDoubleSpinBox:
QComboBox:下拉列表
QDateEdit:日期编辑器
QDate:与日期有关
QCheckBox:复选框
QTextEdit:文本域
QStackedLayout:堆栈布局、卡片布局
QStackedWidget
QListWidget:列表组件
QTableWidget:表格组件
QTreeWidget:树形组件

QTableWidget类:表格相关组件
构造函数:
QTableWidget(int rows, int columns, QWidget *parent = 0);

常用函数:
void setRowCount(int rows); 设置行数
int rowCount() const; 获得行数
void setColumnCount(int columns); 设置列数
int columnCount() const; //获得列数
void setItem(int row, int column, QTableWidgetItem *item);
void setCellWidget(int row, int column, QWidget *widget);

与上面还有一个很类似的类:
QTableWidgetItem类:表项组件
构造函数:
QTableWidgetItem(const QString &text, int type = Type);
QTableWidgetItem(const QIcon &icon, const QString &text, int type = Type);

常用函数:
void setItem()

使用:
如:要做出如下表格的表头(5行5列):
在这里插入图片描述在这里插入图片描述
程序我们可以这么写:

	this->setWindowTitle("表格示例");
	this->setRowCount(5);
	this->setColumnCount(5);

	//作表头
    QTableWidgetItem *head_1 = new QTableWidgetItem("性 别");
    QTableWidgetItem *head_2 = new QTableWidgetItem("姓 名");
    QTableWidgetItem *head_3 = new QTableWidgetItem("生 日");
    QTableWidgetItem *head_4 = new QTableWidgetItem("工 作");
    QTableWidgetItem *head_5 = new QTableWidgetItem("工 资");

    //将表头添加到表格
    this->setItem(0,0,head_1);
    this->setItem(0,1,head_2);
    this->setItem(0,2,head_3);
    this->setItem(0,3,head_4);
	this->setItem(0,4,head_5);

与QData类(时间日期)有关的类有:
QDateTimeEdit类:日期时间编辑器
构造函数:
QDateTimeEdit(QWidget *parent = 0);
QDateTimeEdit(const QDateTime &dt, QWidget *parent = 0);
QDateTimeEdit(const QDate &d, QWidget *parent = 0);
QDateTimeEdit(const QTime &t, QWidget *parent = 0);

常用函数:
void setDateTime(const QDateTime &dateTime); 设置时间文本框的日期时间
void setDate(const QDate &date); 设置时间文本框的日期
void setTime(const QTime &time); 设置时间文本框的时间
void setDisplayFormat(const QString &format); 设置显示的格式yyyy-M-dd
void setCalendarPopup(bool enable); 设置是否弹出日历
如:设置日期时间编辑器:代码如下

	QDateTimeEdit *bday_edit_1_1 = new 	QDateTimeEdit;
	bday_edit_1_1->setDateTime(QDateTime::currentDateTime());  
	bday_edit_1_1->setDisplayFormat("yyyy-M-dd hh:mm:ss");
	bday_edit_1_1->setCalendarPopup(true);

一样的,与上述类相似度很高的一个类:
QDateTime类:与日期时间先关的组件
常用函数:
static QDateTime currentDateTime(); 获取系统当前时间
QDateEdit类:日期编辑器
如:创建日期编辑器:

	QDateEdit *bday_edit_1_1 = new QDateEdit;
	bday_edit_1_1->setDate(bday);     //设置时间
	bday_edit_1_1->setDisplayFormat("yyyy-M-dd"); //设置显示格式
	bday_edit_1_1->setCalendarPopup(true);  //设置日历弹出

QComboBox类:下拉列表
类定义:class Q_GUI_EXPORT QComboBox : public QWidget
构造函数:
QComboBox(QWidget *parent = 0);

常用函数:
void addItem(const QString &atext, const QVariant &auserData); 添加选项
void insertItem(int index, const QString &text, const QVariant &userData = QVariant()); 向下拉列表插入选项
依旧是上述例子,我们设置工作下拉列表,可以这么做:

  //创建工作下拉列表
    QComboBox *work_box_1_1 = new QComboBox;
    work_box_1_1->addItem("工 人");
    work_box_1_1->addItem("学 生");
    work_box_1_1->addItem("农 民");
    work_box_1_1->addItem("军 人");
    work_box_1_1->setCurrentIndex(workindex);

QSpinBox类:分量栏
类定义:class Q_GUI_EXPORT QSpinBox : public QAbstractSpinBox
构造函数:
QSpinBox(QWidget *parent = 0);

常用函数:
void setRange(int min, int max); 设置范围
void setSingleStep(int val); 设置步长

如,设置工资分量框

  	QSpinBox *income_box_1_1 = new QSpinBox;
    income_box_1_1->setRange(0,10000);  //工资范围
    income_box_1_1->setSingleStep(100);  //每按一次步长
	income_box_1_1->setValue(income);  //设置值

完成上述步骤后,我们将所有组件组合一起,代码如下:

 //将组件添加到表格的单元格
    this->setCellWidget(row,0,sex_label_1_1);
    this->setItem(row,1,name_item_1_1);
    this->setCellWidget(row,2,bday_edit_1_1);
    this->setCellWidget(row,3,work_box_1_1);
	this->setCellWidget(row,4,income_box_1_1);

下面给出上述例子所有代码
main.cpp

#include "tablegui.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    TableGui w;
    w.show();

    return a.exec();
}

table.h

#ifndef TABLEGUI_H
#define TABLEGUI_H

#include <QWidget>
#include <QTableWidget>     //表格组件
#include <QTableWidgetItem> //表项组件
#include <QLabel>
#include <QDateTimeEdit>    //日期时间编辑器
#include <QDateTime>
#include <QDateEdit>        //日期编辑器
#include <QDate>
#include <QTimeEdit>        //时间编辑器
#include <QTime>
#include <QComboBox>        //下拉列表
#include <QSpinBox>         //整形分量框

//1.QTableWidget,QTableWidgetItem,QDateTimeEdit,QDateEdit,QTimeEdit,QComboBox,QSpinBox的使用
//2.QDateTime,QDate,QTime
//3.数据的获取

class TableGui : public QTableWidget
{
    Q_OBJECT

public:
    enum sex_t{MAN = 0,WOMAN = 1};
    TableGui(QWidget *parent = 0);
    ~TableGui();

    void addrow(int row,enum sex_t sex,const QString &name,const QDate &bday,int workindex,int income);   //添加一行
};

#endif // TABLEGUI_H

table.cpp

#include "tablegui.h"

TableGui::TableGui(QWidget *parent)
    : QTableWidget(parent)
{
    this->setWindowTitle("表格示例");
    this->setRowCount(5);
    this->setColumnCount(5);

    //作表头
    QTableWidgetItem *head_1 = new QTableWidgetItem("性 别");
    QTableWidgetItem *head_2 = new QTableWidgetItem("姓 名");
    QTableWidgetItem *head_3 = new QTableWidgetItem("生 日");
    QTableWidgetItem *head_4 = new QTableWidgetItem("工 作");
    QTableWidgetItem *head_5 = new QTableWidgetItem("工 资");

    //将表头添加到表格
    this->setItem(0,0,head_1);
    this->setItem(0,1,head_2);
    this->setItem(0,2,head_3);
    this->setItem(0,3,head_4);
    this->setItem(0,4,head_5);

    //添加数据行
    this->addrow(1,MAN,"张 三",QDate(1999,10,11),2,4000);
    this->addrow(2,WOMAN,"李 四",QDate(2000,10,11),1,3000);
    this->addrow(3,WOMAN,"王 五",QDate(2001,1,11),0,2000);
    this->addrow(4,MAN,"赵 六",QDate(1998,10,11),3,10000);

    this->resize(550,300);
}

void TableGui::addrow(int row,enum sex_t sex,const QString &name,const QDate &bday,int workindex,int income)
{
    if(row < 0 || name.isEmpty() || !bday.isValid() || workindex < 0 || workindex > 3)
        return;

    QLabel *sex_label_1_1 = new QLabel;
    if(sex == MAN)
        sex_label_1_1->setPixmap(QPixmap("://images/Male.png"));
    else
        sex_label_1_1->setPixmap(QPixmap("://images/Female.png"));
//    sex_label_1_1->pixmap()

    QTableWidgetItem *name_item_1_1 = new QTableWidgetItem(name);
//    name_item_1_1->text()

#if 0
    QDateTimeEdit *bday_edit_1_1 = new QDateTimeEdit;
    bday_edit_1_1->setDateTime(QDateTime::currentDateTime());
    bday_edit_1_1->setDisplayFormat("yyyy-M-dd hh:mm:ss");
    bday_edit_1_1->setCalendarPopup(true);
#endif

    //创建日期编辑器
    QDateEdit *bday_edit_1_1 = new QDateEdit;
    bday_edit_1_1->setDate(bday);
    bday_edit_1_1->setDisplayFormat("yyyy-M-dd");
    bday_edit_1_1->setCalendarPopup(true);
//    bday_edit_1_1->date()

    //创建工作下拉列表
    QComboBox *work_box_1_1 = new QComboBox;
    work_box_1_1->addItem("工 人");
    work_box_1_1->addItem("学 生");
    work_box_1_1->addItem("农 民");
    work_box_1_1->addItem("军 人");
    work_box_1_1->setCurrentIndex(workindex);
//    work_box_1_1->currentIndex();
//    work_box_1_1->currentText()

    //创建工资分量框
    QSpinBox *income_box_1_1 = new QSpinBox;
    income_box_1_1->setRange(0,10000);
    income_box_1_1->setSingleStep(100);
    income_box_1_1->setValue(income);
//    income_box_1_1->value()

    //将组件添加到表格的单元格
    this->setCellWidget(row,0,sex_label_1_1);
    this->setItem(row,1,name_item_1_1);
    this->setCellWidget(row,2,bday_edit_1_1);
    this->setCellWidget(row,3,work_box_1_1);
    this->setCellWidget(row,4,income_box_1_1);
}

TableGui::~TableGui()
{

}

有关表格和日期时间的类,暂时就说这么多,我们接着往下说。

QTextEdit类:文本域或者叫多行文本框
构造函数:
QTextEdit(QWidget *parent = 0);
QTextEdit(const QString &text, QWidget *parent = 0);

常用函数:
void QWidget::setMinimumSize(const QSize &s)
void setFixedSize(int w, int h);

使用:
上面实例中,我们在“部门信息”一栏中就属于多行文本编辑框(文本域),那么创建这么一个组件我们可以这么操作:

	//先创建组件
	QLabel *dept = new Qlabel(“部门信息”);
	QTextEdit *dept_edit = new QTextEdit;
	//将组件添加到布局中
	QGridLayout *left_layout = new QGridLayout;
	left_layout->addWidget(dept_label,6,0);
	left_layout->addWidget(dept_edit,6,1);

实例中还有一点需要说明一下:
QPixmap类:图片类(像素为单位)
构造函数:
QPixmap(const QString& fileName, const char *format = 0, Qt::ImageConversionFlags flags = Qt::AutoColor);
QPixmap(const char * const xpm[]);

使用:
依旧拿本实例来举例使用,
在界面的右上方:
在这里插入图片描述
这一块,程序中,可以这么操作:

 	QLabel *label_1 = new QLabel("头 像");
    head_label = new QLabel;
    head_label->setPixmap(QPixmap("://images/icon.png"));
    QPushButton *head_button = new QPushButton("修改头像");
	this->connect(head_button,SIGNAL(clicked(bool)),this,SLOT(slotChangeHead()));

void LayoutGui::slotChangeHead()
{
    QString path = QFileDialog::getOpenFileName(this,"选择头像",":","头像图片(*.png);;所有文件(*.*)");
    if(!path.trimmed().isEmpty())
        head_label->setPixmap(QPixmap(path));
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

all of the time

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值