QT学习笔记----day01

先来学习了解一下几个类:
QApplication类
主要功能:
1.负责GUI应用程序的控制流和主要设置
2.对于一个应用程序来说,建立此类的对象是必不可少的
3.在main()函数中创建的第一个对象,其他可执行对象必须在其之后创建
4.包含主事件循环体,负责处理和调度所有来自窗口系统和其他资源事件。

QWidget类
所有用户接口对象的基类,继承了QObject类的属性。组件是用户界面的单元组成部分,它接收鼠标、键盘和其它从窗口系统来的事件,并把它自己绘制在屏幕上
QWidget类有很多成员函数,但一般不直接使用,而是通过子类继承来使用其函数功能。如,QPushButton、QlistBox等都是它的子类。
窗体是Qwidget类或它子类的实例,客户自己的窗体类需要从Qwidget它的子类继承。

QObject类:是所有能够处理signal、slot和事件的QT对象的基类。

一、实例一:

先上效果,如下:
在这里插入图片描述
代码:

#include <QApplication> //导入QApplication类
#include <QpushButton>  //命令按钮
#include <QDebug>

int main(int argc, char *argv[])  //应用程序入口
{
    QApplication a(argc, argv);  //QApplication:1.必须是可执行对象中的第一个  2.对QT界面起着管理作用(管家)

#if 0
    QPushButton button("关 闭"); //一般对象,在栈内存,不常用
    button.show();          //调用显示函数:1.界面组件一般放入布局管理器中,2.没有放入布局管理器的组件则需要show()函数或者exec()去显示
    //QObject::connect(&button,SIGNAL(clicked(bool)),&button,SLOT(close())); //方法1,只关闭了按钮
    QObject::connect(&button,SIGNAL(clicked(bool)),&a,SLOT(quit()));  //方法2,关闭了应用程序
#endif

#if 1
    QPushButton *close_button = new QPushButton("关 闭");  //在堆内存,常用方式   qt使用智能指针:指针由父组件释放
    close_button->setText("按我关闭!");  //在按钮上显示对应文本
    QString text = close_button->text();
    qDebug() << text << endl;
    close_button->show();
    //SIGNAL是信号,SLOT是槽.也就是发生SIGNAL的时候,调用SLOT设定的函数,类似Windows的消息触发.
    QObject::connect(close_button,SIGNAL(clicked(bool)),&a,SLOT(quit()));
    //delete close_button;  //这个不需要
    //close_button->deleteLater();//记住,一般不需要释放
#endif

    return a.exec();   //只有执行了a.exec()界面才会出来
}

QPushbutton类:创建按钮
构造函数:
//构造一个没有文本的命令按钮,参数为parent。
QPushButton ( QWidget * parent = 0 )

//构造一个父对象为parent、文本为text的命令按钮。
QPushButton ( const QString & text, QWidget * parent = 0 )

//构造一个图标为icon、文本为text、父对象为parent命令按钮
QPushButton ( const QIcon & icon, const QString & text, QWidget * parent = 0 )

常用函数:
//设置这个按钮上显示的文本
void setText ( const QString & )

//在按钮设置一个图标
void setIcon(const QIcon &icon)

二、 实例二

同样,实例二最终实现效果如下:
在这里插入图片描述
点击选择文件出现下面窗口:
在这里插入图片描述
选择字体后,出现如下窗口:
在这里插入图片描述
选择颜色后,出现如下窗口:
在这里插入图片描述
最终,我们随便选择一个文件,字体选择楷体,大小为14,颜色选择为红色,效果如下:
在这里插入图片描述

接下来直接上代码:
main.cpp

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

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

    return a.exec();
}

dialoggui.h

#ifndef DIALOGGUI_H
#define DIALOGGUI_H

#include <QDialog>
#include <QLineEdit>
#include <QFileDialog>
#include <QGridLayout>
#include <QPushButton>
#include <QFontDialog>
#include <QColorDialog>
#include <QDebug>
#include <stdio.h>
#include <QFrame>

class DialogGui : public QDialog
{
    Q_OBJECT

public:
    DialogGui(QWidget *parent = 0);
    ~DialogGui();

    QLineEdit *file_edit;
    QLineEdit *font_edit;
    QFrame *color_frame;

public slots:
    void slot_filehandler();
    void slot_fonthandler();
    void slot_colorhandler();
};

