QT 多屏幕截图

目前百度能百度到的,都是让大家调用以下方法获取屏幕

QGuiApplication::primaryScreen();

但这个函数只能获取主屏幕,对多个显示器的截屏就完全不好使了。

所以用以下方法获取所有显示器,并判断当前鼠标、界面位于哪个显示器,即可实现多个屏幕的截图。

int curMonitor = deskTop->screenNumber ( this );
this->baseScreen = screens.at(curMonitor);

截图主要事件为鼠标按下,鼠标移动,鼠标抬起。需要一个全局的点来标记鼠标最开始按下时的点。

以下为三个事件的处理:

void QtScreenShot::mousePressEvent(QMouseEvent* e)
{
    this->startPoint = e->pos();
    this->startGlobalPoint = e->globalPos();
}

void QtScreenShot::mouseMoveEvent(QMouseEvent* e)
{
    QPoint curPoint;
    curPoint = e->pos();
    if (hasArea)
    {
        qDebug() << "return";
        return;
    }
    int posX = startPoint.x();
    int posY = startPoint.y();
    int posWid = curPoint.x() - startPoint.x();
    int posHig = curPoint.y() - startPoint.y();

    this->editArea->setGeometry(posX, posY, posWid, posHig);
    QImage ima = this->getSelectImage(posX, posY, posWid, posHig);
    this->editArea->setBgImage(ima);
    this->editArea->show();
}

void QtScreenShot::mouseReleaseEvent(QMouseEvent* e)
{
    if (e->button() == Qt::RightButton)
    {
        closeAll();
        this->accept();
        return;
    }
    if (hasArea)
    {
        qDebug() << "return";
        return;
    }
    QPoint curPoint;
    curPoint = e->pos();
    int posX = startPoint.x();
    int posY = startPoint.y();
    int posWid = curPoint.x() - startPoint.x();
    int posHig = curPoint.y() - startPoint.y();

    QPoint glcurPoint;
    glcurPoint = e->globalPos();
    this->globalSelectArea.setRect(startGlobalPoint.x(), startGlobalPoint.y(), glcurPoint.x() - startGlobalPoint.x(), glcurPoint.y() - startGlobalPoint.y());
    this->editArea->setGeometry(posX, posY, posWid, posHig);
    QImage ima = this->getSelectImage(posX, posY, posWid, posHig);
    this->editArea->setBgImage(ima);
    this->editArea->show();
    this->toolBar->setPos(curPoint.x(), curPoint.y());
    this->toolBar->show();
    this->hasArea = true;
}

以下为我写的一个demo。测试界面位于哪个屏幕,就可以在哪个屏幕截图,同时还提供操作撤销功能。

如果需要鼠标位于什么屏幕就在哪个屏幕截图,仅需增加一个定时器,在定时器的槽函数中判断当前鼠标的屏幕并重新初始化。(注意根据hasArea判断是否已经取了截图区域)

具体的demo包括DrawingOperationStack(操作栈)、ScreenShotEditArea(截图编辑区)、ScreenShotToolBar(操作栏)三个部分组成。工程我放入了百度网盘中,链接如下:

链接:https://pan.baidu.com/s/1OaHkAlVBiWPx5FU2z3IMrw 
提取码:gogo

具体的代码也贴出来:

测试用的widget(拖进来个按钮和label,按钮用于触发截图,label显示图片):

#include "widget.h"
#include "ui_widget.h"
#include <QDesktopWidget>
#include <QLabel>
Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    this->hide();
}

Widget::~Widget()
{
    delete ui;
}

void Widget::on_pushButton_clicked()
{
    QDesktopWidget* deskTop = QApplication::desktop();
    int curMonitor = deskTop->screenNumber(this);
    st = new QtScreenShot(curMonitor,this);
    st->setParent(this);
    connect(st,SIGNAL(onScreenShotOK()),this,SLOT(closeScreenShot()));
    st->exec();
}

