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

[代码] widget.cpp
view sourceprint?
001
#include "widget.h"
002
#include "ui_widget.h"
003
#include <QtGui>
004
 
005
Widget::Widget(QWidget *parent) :
006
    QWidget(parent),
007
    ui(new Ui::Widget)
008
{
009
    ui->setupUi(this);
010
    createWidgets();
011
    createConnects();
012
    createEventFilter();
013
}
014
 
015
Widget::~Widget()
016
{
017
    delete ui;
018
 
019
    delete quit;
020
    delete mini;
021
    delete restore;
022
    delete menu;
023
    delete trayIcon;
024
 
025
    delete fullScreenLabel;
026
    delete shotScreenLabel;
027
}
028
 
029
bool Widget::eventFilter(QObject *o, QEvent *e)
030
{
031
    if (o != fullScreenLabel)
032
    {
033
        return Widget::eventFilter(o, e);
034
    }
035
 
036
    QMouseEvent *mouseEvent = static_cast<QMouseEvent*> (e);
037
 
038
    //true 鼠标左键按下且按键还未弹起
039
    if ((mouseEvent->button() == Qt::LeftButton)
040
        && (mouseEvent->type() == QEvent::MouseButtonPress))
041
    {
042
        //鼠标左键标志位按下
043
        leftMousePress = true;
044
 
045
        //获取鼠标点
046
        origin = mouseEvent->pos();
047
 
048
        if (!rubberBand)
049
        {
050
            rubberBand = new QRubberBand(QRubberBand::Rectangle, fullScreenLabel);
051
        }
052
 
053
        rubberBand->setGeometry(QRect(origin,QSize()));
054
        rubberBand->show();
055
 
056
        return true;
057
    }
058
 
059
    //true 鼠标左键按下并拖动
060
    if ((mouseEvent->type() == QEvent::MouseMove)
061
        && (leftMousePress))
062
    {
063
        if (rubberBand)
064
        {
065
            rubberBand->setGeometry(QRect(origin, mouseEvent->pos()).normalized());
066
        }
067
 
068
        return true;
069
    }
070
 
071
    //鼠标左键松开
072
    if ((mouseEvent->button() == Qt::LeftButton)
073
        && (mouseEvent->type() == QEvent::MouseButtonRelease))
074
    {
075
        //鼠标标志位弹起
076
        leftMousePress = false;
077
 
078
        if (rubberBand)
079
        {
080
            //获取橡皮筋框的终止坐标
081
            termination = mouseEvent->pos();
082
            QRect rect = QRect(origin, termination);
083
 
084
            //根据橡皮筋框截取全屏上的信息,并将其放入shotScreenLabel
085
            shotScreenLabel->setPixmap(fullScreenPixmap.grabWidget(fullScreenLabel,
086
                                                                   rect.x(),
087
                                                                   rect.y(),
088
                                                                   rect.width(),
089
                                                                   rect.height()));
090
 
091
            //将shotScreenLabel的用户区大小固定为所截图片大小
092
            shotScreenLabel->setFixedSize(rect.width(), rect.height());
093
            shotScreenLabel->show();
094
 
095
            rubberBand->hide();
096
            fullScreenLabel->hide();
097
        }
098
 
099
        return true;
100
    }
101
 
102
    return false;
103
}
104
 
105
/**
106
  descr:实例化控件
107
*/
108
void Widget::createWidgets()
109
{
110
    //两个QLabel的父控件不能为this,否则截图信息会现在是主窗口中,无法正确显示
111
    fullScreenLabel = new QLabel();
112
    shotScreenLabel = new QLabel();
113
 
114
    rubberBand = new QRubberBand(QRubberBand::Rectangle, fullScreenLabel);
115
 
116
    leftMousePress = false;
117
 
118
    //初始化托盘控件并组装**************************************************************
119
 
120
    trayIcon = new QSystemTrayIcon(QIcon(tr(":/images/heart.svg")), this);
121
    menu = new QMenu(this);
122
    restore = new QAction(tr("Restore"), this);
123
    mini = new QAction(tr("Mini"), this);
124
    quit = new QAction(tr("Quit"), this);
125
 
126
    menu->addAction(restore);
127
    menu->addAction(mini);
128
    menu->addAction(quit);
129
    trayIcon->setContextMenu(menu);
130
 
131
    //将托盘显示
132
    trayIcon->show();
133
 
134
    //初始化托盘控件并组装**************************************************************
135
 
136
    savePixmap = new QAction(tr("save"), shotScreenLabel);
137
 
138
    shotScreenLabel->addAction(savePixmap);
139
    shotScreenLabel->setContextMenuPolicy(Qt::ActionsContextMenu);
140
}
141
 
142
void Widget::createConnects()
143
{
144
    //主窗口信号槽*****************************************************************
145
 
146
    connect(ui->pbtnShot, SIGNAL(clicked()), this, SLOT(grapWindowScreen()));
147
    connect(ui->pbtnShotAndMin, SIGNAL(clicked()), this, SLOT(miniWindows()));
148
    connect(ui->pbtnMin, SIGNAL(clicked()), this, SLOT(miniWindows()));
149
 
150
    connect(savePixmap, SIGNAL(triggered()), this, SLOT(saveShotPixmap()));
151
 
152
    //主窗口信号槽*****************************************************************
153
 
154
    //托盘信号槽*******************************************************************
155
 
156
    connect(restore, SIGNAL(triggered()), this, SLOT(restoreWindows()));
157
    connect(mini, SIGNAL(triggered()), this, SLOT(miniWindows()));
158
    connect(quit, SIGNAL(triggered()), this, SLOT(quitApplication()));
159
 
160
    //托盘信号槽*******************************************************************
161
}
162
 
163
void Widget::createEventFilter()
164
{
165
    fullScreenLabel->installEventFilter(this);
166
}
167
 
168
QString Widget::getSaveShotPixmap()
169
{
170
    return QFileDialog::getSaveFileName(shotScreenLabel,
171
                                        tr("Open Image"),
172
                                        ".",
173
                                        tr("Image Files(*.JPG *.PNG)"));
174
}
175
 
176
void Widget::grapWindowScreen()
177
{
178
    if (!fullScreenLabel)
179
    {
180
        fullScreenLabel = new QLabel();
181
    }
182
 
183
    //获取全屏截图fullScreenPixmap,并将其放入fullScreenLabel
184
    fullScreenPixmap = QPixmap::grabWindow(QApplication::desktop()->winId());
185
    fullScreenLabel->setPixmap(fullScreenPixmap);
186
 
187
    //label全屏显示
188
    fullScreenLabel->showFullScreen();
189
}
190
 
191
void Widget::miniWindows()
192
{
193
    showMinimized();
194
    grapWindowScreen();
195
}
196
 
197
void Widget::restoreWindows()
198
{
199
    showNormal();
200
}
201
 
202
void Widget::quitApplication()
203
{
204
    qApp->quit();
205
}
206
 
207
void Widget::saveShotPixmap()
208
{
209
    QString fileName = getSaveShotPixmap();
210
 
211
    if (!fileName.isNull())
212
    {
213
        fullScreenPixmap.save(fileName);
214
    }
215
 
216
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值