Qt版本的hello word
直接在构造函数里面用QLabel组件
#include "ui_meet_qt.h" #include <QLabel> //QLabel的头文件 #include<QString> //QString的头文件 Meet_Qt::Meet_Qt(QWidget *parent) : QWidget(parent) , ui(new Ui::Meet_Qt) { ui->setupUi(this); QLabel * label = new QLabel(this); //定义QLabel label->setText(QString("Hello word!")); //设置文本 } Meet_Qt::~Meet_Qt() { delete ui; }效果
问题: QLabel * label = new QLabel(this); 定义完new了之后不释放不会内存泄漏吗?
答:不会。该指针对象通过this被挂载到对象树上,生命周期交给Qt的对象树来统一管理,程序结束统一释放,前端开发也有类似的对象书,本质是一个树形结构,通过树形结构把界面上的各种元素组织起来。
如果不用指针的话,程序结束析构函数会直接释放掉,连效果都没有
继承一个QLabel,然后实例化,看看关掉程序后会不会调用析构释放该指针
//main.cpp #include "meet_qt.h" #include <QApplication> #include"my_qlabel.h" int main(int argc, char *argv[]) { QApplication a(argc, argv); My_QLabel w(nullptr); w.show(); return a.exec(); } //meet_qt.h #ifndef MEET_QT_H #define MEET_QT_H #include <QWidget> QT_BEGIN_NAMESPACE namespace Ui { class Meet_Qt; } QT_END_NAMESPACE class Meet_Qt : public QWidget { Q_OBJECT public: Meet_Qt(QWidget *parent = nullptr); ~Meet_Qt(); private: Ui::Meet_Qt *ui; }; #endif // MEET_QT_H //my_qlabel.h #ifndef MY_QLABEL_H #define MY_QLABEL_H #include<QLabel> class My_QLabel : public QLabel { public: My_QLabel(QWidget * parent);//构造函数使用带QWidget*的,确保自己的对象能够加载到对象树上 ~My_QLabel(); }; #endif // MY_QLABEL_H meet_qt.cpp #include "meet_qt.h" #include"my_qlabel.h" #include "ui_meet_qt.h" #include <QLabel> //QLabel的头文件 #include<QString> //QString的头文件 Meet_Qt::Meet_Qt(QWidget *parent) : QWidget(parent) , ui(new Ui::Meet_Qt) { ui->setupUi(this); My_QLabel* label = new My_QLabel(this); label->setText("使用this挂载对象树"); label->setGeometry(20, 20, 300, 30); } Meet_Qt::~Meet_Qt() { delete ui; } //my_qlabel.cpp #include "my_qlabel.h" #include <iostream> My_QLabel::My_QLabel(QWidget* prent):QLabel(prent) { } My_QLabel::~My_QLabel() { std::cout<< "My_QLabel被销毁!"<<std::endl; }qDebug
为什么会乱码呢,主要是qt里面不是用UTF-8表示的,Qt中提供了一个qDebug()工具,借助这个就可以完成打印日志的过程,很好的处理字符编码问题,这个宏,封装了qDebug对象,直接使用qDebug()这个东西可以当成cout来使用,但如果是Linux下就没问题,Linux下默认就是utf-8,windows容易出现乱码
#include "my_qlabel.h" #include <iostream> #include<QDebug> My_QLabel::My_QLabel(QWidget* prent):QLabel(prent) { } My_QLabel::~My_QLabel() { //std::cout<< "My_QLabel被销毁!"<<std::endl; qDebug()<< "My_QLabel被销毁!"; }总结:
- 认识QLabel类,能够在界面上显示字符串,通过setText来设置的,参数QString(历史原因,qt重新封装了C++的容器)
- 内存泄漏/文件资源泄漏
- 对象树,qt通过对象树来统一释放界面的控件对象,qt还是推荐使用new的方式在堆上创建对象,通过对象树统一释放对象,创建的时候在构造函数里指定父对象(此时才会挂到对象树上,否则的话没有挂到对象树上就得在合适的时候手动释放)
- 通过继承自Qt内置的类,达到对现有控件进行功能拓展效果Qt内置的QLabel,没法看到销毁的过程,为了看清楚创建了一个类并继承QLabel,重写析构,在析构上加上日志,看到效果
- 乱码问题和字符集
- 在Qt中打印日志,cout容易产生乱码问题,引入内部封装的日志qDebug()来完成打印
QLineEdit
#include "widget.h" #include "ui_widget.h" #include <QLineEdit> Widget::Widget(QWidget *parent) : QWidget(parent) , ui(new Ui::Widget) { ui->setupUi(this); QLineEdit * edit = new QLineEdit(this); edit->setText("hello word!"); } Widget::~Widget() { delete ui; }connect
#include "widget.h" #include "ui_widget.h" Widget::Widget(QWidget *parent) : QWidget(parent) , ui(new Ui::Widget) { ui->setupUi(this); connect(ui->pushButton,&QPushButton::clicked,this,&Widget::handleClick); } Widget::~Widget() { delete ui; } void Widget::handleClick() { //当按钮被点击之后,就把按钮中的文本进行切换 if(ui->pushButton->text()==QString("hello word!")) { ui->pushButton->setText("hello qt!"); } else { ui->pushButton->setText("hello word!"); } }ui里面实例化了一个pushButton,而ui又继承Widget,他们都在Ui_Widget下
纯代码版本和图形化版本对比
纯代码
#include "widget.h" #include "ui_widget.h" Widget::Widget(QWidget *parent) : QWidget(parent) , ui(new Ui::Widget) { ui->setupUi(this); myButton = new QPushButton(this); myButton->setText("hello word!"); connect(myButton,&QPushButton::clicked,this,&Widget::handleClick); } Widget::~Widget() { delete ui; } void Widget::handleClick() { if(myButton->text()==QString("hello word!")) { myButton->setText("hello qt"); } else { myButton->setText("hello word!"); } }图形化生成
#include "widget.h" #include "ui_widget.h" Widget::Widget(QWidget *parent) : QWidget(parent) , ui(new Ui::Widget) { ui->setupUi(this); connect(ui->pushButton,&QPushButton::clicked,this,&Widget::handleClick); } Widget::~Widget() { delete ui; } void Widget::handleClick() { //当按钮被点击之后,就把按钮中的文本进行切换 if(ui->pushButton->text()==QString("hello word!")) { ui->pushButton->setText("hello qt!"); } else { ui->pushButton->setText("hello word!"); } }对比:纯代码生成需要把变量作为类内成员使用,而图形化生成的话他自动就被放在ui实例化的内部成员了
这两种都能达到一样的效果哪种更好呢?
两种都好,难分主次!如果当前界面内容比较固定不需要太大变化就用图形化界面来构造,如果经常要动态变化,就用代码的形式来构造。哪种方便就用哪种,配合使用
QTextEdit
变量命名风格
C++和Linux一般用蛇形命名法,如student_count,unordered_map,Qt中偏好驼峰命名法,使用大写字符来进行单词分割,如QWidget,QApplication。
使用帮助文档
1.光标放到要查询的类名/方法上直接按F1
2.Qt Creator 左边栏中直接用鼠标单击“帮助”按钮
3.在桌面开始菜单栏找到Assistant