void Widget::closeScreenShot()
{
    QImage image = st->getScreenShotImage();
    image = image.scaled(ui->label_1->size(), Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
    QPixmap im = QPixmap::fromImage(image);
    ui->label_1->setPixmap(im);
    st->accept();
}

drawingoperationstack.h:

#ifndef DRAWINGOPERATIONSTACK_H
#define DRAWINGOPERATIONSTACK_H

#include <QObject>
#include <QRect>
#include <QStack>
struct DrawingOperation{
    int curMode;
    QRect recogRect;
    QRect tempRect;
};

class DrawingOperationStack
{
public:
    DrawingOperationStack();
    void pushInStack(int curMode, QRect rectr, QRect rectt);
    DrawingOperation popStack();
    DrawingOperation getCurOperation();
    bool isEmptyStack();
private:
    QStack<DrawingOperation*> stack;

};

#endif // DRAWINGOPERATIONSTACK_H

drawingoperationstack.cpp

#include "drawingoperationstack.h"
#include <QDebug>
DrawingOperationStack::DrawingOperationStack()
{
    this->stack.clear();
}

void DrawingOperationStack::pushInStack(int curMode, QRect rectr, QRect rectt)
{
    DrawingOperation *st;
    st = new DrawingOperation;
    st->curMode = curMode;
    st->recogRect = rectr;
    st->tempRect = rectt;
    //qDebug()<<st->mode<<st->rect;
    this->stack.push(st);
}

DrawingOperation DrawingOperationStack::popStack()
{
    DrawingOperation op;
    op.curMode = -1;
    op.recogRect = QRect(0,0,0,0);
    op.tempRect = QRect(0,0,0,0);

    if(this->stack.isEmpty())
    {
        return op;
    }
    DrawingOperation *opera = this->stack.pop();
    op.recogRect = opera->recogRect;
    op.tempRect = opera->tempRect;
    op.curMode = opera->curMode;
    //this->stack.pop();
    delete opera;
    //qDebug()<<op.mode << op.rect;
    return op;
}

DrawingOperation DrawingOperationStack::getCurOperation()
{
    DrawingOperation op;
    op.curMode = -1;
    op.recogRect = QRect(0,0,0,0);
    op.tempRect = QRect(0,0,0,0);
    if(this->stack.isEmpty())
    {
        return op;
    }
    DrawingOperation *opera = this->stack.back();
    op.recogRect = opera->recogRect;
    op.tempRect = opera->tempRect;
    op.curMode = opera->curMode;
    return op;
}

bool DrawingOperationStack::isEmptyStack()
{
    return this->stack.isEmpty();
}

qtscreenshoteditarea.h

#ifndef QTSCREENSHOTEDITAREA_H
#define QTSCREENSHOTEDITAREA_H

#include <QObject>
#include <QWidget>
#include "drawingoperationstack.h"
class QtScreenShotEditArea : public QWidget
{
    Q_OBJECT
public:
    explicit QtScreenShotEditArea(QWidget *parent = nullptr);

    void setBgImage(QImage &image);
    void paintEvent(QPaintEvent *event);
    void setMode(int mode);
    void setOpRect(QRect rectR,QRect rectT);
    QImage getImage();
protected:
    void mousePressEvent(QMouseEvent *e);//鼠标按下事件
    void mouseMoveEvent(QMouseEvent *e);//鼠标按下后移动事件
    void mouseReleaseEvent(QMouseEvent *e);//鼠标按下后抬起事件
private:
    QRect tempRect;
    QPoint startPoint;
    QRect recogRect;
    QImage bgImage;
    int curMode;
signals:
    void emitOperation(DrawingOperation op);
};

#endif // QTSCREENSHOTEDITAREA_H

qtscreenshoteditarea.cpp

#include "qtscreenshoteditarea.h"
#include <QPainter>
#include <QRect>
#include <QMouseEvent>
#include <QDebug>
QtScreenShotEditArea::QtScreenShotEditArea(QWidget *parent) : QWidget(parent)
{
    this->recogRect = QRect(0,0,0,0);
    this->tempRect = QRect(0,0,0,0);
    setWindowFlags(windowFlags() | Qt::FramelessWindowHint|Qt::FramelessWindowHint|Qt::WindowStaysOnTopHint);
    bgImage = QImage(0,0,QImage::Format_RGB32);
    setAttribute(Qt::WA_TranslucentBackground, true);
    curMode = -1;

}

void QtScreenShotEditArea::setBgImage(QImage &image)
{
    this->bgImage = image;
    this->update();
}

void QtScreenShotEditArea::paintEvent(QPaintEvent *event)
{
     QPainter painter(this);
     if(bgImage.size().width() == 0 && bgImage.size().height() == 0)
     {
         return;
     }
     QPoint pt(0,0);
     QColor recogColor(223,80,80);
     QColor tempColor(0,137,255);
     painter.drawImage(pt,bgImage);
     if(recogRect.width() != 0 && recogRect.height() != 0)
     {
         QPoint pt1(recogRect.x(),recogRect.y());
         QPoint pt2(recogRect.x(),recogRect.y() + recogRect.height());
         QPoint pt3(recogRect.x() + recogRect.width(), recogRect.y() + recogRect.height());
         QPoint pt4(recogRect.x() + recogRect.width(), recogRect.y());
         painter.setPen(QPen(recogColor));
         painter.drawLine(pt1,pt2);
         painter.drawLine(pt2,pt3);
         painter.drawLine(pt3,pt4);
         painter.drawLine(pt4,pt1);
     }

     if(tempRect.width() !=0 &&tempRect.height() != 0)
     {
         QPoint pt1(tempRect.x(),tempRect.y());
         QPoint pt2(tempRect.x(),tempRect.y() + tempRect.height());
         QPoint pt3(tempRect.x() + tempRect.width(), tempRect.y() + tempRect.height());
         QPoint pt4(tempRect.x() + tempRect.width(), tempRect.y());
         painter.setPen(QPen(tempColor));
         painter.drawLine(pt1,pt2);
         painter.drawLine(pt2,pt3);
         painter.drawLine(pt3,pt4);
         painter.drawLine(pt4,pt1);
     }

}

void QtScreenShotEditArea::setMode(int mode)
{
    curMode = mode;
}

void QtScreenShotEditArea::setOpRect(QRect rectR,QRect rectT)
{

    this->tempRect = rectT;

    this->recogRect = rectR;
    //qDebug()<<this->tempRect<<this->recogRect;
    this->update();
}

QImage QtScreenShotEditArea::getImage()
{
    QImage image;
    QPixmap pix;
    pix = this->grab(QRect(0,0,this->width(),this->height()));
    image = pix.toImage();

    return image;
}

void QtScreenShotEditArea::mousePressEvent(QMouseEvent *e)
{
    this->startPoint = e->pos();
}

void QtScreenShotEditArea::mouseMoveEvent(QMouseEvent *e)
{
    QPoint pt = e->pos();
    QRect curRect = QRect(startPoint.x(),startPoint.y(), pt.x() - startPoint.x(), pt.y() - startPoint.y());
    if(curMode ==0)
    {
        this->recogRect = curRect;
    }
    if(curMode == 1)
    {
        this->tempRect = curRect;
    }
    this->update();
}

void QtScreenShotEditArea::mouseReleaseEvent(QMouseEvent *e)
{
    QPoint pt = e->pos();
    QRect curRect = QRect(startPoint.x(),startPoint.y(), pt.x() - startPoint.x(), pt.y() - startPoint.y());
    DrawingOperation op;
    op.curMode =curMode;
    if(curMode == -1)
    {
        return;
    }
    if(curMode ==0)
    {
        this->recogRect = curRect;

    }
    if(curMode == 1)
    {
        this->tempRect = curRect;
    }
    op.recogRect = this->recogRect;
    op.tempRect = this->tempRect;
    //qDebug()<<op.curMode<<op.recogRect<<op.tempRect;
    emit emitOperation(op);
    this->update();
}

qtscreenshottoolbar.h

#ifndef QTSCREENSHOTTOOLBAR_H
#define QTSCREENSHOTTOOLBAR_H

#include <QObject>
#include <QWidget>
#include <QMouseEvent>
class QtScreenShotToolBar : public QWidget
{
    Q_OBJECT
public:
    explicit QtScreenShotToolBar(QWidget *parent = nullptr);

    void setPos(int x, int y);
    void setMode(int mode);
    void init();

    void paintEvent(QPaintEvent *event);
    void resetChosen(int num);
protected:
    void mouseReleaseEvent(QMouseEvent *e);//鼠标按下后抬起事件

private:
    bool bChosen[5];
signals:
    void setDrawMode(int mode);
    void removeDrawMode(int mode);
};

#endif // QTSCREENSHOTTOOLBAR_H

qtscreenshottoolbar.cpp

#include "qtscreenshottoolbar.h"
#include <QPainter>
#include <QRect>
#include <QDebug>

QtScreenShotToolBar::QtScreenShotToolBar(QWidget *parent) : QWidget(parent)
{
    //this->setGeometry(100,100,100,20);
    setWindowFlags(windowFlags() | Qt::FramelessWindowHint|Qt::FramelessWindowHint|Qt::WindowStaysOnTopHint);
    setAttribute(Qt::WA_TranslucentBackground, true);

    this->init();
    this->update();
}

void QtScreenShotToolBar::setPos(int x, int y)
{
    int curX = x- 100;
    if(curX<0)
    {
        curX = 0;
    }
    this->setGeometry(curX,y,100,20);
    this->update();
}

void QtScreenShotToolBar::setMode(int mode)
{

    bChosen[mode] = true;
    resetChosen(mode);
    qDebug()<<mode<<bChosen[mode];
    qDebug()<<"ready to update";
    this->update();
}

void QtScreenShotToolBar::init()
{
    //this->setGeometry(100,100,100,20);
    for(int i=0;i<5;i++)
    {
        bChosen[i] = false;
    }
//    this->set
}

void QtScreenShotToolBar::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);
    qDebug()<<"painting!";
    for(int i=0;i<5;i++)
    {
        qDebug()<<bChosen[i];
    }
    QColor bgcolor(81,162,249);
    QColor darkColor(0,118,246);
    QColor penColor(0,50,100);
    /*绘制圆角四四边形的底*/
    /**/
    painter.setPen(QPen(penColor));
    painter.setBrush(QBrush(bgcolor));
    painter.drawRect(0,0,100,20);

    /*绘制五个图形*/
    /*绘制第一个区域的长方体*/
    /**/
    QRect recRect = QRect(3,4,14,12);
    QRect bgRect = QRect(0,0,20,20);
    //qDebug()<<recRect;
    if(bChosen[0])
    {
        painter.setBrush(QBrush(darkColor));
    }
    else
    {
        painter.setBrush(QBrush(bgcolor));
    }
    painter.drawRect(bgRect);
    painter.drawRect(recRect);

    /*绘制第二个区域的圆形*/
    /**/
    if(bChosen[1])
    {
        painter.setBrush(QBrush(darkColor));
    }
    else
    {
        painter.setBrush(QBrush(bgcolor));
    }
    bgRect.setX(bgRect.x() + 20);
    bgRect.setWidth(20);
    painter.drawRect(bgRect);
    recRect.setX(recRect.x()+20);
    recRect.setWidth(14);
    //qDebug()<<recRect;
    painter.drawRoundedRect(recRect,10,5);

    /*绘制第三个区域的图形*/
    /**/
    if(bChosen[2])
    {
        painter.setBrush(QBrush(darkColor));
    }
    else
    {
        painter.setBrush(QBrush(bgcolor));
    }
    bgRect.setX(bgRect.x() + 20);
    bgRect.setWidth(20);
    painter.drawRect(bgRect);
    recRect.setX(recRect.x()+20);
    recRect.setWidth(14);
    painter.drawLine(recRect.x()+1,recRect.y()+6,recRect.x()+13,recRect.y()+6);
    painter.drawLine(recRect.x()+1,recRect.y()+6,recRect.x()+6,recRect.y() + 3);
    painter.drawLine(recRect.x()+1,recRect.y()+6,recRect.x()+6,recRect.y() + 9);

    /*绘制第四个区域的图形*/
    /**/
    if(bChosen[3])
    {
        painter.setBrush(QBrush(darkColor));
    }
    else
    {
        painter.setBrush(QBrush(bgcolor));
    }
    bgRect.setX(bgRect.x() + 20);
    bgRect.setWidth(20);
    painter.drawRect(bgRect);
    recRect.setX(recRect.x()+20);
    recRect.setWidth(14);
    //qDebug()<<recRect;
    painter.drawLine(recRect.x() + 2,recRect.y()+6,recRect.x()+5,recRect.y()+11);
    painter.drawLine(recRect.x()+5,recRect.y()+11,recRect.x()+13,recRect.y()+3);

    /*绘制第五个区域的图形*/
    /**/
    if(bChosen[4])
    {
        painter.setBrush(QBrush(darkColor));
    }
    else
    {
        painter.setBrush(QBrush(bgcolor));
    }
    bgRect.setX(bgRect.x() + 20);
    bgRect.setWidth(20);
    painter.drawRect(bgRect);
    recRect.setX(recRect.x()+20);
    recRect.setWidth(14);
   // qDebug()<<recRect;
    painter.drawLine(recRect.x()+1,recRect.y()+1,recRect.x()+12,recRect.y()+12);
    painter.drawLine(recRect.x()+12,recRect.y()+1,recRect.x()+1,recRect.y()+12);

    /*绘制四条个竖线*/
    /**/
    painter.setPen(QPen(penColor));
    painter.setBrush(QBrush(darkColor));
    for(int i =1;i<5;i++)
    {
        painter.drawLine(0+ i*20,0 + 0 ,0+i*20, 20);
    }

}

