QT新建一个项目

作者:去你个锤子
#qt基础笔记:
使用QT开发时,通常使用connect函数只传递四个参数:connect(Sender,SIGNAL(signal),Receiver,SLOT(slot));
实际上connect函数应该是如下形式:connect(sender,SIGNAL(signal),receiver,SLOT(slot),Qt::DirectConnection);

sender和receiver是QObject对象指针,函数里面我们用到了Qt提供的两个宏SIGNAL()和SLOT();这是Qt要求的,
要关联信号和槽必须借助于这两个宏,两个宏的定义如下:
#define SLOT(name) “1”#name
#define SIGNAL(name) “2”#name
通过这两个宏,就可以把我们传递进去的槽和信号的名字转化成字符串,并在这两个字符串前面加上附加的字符。

clicked(bool)和toggled(bool)这两个信号函数,
比如单击clicked为true,那就是true,下次单击还是true。
单击toggled,第一次为true,第二次就是false。

#基础目录如下:

在这里插入图片描述
##mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>//窗口类头文件

QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

//定义了一个MainWindow类,并继承主窗口QMainWindow
class MainWindow : public QMainWindow
{
    Q_OBJECT //允许类中使用信号和槽
    /*
     * 事件发生后(点击),发出一个信号,若有对象对这个感兴趣
     * 就会连接(connect)这个信号。
     * 就是将想要处理的信号和自己的函数(槽slot)绑定来处理这个信号
     * 当信号发出时,被连接的槽函数会自动被回调。
    */

public:
    //构造函数(允许重载):定义如何对类进行初始化的成员函数
    //但是不能有返回值,不能声明const,名字必须和类名相同。
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();//析构函数:释放对象使用的资源,并销毁非static成员

private:
    Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H

##main.cpp

#include "mainwindow.h"
#include <QPushButton>
#include <QApplication>

//argc表示命令变量的数量,argv表示命令行变量数组
int main(int argc, char *argv[])
{
    //应用程序对象,QT中只有一个
    QApplication a(argc, argv);
    MainWindow w;
    w.show();
    return a.exec();
}

##mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
#include <QPixmap>
#include <QScreen>
#include <QDateTime>
#include <QMessageBox>
#include <QFileDialog>
#include <alibabacloud/oss/OssClient.h>
#include <fstream>
#include <string>
#include <sstream>
#include <iostream>
using namespace AlibabaCloud::OSS;
/*
 * A.B  A为对象或者结构体
 * A->B 分别为指针、成员提取、成员。提取A(只能指向类、结构、联合指针)中的成员B。
 * A::B 作用域A(空间、类、结构)中的名称B,
 * A:B  继承、构造函数
*/
/*
 * A::B:C
 * B是构造函数、C(这里是调用不是定义,因此是实参)是基类构造函数。
 * 首先QMainWindow是父类(基类)而MainWindow是子类(派生类),他们是继承关系;
 * MainWindow(QWidget *parent)是类MainWindow的构造函数,
 * QMainwindow(parent)是类QMainwindow的构造函数。类Mainwindow在实现构造函数时,
 * 要初始化基类QMainwindow的数据成员但是因为无法继承基类的构造函数,
 * 所以才用这种方式初始化基类的数据成员。
 * 这个博客解析的不错
 * https://www.cnblogs.com/wllwqdeai/p/10839326.html
*/
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    /*ui->setupUi(this)是由.ui文件生成的类的构造函数,这个函数的作用是对界面进行初始化,
     *它按照我们在Qt设计器里设计的样子把窗体画出来,把我们在Qt设计器里面定义的信号和槽建立起来。
     * this实际上是成员函数的一个形参,在调用成员函数时将对象的地址作为实参传递给this。不过 this 这个形参是隐式的,
     * 它并不出现在代码中,而是在编译阶段由编译器默默地将它添加到参数列表中。this 作为隐式形参,
     * 本质上是成员函数的局部变量,所以只能用在成员函数的内部,并且只有在通过对象调用成员函数时才给 this 赋值。
     */
    ui->setupUi(this);
}


