Qt第十七天
阿新 • • 發佈:2018-12-16
QGraphicsView程式基本結構和功能實現
mainwindow.h
#ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include <QLabel> #include<QGraphicsScene> #include<QGraphicsItem> #include"qwgraphicsview.h" namespace Ui { class MainWindow; } class MainWindow : public QMainWindow { Q_OBJECT private: QGraphicsScene *scene; QLabel *labViewCord; QLabel *labSceneCord; QLabel *labItemCord; void iniGraphicsSystem(); //建立Graphics View的各項 protected: void resizeEvent(QResizeEvent *event);//重定義了resizeEvent()事件 private slots: void on_mouseMovePoint(QPoint point); void on_mouseClicked(QPoint point); public: explicit MainWindow(QWidget *parent = nullptr); ~MainWindow(); private: Ui::MainWindow *ui; }; #endif // MAINWINDOW_H
qwgraphicsview.h
#ifndef QWGRAPHICSVIEW_H #define QWGRAPHICSVIEW_H #include<QGraphicsView> #include <QObject> #include<qwgraphicsview.h> #include"mainwindow.h" /** * 從QGraphicsView類繼承一個類 * 實現mouseMoveEvent和mousePressEvent事件 * 把滑鼠事件轉換為訊號,在主程式中設計槽函式響應這些滑鼠事件 **/ class QWGraphicsView : public QGraphicsView { Q_OBJECT protected: void mouseMoveEvent(QMouseEvent *event); void mousePressEvent(QMouseEvent *event); public: QWGraphicsView(QWidget *parent=nullptr); signals: void mouseMovePoint(QPoint point); void mouseClicked(QPoint point); }; #endif // QWGRAPHICSVIEW_H
mainwindow.cpp
#include "mainwindow.h" #include "ui_mainwindow.h" #include"qwgraphicsview.h" #include<QFlags> #include<QPen> #include<QGraphicsEllipseItem> #include<QBrush> MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); labViewCord=new QLabel("View 座標:"); labViewCord->setMinimumWidth(150); ui->statusBar->addWidget(labViewCord); labSceneCord=new QLabel("Sence 座標:"); labSceneCord->setMinimumWidth(150); ui->statusBar->addWidget(labSceneCord); labItemCord=new QLabel("Item 座標:"); labItemCord->setMinimumWidth(150); ui->statusBar->addWidget(labItemCord); ui->View->setCursor(Qt::CrossCursor);//指標 ui->View->setMouseTracking(true);//該屬性用來設定某個控制元件是否被跟蹤軌跡。 ui->View->setDragMode(QGraphicsView::RubberBandDrag); QObject::connect(ui->View,SIGNAL(mouseMovePoint(QPoint)),this,SLOT(on_mouseMovePoint(QPoint))); QObject::connect(ui->View,SIGNAL(mouseClicked(QPoint)),this,SLOT(on_mouseClicked(QPoint))); iniGraphicsSystem(); } MainWindow::~MainWindow() { delete ui; } /** * @brief MainWindow::on_mouseClicked * @param point */ void MainWindow::on_mouseClicked(QPoint point)//滑鼠點選事件 { QPointF pointScene=ui->View->mapToScene(point);//轉換到Scene座標 QGraphicsItem *item=nullptr; item=scene->itemAt(pointScene,ui->View->transform());//獲取游標下的圖形項 if(item!=nullptr)//判斷是否有圖形項 { QPointF pointItem=item->mapFromScene(pointScene);//圖形項的區域性座標 labItemCord->setText(QString::asprintf("Item座標:%。0f,%。0f",pointItem.x(),pointItem.y())); } } /** * @brief MainWindow::on_mouseMovePoint * @param point */ void MainWindow::on_mouseMovePoint(QPoint point) /* * 滑鼠移動事件 * point是GraphicsView的座標 * 是物理座標 */ { labViewCord->setText(QString::asprintf("View座標:%d,%d",point.x(),point.y())); QPointF pointScene=ui->View->mapToScene(point);//轉換到Scene座標 labSceneCord->setText(QString::asprintf("Sence座標:%.0f,%.0f",pointScene.x(),pointScene.y())); } /** * @brief MainWindow::resizeEvent * @param event */ void MainWindow::resizeEvent(QResizeEvent *event)//視窗變化時事件 { Q_UNUSED(event); ui->label->setText(QString::asprintf("Graphics View座標,左上角總是(0,0),寬度=%d,高度=%d",ui->View->width(),ui->View->height())); QRectF rectF=ui->View->sceneRect();// ui->label_2->setText(QString::asprintf("GraphcsView::sceneRect=(Left,Top,Width,Height)=%.0f,%.0f,%.0f,%.0f", rectF.left(),rectF.top(),rectF.width(),rectF.height())); } void MainWindow::iniGraphicsSystem()//構造Graphics View中的各項 { QRectF rect(-200,-100,400,200);//左上角的座標,寬度,高度 scene=new QGraphicsScene(rect);//scene邏輯座標系定義 ui->View->setScene(scene); //畫一個矩形,大小等於scene QGraphicsRectItem *item=new QGraphicsRectItem(rect); item->setFlags(QGraphicsItem::ItemIsSelectable|//設定flags //QGraphicsItem::ItemIsMovable| QGraphicsItem::ItemIsFocusable); QPen pen; pen.setWidth(2); item->setPen(pen); scene->addItem(item); //畫一個位於scene中心的橢圓 QGraphicsEllipseItem *item2=new QGraphicsEllipseItem(-100,-50,200,100); item2->setPos(0,0); item2->setBrush(QBrush(Qt::blue)); item2->setFlags(QGraphicsItem::ItemIsSelectable| QGraphicsItem::ItemIsMovable| QGraphicsItem::ItemIsFocusable); scene->addItem(item2); //畫一個圓,中心位於scene的邊緣 QGraphicsEllipseItem *item3=new QGraphicsEllipseItem(-50,-50,100,100); item3->setPos(rect.right(),rect.bottom()); item3->setBrush(QBrush(Qt::red)); item3->setFlags(QGraphicsItem::ItemIsSelectable| QGraphicsItem::ItemIsMovable| QGraphicsItem::ItemIsFocusable); scene->addItem(item3); scene->clearSelection(); }
qwgraphicsview.cpp
#include "qwgraphicsview.h"
#include<QPoint>
#include<QMouseEvent>
/**
* @brief QWGraphicsView::QWGraphicsView
* @param parent
*/
QWGraphicsView::QWGraphicsView(QWidget *parent):QGraphicsView(parent)
{
}
/**
* @brief QWGraphicsView::mouseMoveEvent
* @param event
*/
void QWGraphicsView::mouseMoveEvent(QMouseEvent *event)//滑鼠移動事件
{
QPoint point=event->pos();//QGraphicsView的座標
emit mouseMovePoint(point);//發射訊號
QGraphicsView::mouseMoveEvent(event);
}
/**
* @brief QWGraphicsView::mousePressEvent
* @param event
*/
void QWGraphicsView::mousePressEvent(QMouseEvent *event)
{
if(event->button()==Qt::LeftButton)//判斷按下滑鼠左鍵
{
QPoint point=event->pos();//獲取座標
emit mouseClicked(point);//發射訊號
}
QGraphicsView::mousePressEvent(event);
}
執行結果