用qt实现类似qq截图的工具

 
#include "widget.h"
#include "ui_widget.h"
#include <QtGui>

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

Widget::~Widget()
{
    delete ui;

    delete quit;
    delete mini;
    delete restore;
    delete menu;
    delete trayIcon;

    delete fullScreenLabel;
    delete shotScreenLabel;
}

bool Widget::eventFilter(QObject *o, QEvent *e)
{
    if (o != fullScreenLabel)
    {
        return Widget::eventFilter(o, e);
    }

    QMouseEvent *mouseEvent = static_cast<QMouseEvent*> (e);

    //true 鼠标左键按下且按键还未弹起
    if ((mouseEvent->button() == Qt::LeftButton)
        && (mouseEvent->type() == QEvent::MouseButtonPress))
    {
        //鼠标左键标志位按下
        leftMousePress = true;

        //获取鼠标点
        origin = mouseEvent->pos();

        if (!rubberBand)
        {
            rubberBand = new QRubberBand(QRubberBand::Rectangle, fullScreenLabel);
        }

        rubberBand->setGeometry(QRect(origin,QSize()));
        rubberBand->show();

        return true;
    }

    //true 鼠标左键按下并拖动
    if ((mouseEvent->type() == QEvent::MouseMove)
        && (leftMousePress))
    {
        if (rubberBand)
        {
            rubberBand->setGeometry(QRect(origin, mouseEvent->pos()).normalized());
        }

        return true;
    }

    //鼠标左键松开
    if ((mouseEvent->button() == Qt::LeftButton)
        && (mouseEvent->type() == QEvent::MouseButtonRelease))
    {
        //鼠标标志位弹起
        leftMousePress = false;

        if (rubberBand)
        {
            //获取橡皮筋框的终止坐标
            termination = mouseEvent->pos();
            QRect rect = QRect(origin, termination);

            //根据橡皮筋框截取全屏上的信息,并将其放入shotScreenLabel
            shotScreenLabel->setPixmap(fullScreenPixmap.grabWidget(fullScreenLabel,
                                                                   rect.x(),
                                                                   rect.y(),
                                                                   rect.width(),
                                                                   rect.height()));

            //将shotScreenLabel的用户区大小固定为所截图片大小
            shotScreenLabel->setFixedSize(rect.width(), rect.height());
            shotScreenLabel->show();

            rubberBand->hide();
            fullScreenLabel->hide();
        }

        return true;
    }

    return false;
}

/**
  descr:实例化控件
*/
void Widget::createWidgets()
{
    //两个QLabel的父控件不能为this,否则截图信息会现在是主窗口中,无法正确显示
    fullScreenLabel = new QLabel();
    shotScreenLabel = new QLabel();

    rubberBand = new QRubberBand(QRubberBand::Rectangle, fullScreenLabel);

    leftMousePress = false;

    //初始化托盘控件并组装**************************************************************

    trayIcon = new QSystemTrayIcon(QIcon(tr(":/images/heart.svg")), this);
    menu = new QMenu(this);
    restore = new QAction(tr("Restore"), this);
    mini = new QAction(tr("Mini"), this);
    quit = new QAction(tr("Quit"), this);

    menu->addAction(restore);
    menu->addAction(mini);
    menu->addAction(quit);
    trayIcon->setContextMenu(menu);

    //将托盘显示
    trayIcon->show();

    //初始化托盘控件并组装**************************************************************

    savePixmap = new QAction(tr("save"), shotScreenLabel);

    shotScreenLabel->addAction(savePixmap);
    shotScreenLabel->setContextMenuPolicy(Qt::ActionsContextMenu);
}

void Widget::createConnects()
{
    //主窗口信号槽*****************************************************************

    connect(ui->pbtnShot, SIGNAL(clicked()), this, SLOT(grapWindowScreen()));
    connect(ui->pbtnShotAndMin, SIGNAL(clicked()), this, SLOT(miniWindows()));
    connect(ui->pbtnMin, SIGNAL(clicked()), this, SLOT(miniWindows()));

    connect(savePixmap, SIGNAL(triggered()), this, SLOT(saveShotPixmap()));

    //主窗口信号槽*****************************************************************

    //托盘信号槽*******************************************************************

    connect(restore, SIGNAL(triggered()), this, SLOT(restoreWindows()));
    connect(mini, SIGNAL(triggered()), this, SLOT(miniWindows()));
    connect(quit, SIGNAL(triggered()), this, SLOT(quitApplication()));

    //托盘信号槽*******************************************************************
}