MainWindow::~MainWindow()
{
    delete ui;//释放内存
}




void MainWindow::on_pushButton1_clicked()
{
    QScreen *screen      = QGuiApplication::primaryScreen();
    QString filePathName = "Screen-";
    filePathName        += QDateTime::currentDateTime().toString("yyyy-MM-dd hh-mm-ss");
    filePathName        += ".jpg";//控制图片格式的关键
    QString localPath    = "C:/Users/dell/Desktop/QT-TEST/" + filePathName;//保存图片的本地路径


    if(!screen->grabWindow(0).save(localPath, "jpg"))
    {
        QMessageBox::information(this,"","");
    }
    qDebug() << localPath;
    std::string PATH            = localPath.toStdString();
    std::string NAME            = filePathName.toStdString();
    std::string AccessKeyId     = "LTAI4G2nwLG1FfG6ar79k35g";
    std::string AccessKeySecret = "GfZMeK6t4yZR25Pn5FSWjPH2hd2Jko";
    std::string Endpoint        = "oss-cn-shanghai.aliyuncs.com";
    std::string BucketName      = "xxp";
    std::string ObjectName      = "image5/"+NAME;


    InitializeSdk();
    ClientConfiguration            conf;
    OssClient                      client(Endpoint, AccessKeyId, AccessKeySecret, conf);
    std::shared_ptr<std::iostream> content = std::make_shared<std::fstream>(PATH, std::ios::in | std::ios::binary);
    PutObjectRequest               request(BucketName, ObjectName, content);


    auto outcome = client.PutObject(request);
    if (!outcome.isSuccess())
    {
        std::cout << "PutObject fail" <<
            ",code:" << outcome.error().Code() <<
            ",message:" << outcome.error().Message() <<
            ",requestId:" << outcome.error().RequestId() << std::endl;
        ShutdownSdk();
    }


    /* 释放网络等资源 */
    ShutdownSdk();
}


void MainWindow::on_pushButton2_clicked()
{
    QScreen *screen      = QGuiApplication::primaryScreen();
    QString filePathName = "Screen-";
    filePathName        += QDateTime::currentDateTime().toString("yyyy-MM-dd hh-mm-ss");
    filePathName        += ".mp4";//控制图片格式的关键
    QString localPath    = "C:/Users/dell/Desktop/QT-TEST/" + filePathName;//保存图片的本地路径


//    if(!screen->grabWindow(0).save(localPath, "jpg"))
//    {
//        QMessageBox::information(this,"","");
//    }
    qDebug() << localPath;
  //  std::string PATH            = localPath.toStdStrin();
    std::string NAME            = filePathName.toStdString();
    std::string AccessKeyId     = "***********";
    std::string AccessKeySecret = "******";
    std::string Endpoint        = "oss-cn-shanghai.aliyuncs.com";
    std::string BucketName      = "xxp";
    std::string ObjectName      = "image5/"+NAME;


    InitializeSdk();
    ClientConfiguration            conf;
    OssClient                      client(Endpoint, AccessKeyId, AccessKeySecret, conf);
    std::shared_ptr<std::iostream> content = std::make_shared<std::fstream>("C:/Users/dell/Desktop/ThirdParty/image/21.mp4", std::ios::in | std::ios::binary);
    PutObjectRequest               request(BucketName, ObjectName, content);


    auto outcome = client.PutObject(request);
    if (!outcome.isSuccess())
    {
        std::cout << "PutObject fail" <<
            ",code:" << outcome.error().Code() <<
            ",message:" << outcome.error().Message() <<
            ",requestId:" << outcome.error().RequestId() << std::endl;
        ShutdownSdk();
    }


    /* 释放网络等资源 */
    ShutdownSdk();
}

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值