頭文件:
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include "mywidget.h"
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
Ui::MainWindow *ui;
private slots:
void OnSelectedBtn(QRect a);
};
#endif // MAINWINDOW_H
mywidget.h(繼承於QWidget)
#ifndef MYWIDGET_H
#define MYWIDGET_H
#include <QWidget>
#include <QPixmap>
#include <QPainter>
#include <QPaintEvent>
#include <QMouseEvent>
#include <QPoint>
class MyWidget : public QWidget
{
Q_OBJECT
public:
explicit MyWidget(QWidget *parent = 0);
signals:
void selected(QRect a);//信號
public slots:
private:
void paintEvent(QPaintEvent *event); //繪圖函數
void mouseMoveEvent(QMouseEvent *event); //鼠標移動
void mousePressEvent(QMouseEvent *event); //鼠標點擊
void mouseReleaseEvent(QMouseEvent *event); //鼠標擡起
private:
QPixmap my_picture;
bool m_pressedFlag;//獲取鼠標是否按下
QPoint currPos;//鼠標當前位置
QPoint startPos,endPos;//起始、結束、座標
};
#endif // MYWIDGET_H
源文件:
main.cpp
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
// MyWidget p;
// p.resize(400,300);
// p.move(200,200);
// p.show();
return a.exec();
}
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
connect(ui->selectedwidget,SIGNAL(selected(QRect)),
this,SLOT(OnSelectedBtn(QRect)));
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::OnSelectedBtn(QRect a)
{
qDebug() << "關聯......";
}
mywidget.cpp
#include "mywidget.h"
#include <QtGui>
MyWidget::MyWidget(QWidget *parent) :
QWidget(parent)
{
m_pressedFlag = false;
startPos = endPos = QPoint(0,0);
my_picture.load("D:/QT_Library/QJie_Tu/1.jpg");//圖片存放位置
}
void MyWidget::paintEvent(QPaintEvent *event)//繪圖函數
{
QPainter painter(this);
int width = this->width();
int height = this->height();
painter.drawPixmap(0,0,width,height,my_picture);//添加本地圖片作爲背景
if(m_pressedFlag)//如果鼠標按下
{
//構建十字交叉線
painter.setPen(QColor(0xFF,0,0));//設置線條顏色
painter.drawLine(QPoint(0,currPos.y()),QPoint(width,currPos.y()));//X軸
painter.drawLine(QPoint(currPos.x(),0),QPoint(currPos.x(),height));//Y軸
}
QRect select(startPos,endPos);//構建截圖選擇框
//普通畫法
// painter.setBrush(QColor(0x88,0x88,0x88));
// painter.drawRect(select);
//透明度畫法
QRect full(0,0,width,height);
painter.setBrush(QColor(0,0,0,100));
painter.setClipRegion(QRegion(full)-QRegion(select));
painter.drawRect(full);
}
void MyWidget::mouseMoveEvent(QMouseEvent *event)//鼠標移動
{
if(m_pressedFlag)
{
currPos = event->pos();
endPos = event->pos();
update();
}
}
void MyWidget::mousePressEvent(QMouseEvent *event)//鼠標按下
{
m_pressedFlag = true;
startPos = endPos = event->pos();//截圖框開始座標
}
void MyWidget::mouseReleaseEvent(QMouseEvent *event)//鼠標擡起
{
m_pressedFlag = false;
update();
QRect a(startPos,endPos);
emit selected(a);//發射信號
}
設計界面:(選擇Widget控件,提升爲自定義控件)
效果圖: