main.cpp

#include "mywidget.h"
#include <QApplication>//包含一个应用程序类的头文件
#include <iostream>
#include <QThread>
#include <QDebug>
#include <thread>
#include <QString> //帮助文档 F1 退出ESC
//自动对其 ctrl + i
//同名之间的.h和.cpp切换 F4 using namespace std; //main程序入口 argc命令行变量的数量 argv命令行变量的数组
int main(int argc, char *argv[])
{
//a应用程序对象,在QT中,应用程序对象,有且只有一个
QApplication a(argc, argv); cout << "nihao" << endl; //打印线程id方法 1
QString LogInfo;
LogInfo.sprintf("%p", QThread::currentThread());
qDebug() << "OpenSerialPort " <<"threadID : "<<LogInfo; //打印线程id方法 2
//以下代码片段得到当前线程的id,并通过QT输出窗口输出调试信息
std::thread::id id = std::this_thread::get_id();
cout << "----打印线程id:----" << id;
// std::stringstream sin;
// sin << id; // qDebug()<<"threadID : "<<QString::fromStdString(sin.str()); //窗口对象 它的父类是QWidget
MyWidget w;
w.show(); //在应用程序对象进入消息循环(机制)
//让代码阻塞到这儿
return a.exec();
}

mywidget.h

#ifndef MYWIDGET_H
#define MYWIDGET_H #include <QWidget> class MyWidget : public QWidget
{
Q_OBJECT // Q_OBJECT 宏,允许类中使用信号和槽的机制 public:
MyWidget(QWidget *parent = 0);
~MyWidget();
}; #endif // MYWIDGET_H

mywidget.cpp

#include "mywidget.h"
#include <QPushButton>
#include "mypushbutton.h" MyWidget::MyWidget(QWidget *parent)
: QWidget(parent)
{
//创建一个按钮 让btn对象依赖在 MyWidget中
QPushButton * btn1 = new QPushButton("第一个按钮", this);
btn1->resize(80, 20);
//重置窗口大小
//resize(600, 400);
//设置窗口标题
setWindowTitle("第一个窗口");
//设置固定窗口大小(用户不能修改)
setFixedSize(600, 400); //创建第二个按钮
QPushButton * btn2 = new QPushButton;
btn2->setParent(this);
btn2->setText("第二个按钮");
btn2->move(100, 100); //创建自己的按钮
MyPushButton * btn3 = new MyPushButton;
btn3->setParent(this);
btn3->setText("自己的按钮");
btn3->move(200, 0); //需求 点击我的按钮 关闭窗口
connect(btn3, &QPushButton::clicked, this, &MyWidget::close);
//connect(btn3, &MyPushButton::clicked, this, &QWidget::close); } MyWidget::~MyWidget()
{ }

mypushbutton.h

#ifndef MYPUSHBUTTON_H
#define MYPUSHBUTTON_H #include <QPushButton> class MyPushButton : public QPushButton
{
Q_OBJECT
public:
explicit MyPushButton(QWidget *parent = nullptr); ~MyPushButton(); signals: public slots:
}; #endif // MYPUSHBUTTON_H

mypushbutton.cpp

#include "mypushbutton.h"

#include <QDebug>

MyPushButton::MyPushButton(QWidget *parent) : QPushButton(parent)
{
qDebug() << "我的按钮类构造调用"; } MyPushButton::~MyPushButton()
{
qDebug() << "我的按钮类析构"; }

最新文章

  1. C# 方法返回值的个数
  2. 研究Mysql优化得出一些建设性的方案
  3. js计时器
  4. WebView---Android与js交互实例
  5. oracle win7下 卸载
  6. ecshop怎么添加配送方式
  7. java.io.IOException: Could not locate executable null\bin\winutils.exe in the Hadoop binaries
  8. Unity3D The Blacksmith Demo部分内容学习
  9. linux之history
  10. C++ Socket学习记录 -2
  11. linu下C语言之BMP图片操作编程(上)
  12. Centos 7 安装composer和Laravel
  13. Java ee第四周作业
  14. spring cloud+.net core搭建微服务架构:服务发现(二)
  15. Fiddler查看接口响应时间
  16. 如何Python下载大文件?
  17. CentOS下Docker安装
  18. PCB规则设置
  19. 11-01 Java 开发工具 eclipse从下载、安装到实际使用的详细教程
  20. JS继承实现的几种方式

热门文章

  1. python10day
  2. Linux 系统conda环境,pip文件的导出和安装。
  3. APC 篇——初识 APC
  4. Swift数组
  5. java run()方法无法throws 异常
  6. NSMutableString常用方法
  7. LaunchScreen&amp;LaunchImage
  8. NSLog输出格式及随机数
  9. IOS tableView 滑动删除与排序功能
  10. 泛型、Junit