void QtScreenShotToolBar::mouseReleaseEvent(QMouseEvent *e)
{
    QPoint pt = e->pos();
    if(pt.y()<0 || pt.y()>20)
    {
        return;
    }
    int chosenNum = 0;
    if(pt.x() < 0)
    {
        return;
    }
    else if(pt.x() < 20)
    {
        chosenNum = 0;
    }
    else if(pt.x() < 40)
    {
        chosenNum = 1;
    }
    else if(pt.x() < 60)
    {
        chosenNum = 2;
    }
    else if(pt.x() < 80)
    {
        chosenNum = 3;
    }
    else if(pt.x()<100)
    {
        chosenNum = 4;
    }
    else{
        return;
    }
    bChosen[chosenNum] = !bChosen[chosenNum];
    if(bChosen[chosenNum] == true)
    {
        resetChosen(chosenNum);
        emit setDrawMode(chosenNum);
    }
    else
    {
        resetChosen(chosenNum);
        emit removeDrawMode(chosenNum);
    }
    if(chosenNum > 1)
    {
        bChosen[chosenNum] = false;
    }


    this->update();
}

void QtScreenShotToolBar::resetChosen(int num)
{
    for(int i=0; i<5;i++)
    {
        if(i != num)
        {
            bChosen[i] = false;
        }
    }
}

qtscreenshot.h

#ifndef QTSCREENSHOT_H
#define QTSCREENSHOT_H
#include <QObject>
#include <QWidget>
#include <QDialog>
#include <QPoint>
#include <QScreen>
#include <QMouseEvent>
#include "qtscreenshottoolbar.h"
#include "qtscreenshoteditarea.h"
#include "drawingoperationstack.h"
class QtScreenShot :public QDialog
{
    Q_OBJECT

public:

public:
    explicit QtScreenShot(int curScreen, QWidget* parent = nullptr);

    void init(int curScreen);
    void ShowToolBar();
    void paintEvent(QPaintEvent* event);
    QImage getScreenShotImage();
    QImage getFullScreenImage();
    void closeAll();
    bool hasGotImage;
public slots:
    void onDrawModeSet(int mode);
    void onDrawModeRemove(int mode);
    void onGetOperation(DrawingOperation op);
    //void reject();
protected:
    void mousePressEvent(QMouseEvent* e);//鼠标按下事件
    void mouseMoveEvent(QMouseEvent* e);//鼠标按下后移动事件
    void mouseReleaseEvent(QMouseEvent* e);//鼠标按下后抬起事件

private:
    QScreen* baseScreen;//屏幕截图
    QRect selectArea;//选中截图区域大小(相对)
    QRect globalSelectArea;
    QRect selectTempArea;
    QRect globalTempArea;
    QRect selectRecogArea;
    QRect globalSelectRecogArea;
    QtScreenShotToolBar *toolBar;//截图工具栏
    QtScreenShotEditArea *editArea;//截图编辑界面
    QPoint startPoint;
    QPoint startGlobalPoint;
    QImage baseImage;
    DrawingOperationStack operationStack;
    int curMode;
    int curScreen;
    QImage ansImage;
private:
    QImage getSelectImage(int x, int y, int width, int height);
    void bindSignalSlots();
    QImage AdjustBrightness(QImage Img, int iBrightValue);
    void AutoUnOperation(DrawingOperation delOperation, DrawingOperation curOperation);
    bool hasArea;

signals:
    void onScreenShotOK();
};