#endif // DIALOGGUI_H

dialog.cpp

#include "dialoggui.h"

DialogGui::DialogGui(QWidget *parent)
    : QDialog(parent)
{
    QPushButton *file_button = new QPushButton("选择文件");
    QPushButton *font_button = new QPushButton("字    体");
    QPushButton *color_button = new QPushButton("颜    色");
    file_edit = new QLineEdit;
    font_edit = new QLineEdit("世界,你好!");
    color_frame = new QFrame;
    color_frame->setFrameShape(QFrame::Box);
    color_frame->setAutoFillBackground(true);

    QGridLayout *grid_layout = new QGridLayout(this);
    grid_layout->addWidget(file_button,0,0);
    grid_layout->addWidget(file_edit,0,1);
    grid_layout->addWidget(font_button,1,0);
    grid_layout->addWidget(font_edit,1,1);
    grid_layout->addWidget(color_button,2,0);
    grid_layout->addWidget(color_frame,2,1);

    font_edit->setReadOnly(true);

    QPushButton::connect(file_button,SIGNAL(clicked(bool)),this,SLOT(slot_filehandler()));
    QPushButton::connect(font_button,SIGNAL(clicked(bool)),this,SLOT(slot_fonthandler()));
    QPushButton::connect(color_button,SIGNAL(clicked(bool)),this,SLOT(slot_colorhandler()));
}

void DialogGui::slot_filehandler()
{
    QFileDialog *file_dialog = new QFileDialog;

    //打开文件
    QString path = file_dialog->getOpenFileName(this,"选择一个文件","/","C++源文件(*.cpp);;C++头文件(*.h);;所有文件(*.*)");
    if(path != path.isEmpty())
    {
        file_edit->setText(path);
    }
}

void DialogGui::slot_fonthandler()
{
    bool ok;
    QFontDialog *font_dialog = new QFontDialog;
    QFont qfont = font_dialog->getFont(&ok,QFont( "Helvetica [Cronyx]", 10 ), this );
    if(ok)
    {
        font_edit->setFont(qfont);
    }
}

void DialogGui::slot_colorhandler()
{
    QColorDialog *color_dialog = new QColorDialog;
    QColor color = color_dialog->getColor();
    if(color.isValid())
        color_frame->setPalette(QPalette(color));//改变填充颜色
}

DialogGui::~DialogGui()
{

}

实例二笔记:
1.QLineEdit类:创建一个单行文本框
构造函数:
QLineEdit(QWidget* parent=0); //默认的构造函数

QLineEdit(const QString &, QWidget* parent=0); //使用字符串构造

常用函数:
void setReadOnly(bool); //设置只读

void setText(const QString &);//设置文本

void setMaxLength(int); //设置最大长度

使用:
QLineEdit *file_edit = new QLineEdit (“打开文件”); //初始化一个实例

如果要设置文本框为只读,操作如下:
file_edit->setReadOnly(true);

往文本框填写字符串:
file_edit->setText(“LHSMD”);

2.QFrame类:有框架的窗口部件的基类
构造函数
QFrame ( QWidget * parent = 0, const char * name = 0, WFlags f = 0 )

常用函数
void setAutoFillBackground(bool enabled); 设置Frame自动填充背景颜色

void setFrameShape ( Shape ) 设置Frame的形状

Shape frameShape () const 获取Frame的形状

使用:
初始化一个实例:
color_frame = new QFrame;

设置Frame的形状
color_frame->setFrameShape(QFrame::Box);

设置Frame自动填充背景颜色
color_frame->setAutoFillBackground(true);

3.QGridLayout类:网格布局管理器
构造函数:
QGridLayout::QGridLayout(QWidget *parent,int nRows = 1,int nCols = 1, int margin = 0,int space = -1,const char * name = 0)

构造时parent一般为this,相当于parent.setlayout(网格布局管理器对象)

QGridLayout::QGridLayout(QLayout *parentLayout, int nRows = 1, int nCols = 1,int spacing = -1,const char * name = 0)

常用函数:
int numRows() const 获得行数
int numCols() const 获得列数
void addItem(QLayoutItem *item,int row,int col) 添加item对象到指定行列