void Widget::createEventFilter()
{
    fullScreenLabel->installEventFilter(this);
}

QString Widget::getSaveShotPixmap()
{
    return QFileDialog::getSaveFileName(shotScreenLabel,
                                        tr("Open Image"),
                                        ".",
                                        tr("Image Files(*.JPG *.PNG)"));
}

void Widget::grapWindowScreen()
{
    if (!fullScreenLabel)
    {
        fullScreenLabel = new QLabel();
    }

    //获取全屏截图fullScreenPixmap,并将其放入fullScreenLabel
    fullScreenPixmap = QPixmap::grabWindow(QApplication::desktop()->winId());
    fullScreenLabel->setPixmap(fullScreenPixmap);

    //label全屏显示
    fullScreenLabel->showFullScreen();
}

void Widget::miniWindows()
{
    showMinimized();
    grapWindowScreen();
}

void Widget::restoreWindows()
{
    showNormal();
}

void Widget::quitApplication()
{
    qApp->quit();
}

void Widget::saveShotPixmap()
{
    QString fileName = getSaveShotPixmap();

    if (!fileName.isNull())
    {
        fullScreenPixmap.save(fileName);
    }

}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现登陆、注册功能的过程如下: 1.设计登陆界面 首先,我们需要设计一个类似QQ的登陆界面。可以使用Qt提供的UI设计工具进行设计,或者手动编写代码实现。 2.连接数据库 接下来,我们需要连接到数据库,使用Qt提供的QtSQL模块可以很方便地实现。需要在代码中加入以下代码: ```cpp #include <QtSql> //连接数据库 QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL"); db.setHostName("localhost"); //服务器地址 db.setDatabaseName("mydatabase"); //数据库名称 db.setUserName("root"); //用户名 db.setPassword("123456"); //密码 if (!db.open()) { qDebug() << "Database error:" << db.lastError().text(); return; } ``` 其中,需要根据实际情况设置服务器地址、数据库名称、用户名和密码。 3.实现注册功能 在登陆界面中添加一个“注册”按钮,点击后跳转到注册界面。注册界面中需要输入用户名、密码和确认密码,点击“注册”按钮后将数据保存到数据库中。具体实现代码如下: ```cpp //注册按钮点击事件 void MainWindow::on_registerBtn_clicked() { QString username = ui->usernameEdit->text(); QString password = ui->passwordEdit->text(); QString confirm = ui->confirmEdit->text(); if (username.isEmpty() || password.isEmpty() || confirm.isEmpty()) { QMessageBox::warning(this, "Warning", "Please enter username and password."); return; } if (password != confirm) { QMessageBox::warning(this, "Warning", "Password doesn't match."); return; } QSqlQuery query; query.prepare("INSERT INTO users (username, password) VALUES (?, ?)"); query.addBindValue(username); query.addBindValue(password); if (!query.exec()) { qDebug() << "Insert error:" << query.lastError().text(); QMessageBox::warning(this, "Warning", "Fail to register."); return; } QMessageBox::information(this, "Information", "Register successfully."); } ``` 其中,需要在数据库中创建一个名为“users”的表,包含“username”和“password”两个字段。 4.实现登陆功能 在登陆界面中添加“登陆”按钮,点击后检查输入的用户名和密码是否正确。如果正确,跳转到主界面;如果不正确,弹出提示框。具体实现代码如下: ```cpp //登陆按钮点击事件 void MainWindow::on_loginBtn_clicked() { QString username = ui->usernameEdit->text(); QString password = ui->passwordEdit->text(); if (username.isEmpty() || password.isEmpty()) { QMessageBox::warning(this, "Warning", "Please enter username and password."); return; } QSqlQuery query; query.prepare("SELECT * FROM users WHERE username = ? AND password = ?"); query.addBindValue(username); query.addBindValue(password); if (!query.exec() || !query.next()) { qDebug() << "Select error:" << query.lastError().text(); QMessageBox::warning(this, "Warning", "Incorrect username or password."); return; } QMessageBox::information(this, "Information", "Login successfully."); //跳转到主界面 hide(); MainWidget mainWidget; mainWidget.show(); } ``` 其中,需要在主界面中编写代码,用于处理用户登陆后的操作。 以上就是实现登陆、注册功能的详细过程。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值