#endif // QTSCREENSHOT_H

qtscreenshot.cpp

#include "qtscreenshot.h"
#include <QGuiApplication>
#include <QPainter>
#include <QImage>
#include <QSize>
#include <QDebug>
#include <QDesktopWidget>
#include <QApplication>
QtScreenShot::QtScreenShot(int curScreen, QWidget* parent) :
    QDialog(parent)
{
    this->init(curScreen);

}

void QtScreenShot::init(int curScreen)
{
    this->selectArea = QRect(-1, -1, -1, -1);
    this->selectRecogArea = QRect(-1, -1, -1, -1);
    this->selectTempArea = QRect(-1, -1, -1, -1);
    QList<QScreen*> screens = QGuiApplication::screens();
    qDebug() << screens.size();

    qDebug() << curScreen;
    this->baseScreen = screens.at(curScreen);
    QRect rectCurWin = this->baseScreen->geometry();;
    qDebug() << rectCurWin;
    QSize siz = this->baseScreen->size();
    this->setGeometry(rectCurWin);

    setWindowFlags(windowFlags() | Qt::FramelessWindowHint | Qt::FramelessWindowHint);
    setAttribute(Qt::WA_TranslucentBackground, true);

    this->hasGotImage = false;
    this->editArea = new QtScreenShotEditArea(this);
    this->editArea->hide();
    this->toolBar = new QtScreenShotToolBar(this);
    this->toolBar->hide();

    this->baseImage = baseScreen->grabWindow(0).toImage();
    this->curMode = -1;
    this->hasArea = false;
    this->bindSignalSlots();
}

