系统整理qt笔记1
2024-09-08 13:54:33
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() << "我的按钮类析构"; }
最新文章
- C# 方法返回值的个数
- 研究Mysql优化得出一些建设性的方案
- js计时器
- WebView---Android与js交互实例
- oracle win7下 卸载
- ecshop怎么添加配送方式
- java.io.IOException: Could not locate executable null\bin\winutils.exe in the Hadoop binaries
- Unity3D The Blacksmith Demo部分内容学习
- linux之history
- C++ Socket学习记录 -2
- linu下C语言之BMP图片操作编程(上)
- Centos 7 安装composer和Laravel
- Java ee第四周作业
- spring cloud+.net core搭建微服务架构:服务发现(二)
- Fiddler查看接口响应时间
- 如何Python下载大文件?
- CentOS下Docker安装
- PCB规则设置
- 11-01 Java 开发工具 eclipse从下载、安装到实际使用的详细教程
- JS继承实现的几种方式