Qt文档阅读笔记-Widgets Tutorial官方解析及实例

583 篇文章 127 订阅
196 篇文章 13 订阅

目录

官方解析

博主栗子


官方解析


QWiget通常作为其他窗口的容器,可以使用QWidget到达最小代价定义自定义控件(经验:通常可以采用子类QLabel做成自定义控件)
可以通过构建他的父类对象来指定他的对象树。

注意:当删除窗口后内部的控件都会自动删除!

  #include <QtWidgets>

  // Include header files for application components.
  // ...

  int main(int argc, char *argv[])
  {
      QApplication app(argc, argv);

      // Set up and show widgets.
      // ...

      return app.exec();
  }


argc与*argv[]通过命令行可以对app进行配置;
QApplication::exec()进入Qt的事件循环;

 

博主栗子

来验证一下关闭主窗口后是否释放了子控件,ui文件如下:

源码如下:

mylabel.h

#ifndef MYLABEL_H
#define MYLABEL_H

#include <QLabel>

class MyLabel : public QLabel
{
    Q_OBJECT
public:
   MyLabel(QWidget *parent = 0);
   ~MyLabel();

signals:

public slots:
};

#endif // MYLABEL_H

widget.h

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>

namespace Ui {
class Widget;
}

class Widget : public QWidget
{
    Q_OBJECT

public:
    explicit Widget(QWidget *parent = 0);
    ~Widget();

private:
    Ui::Widget *ui;
};

#endif // WIDGET_H

main.cpp

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

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

    return a.exec();
}

mylabel.cpp

#include "mylabel.h"
#include <QDebug>

MyLabel::MyLabel(QWidget *parent) : QLabel(parent)
{

}

MyLabel::~MyLabel()
{
    qDebug()<<"MyLabel::~MyLabel() called!";
}

widget.cpp

#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
}

Widget::~Widget()
{
    delete ui;
}

程序运行截图如下:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

IT1995

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

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

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

打赏作者

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

抵扣说明:

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

余额充值