void QtScreenShot::bindSignalSlots()
{
    connect(toolBar, SIGNAL(setDrawMode(int)), this, SLOT(onDrawModeSet(int)));
    connect(toolBar, SIGNAL(removeDrawMode(int)), this, SLOT(onDrawModeRemove(int)));
    connect(editArea, SIGNAL(emitOperation(DrawingOperation)), this, SLOT(onGetOperation(DrawingOperation)));
}

QImage QtScreenShot::AdjustBrightness(QImage Img, int iBrightValue)
{
    int red, green, blue;
    int pixels = Img.width() * Img.height();
    unsigned int* data = (unsigned int*)Img.bits();

    for (int i = 0; i < pixels; ++i)
    {
        red = qRed(data[i]) + iBrightValue;
        red = (red < 0x00) ? 0x00 : (red > 0xff) ? 0xff : red;
        green = qGreen(data[i]) + iBrightValue;
        green = (green < 0x00) ? 0x00 : (green > 0xff) ? 0xff : green;
        blue = qBlue(data[i]) + iBrightValue;
        blue = (blue < 0x00) ? 0x00 : (blue > 0xff) ? 0xff : blue;
        data[i] = qRgba(red, green, blue, qAlpha(data[i]));
    }

    return Img;

}

void QtScreenShot::AutoUnOperation(DrawingOperation delOperation, DrawingOperation curOperation)
{
    /**/
    /**/
    if (delOperation.curMode == -1)
    {
        this->hasArea = false;
        editArea->hide();
        toolBar->hide();
    }
    else
    {
        /*无论什么操作,先清空*/
        /**/
        QRect rect(0, 0, 0, 0);
        editArea->setOpRect(rect, rect);
        editArea->setMode(-1);
        /*可还原再还原*/
        /**/
        if (curOperation.curMode != -1)
        {
            editArea->setMode(curOperation.curMode);
            editArea->setOpRect(curOperation.recogRect, curOperation.tempRect);
            this->toolBar->setMode(curOperation.curMode);
        }
    }

}
void QtScreenShot::ShowToolBar()
{

}

