zoukankan      html  css  js  c++  java
  • 系统整理qt笔记1

    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() << "我的按钮类析构";
    
    }
  • 相关阅读:
    51 Nod 1068 Bash游戏v3
    51 Nod Bash 游戏v2
    51 Nod 1073 约瑟夫环
    UVA 12063 Zeros and ones 一道需要好好体会的好题
    51 Nod 1161 Partial sums
    2018中国大学生程序设计竞赛
    UVA 11971 Polygon
    UVA 10900 So do you want to be a 2^n-aire?
    UVA 11346 Possibility
    python with as 的用法
  • 原文地址:https://www.cnblogs.com/kongweisi/p/14010644.html
Copyright © 2011-2022 走看看