添加widget对象到指定行列
void addWidget(QWidget *w,int row,int col,int alignment = 0)

添加指定布局管理器到指定行列
void addLayout(QLayout *layout,int row,int col)

void setMargin(int); 设置边框的宽度
void setSpacing(int); 设置组件的间距

使用:
//初始化实例,在指定的位置显示组件(网格)
QGridLayout *grid_layout = new QGridLayout(this);
grid_layout->addWidget(file_button,0,0);
grid_layout->addWidget(file_edit,0,1);
grid_layout->addWidget(font_button,1,0);
grid_layout->addWidget(font_edit,1,1);
grid_layout->addWidget(color_button,2,0);
grid_layout->addWidget(color_frame,2,1);

4.QFileDialog类:文件对话框
构造函数:
QFileDialog(QWidget *parent, Qt::WindowFlags f);
QFileDialog(QWidget *parent = 0,
const QString &caption = QString(),
const QString &directory = QString(),
const QString &filter = QString());

静态公有函数
static QString getOpenFileName(QWidget *parent = 0, //父窗口对象
const QString &caption = QString(), //窗口标题
const QString &dir = QString(), //默认打开的路径
const QString &filter = QString(), //过滤器
QString *selectedFilter = 0, //查询过滤器
Options options = 0); //操作

static QString getSaveFileName(QWidget *parent = 0,
const QString &caption = QString(),
const QString &dir = QString(),
const QString &filter = QString(),
QString *selectedFilter = 0,
Options options = 0);

static QString getExistingDirectory(QWidget *parent = 0,
const QString &caption = QString(),
const QString &dir = QString(),
Options options = ShowDirsOnly);

static QStringList getOpenFileNames(QWidget *parent = 0,
const QString &caption = QString(),
const QString &dir = QString(),
const QString &filter = QString(),
QString *selectedFilter = 0,
Options options = 0);

常用函数
void setDirectory(const QString &directory); 设置默认打开目录
void selectFile(const QString &filename); 查询所选择文件
QStringList selectedFiles() const; 查询所返回文件列表
void setNameFilter(const QString &filter); 设置过滤器
void setNameFilters(const QStringList &filters); 设置多个过滤器

使用:
初始化一个实例

QFileDialog *file_dialog = new QFileDialog;
    //打开文件
QString path = file_dialog->***getOpenFileName***(this,"选择一个文件","/","C++源文件(*.cpp);;C++头文件(*.h);;所有文件(*.*)");
    if(path != path.isEmpty())
    {
        file_edit->setText(path);
    }

5.QFontDialog类:字体对话框
构造函数:
explicit QFontDialog(QWidget *parent = 0);
explicit QFontDialog(const QFont &initial, QWidget *parent = 0);
静态函数:
static QFont getFont(bool *ok, QWidget *parent = 0);
static QFont getFont(bool *ok, const QFont &initial, QWidget *parent, const QString &title,FontDialogOptions options);
常用函数:
void setCurrentFont(const QFont &font);
QFont currentFont() const;
QFont selectedFont() const;
void setOption(FontDialogOption option, bool on = true);
FontDialogOptions options() const;

使用:

bool ok;
    QFontDialog *font_dialog = new QFontDialog;
    QFont qfont = font_dialog->getFont(&ok,QFont( "Helvetica [Cronyx]", 10 ), this );
    if(ok)
    {
        font_edit->setFont(qfont);
    }

6.QColorDialog类:颜色对话框
构造函数
explicit QColorDialog(QWidget *parent = 0);
explicit QColorDialog(const QColor &initial, QWidget *parent = 0);
静态函数
static QColor getColor(const QColor &initial, QWidget *parent, const QString &title,ColorDialogOptions options = 0);
static QRgb getRgba(QRgb rgba = 0xffffffff, bool *ok = 0, QWidget *parent = 0);

常用函数
void setCurrentColor(const QColor &color);
QColor currentColor() const;
QColor selectedColor() const;
void setOption(ColorDialogOption option, bool on = true);

使用:

QColorDialog *color_dialog = new QColorDialog;
QColor color = color_dialog->getColor();
if(color.isValid())
    color_frame->setPalette(QPalette(color));//改变填充颜色
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 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、付费专栏及课程。

余额充值