void QtScreenShot::paintEvent(QPaintEvent* event)
{
    QPainter painter(this);
    QImage image = getFullScreenImage();
    image = AdjustBrightness(image, -30);
    painter.drawImage(0, 0, image);
}

QImage QtScreenShot::getScreenShotImage()
{
    QImage pixmap = this->editArea->getImage();
    this->ansImage = pixmap;
    return this->ansImage;
}

QImage QtScreenShot::getFullScreenImage()
{
    return baseImage;
}

void QtScreenShot::closeAll()
{
    while (!operationStack.isEmptyStack())
    {
        operationStack.popStack();
    }

}

void QtScreenShot::onDrawModeSet(int mode)
{
    curMode = mode;
    if (mode < 0)
    {
        return;
    }
    else if (mode < 2)
    {
        this->editArea->setMode(mode);
    }
    else if (mode == 2)
    {
        qDebug() << "in redo ";
        DrawingOperation op = operationStack.popStack();
        DrawingOperation op2 = operationStack.getCurOperation();
        AutoUnOperation(op, op2);
    }
    else if (mode == 3)
    {
        emit onScreenShotOK();

    }
    else if (mode == 4)
    {
        this->closeAll();
        this->accept();
    }
    else
    {
        return;
    }

}

void QtScreenShot::onDrawModeRemove(int mode)
{
    curMode = -1;
    if (mode < 0)
    {
        return;
    }
    else if (mode < 2)
    {
        this->editArea->setMode(-1);
    }
    else if (mode == 2)
    {

    }
    else if (mode == 3)
    {
        emit onScreenShotOK();
    }
    else if (mode == 4)
    {

    }
    else
    {
        return;
    }
}

void QtScreenShot::onGetOperation(DrawingOperation op)
{
    this->operationStack.pushInStack(op.curMode, op.recogRect, op.tempRect);
}

void QtScreenShot::mousePressEvent(QMouseEvent* e)
{
    this->startPoint = e->pos();
    this->startGlobalPoint = e->globalPos();
}

void QtScreenShot::mouseMoveEvent(QMouseEvent* e)
{
    QPoint curPoint;
    curPoint = e->pos();
    //qDebug() << "move";
    if (hasArea)
    {
        qDebug() << "return";
        return;
    }
    int posX = startPoint.x();
    int posY = startPoint.y();
    int posWid = curPoint.x() - startPoint.x();
    int posHig = curPoint.y() - startPoint.y();

    this->editArea->setGeometry(posX, posY, posWid, posHig);
    QImage ima = this->getSelectImage(posX, posY, posWid, posHig);
    this->editArea->setBgImage(ima);
    this->editArea->show();
    //qDebug() << "showarea in move";
}

void QtScreenShot::mouseReleaseEvent(QMouseEvent* e)
{
    if (e->button() == Qt::RightButton)
    {
        closeAll();
        this->accept();
        return;
    }
    if (hasArea)
    {
        qDebug() << "return";
        return;
    }
    QPoint curPoint;
    curPoint = e->pos();
    int posX = startPoint.x();
    int posY = startPoint.y();
    int posWid = curPoint.x() - startPoint.x();
    int posHig = curPoint.y() - startPoint.y();

    QPoint glcurPoint;
    glcurPoint = e->globalPos();
    this->globalSelectArea.setRect(startGlobalPoint.x(), startGlobalPoint.y(), glcurPoint.x() - startGlobalPoint.x(), glcurPoint.y() - startGlobalPoint.y());
    this->editArea->setGeometry(posX, posY, posWid, posHig);
    QImage ima = this->getSelectImage(posX, posY, posWid, posHig);
    this->editArea->setBgImage(ima);
    this->editArea->show();
    this->toolBar->setPos(curPoint.x(), curPoint.y());
    this->toolBar->show();
    this->hasArea = true;
    qDebug() << startPoint << curPoint;
    qDebug() << "showarea in release";
}

