
Qt版本的hello word直接在构造函数里面用QLabel组件#include ui_meet_qt.h #include QLabel //QLabel的头文件 #includeQString //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 #includemy_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 #includeQLabel class My_QLabel : public QLabel { public: My_QLabel(QWidget * parent);//构造函数使用带QWidget*的确保自己的对象能够加载到对象树上 ~My_QLabel(); }; #endif // MY_QLABEL_H meet_qt.cpp #include meet_qt.h #includemy_qlabel.h #include ui_meet_qt.h #include QLabel //QLabel的头文件 #includeQString //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-8windows容易出现乱码#include my_qlabel.h #include iostream #includeQDebug 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_mapQt中偏好驼峰命名法使用大写字符来进行单词分割如QWidget,QApplication。使用帮助文档1.光标放到要查询的类名/方法上直接按F12.Qt Creator 左边栏中直接用鼠标单击“帮助”按钮3.在桌面开始菜单栏找到Assistant