QImage QtScreenShot::getSelectImage(int x, int y, int width, int height)
{
    QImage pixmap = baseImage;

    return pixmap.copy(x, y, width, height);
}

  • 3
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
### 回答1: Qt是一个跨平台的GUI库,常用于开发桌面应用程序。在多屏环境下,Qt可以很好地支持屏幕缩放比例。 对于多屏环境,Qt提供了一种简单的方法来访问所有屏幕,并且多个屏幕可以在Qt中作为一个具有单一逻辑大小的桌面表现。Qt 5中提供了两种方法来实现屏幕缩放比例:DPI缩放和UI缩放。 DPI缩放是指Qt通过重新计算每个屏幕上的像素密度来适应不同的屏幕,从而使应用程序在多屏幕上保持屏幕分辨率一致。UI缩放是指Qt可以通过缩放UI控件来适应不同的屏幕分辨率,这样就可以缩放应用程序的整个UI界面。 无论是DPI缩放还是UI缩放,都需要对应用程序进行一些调整,并且需要考虑到每个屏幕的分辨率和大小。Qt提供了一些函数和类来优化多屏环境下的应用程序,如QScreen和QWindow等。 综上所述,Qt提供了多种方法来适应多屏幕环境,其中包括DPI缩放和UI缩放。开发人员应该根据具体情况选择合适的缩放方法,并且对应用程序进行必要的调整,以确保在多屏幕环境下有更好的用户体验。 ### 回答2: 在使用Qt多屏开发时,我们可能需要考虑到不同的屏幕分辨率和缩放比例对UI界面的影响,以保证程序在不同设备上具有良好的用户体验。 首先,Qt提供了一些关于屏幕分辨率和缩放比例的接口函数,比如QScreen::availableSize()和QScreen::logicalDotsPerInch()。这些函数可以帮助我们获取当前屏幕的大小和密度信息。同时,Qt还提供了一些函数来响应屏幕缩放比例的变化,比如QWindow::devicePixelRatioChanged()和QWidget::resizeEvent()。 其次,为了适应不同的分辨率和缩放比例,我们需要采取一些措施来调整UI界面的布局和大小。例如,我们可以使用Qt的布局管理器来自动计算和调整界面控件的大小和位置,以适应不同的屏幕分辨率。另外,我们还可以考虑使用矢量图形代替位图图像,以保证图像在不同分辨率下的清晰度。 最后,我们需要进行充分的测试和调试,以确保程序在不同屏幕分辨率和缩放比例下的表现都符合预期。这个过程既可以手动进行,也可以借助一些自动化测试工具来完成。 综上所述,Qt多屏开发需要考虑到不同的屏幕分辨率和缩放比例,采取相应的措施来调整UI界面的布局和大小,并进行充分的测试和调试。 ### 回答3: Qt 是一个跨平台的 C++ 应用程序开发框架,旨在为开发者提供高效且易于使用的工具来创建现代 UI 界面和应用程序。在 Qt 中,多屏和屏幕缩放比例都是需要注意的重要问题。 Qt 通过 QScreen 类提供了多屏支持。通过检索当前系统中的所有屏幕,并使用 QDesktopWidget 类的基本辅助函数计算其大小和位置,您可以获取要素,如应用程序是否覆盖多个屏幕等。在QScreen中,可以通过geometry()和availableGeometry() 函数获取屏幕的宽高和可用宽高。 对于屏幕缩放比例,Qt 允许您在不同的设备上为不同的密度设置缩放比例。DPI (Dots Per Inch) 是测量显示器分辨率的单位,通常用于确定屏幕上图像和文本的大小。在 Qt 中,可以使用QScreen::devicePixelRatio()获取屏幕的缩放比例。如果您想将您的应用程序设计为在不同分辨率和设备上都能正常显示,您需要根据屏幕的实际 DPI 设置计算缩放比例。在 Qt 中,可以使用QApplication::primaryScreen()->logicalDotsPerInch()获取当前屏幕的 DPI 值,然后根据需要采用不同的缩放比例来设置应用程序。 在 Qt 中,多屏和屏幕缩放比例都需要仔细考虑,以确保您的应用程序在不同的设备和分辨率上都能正常显示,并为最终用户提供出色的体验。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值