日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Qt模仿游戏拖装备(换装备)

發布時間:2025/3/15 编程问答 16 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Qt模仿游戏拖装备(换装备) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

程序運行截圖如下:

代碼如下:
mainwindow.h

#ifndef MAINWINDOW_H #define MAINWINDOW_H#include <QMainWindow>class MyGraphicsScene;namespace Ui { class MainWindow; }class MainWindow : public QMainWindow {Q_OBJECTpublic:enum type{HEAD,CHEST,LEFTHAND,RIGHTHAND,FOOT};explicit MainWindow(QWidget *parent = 0);~MainWindow();void timerEvent(QTimerEvent *event);protected:void initGraphicsItem();private:Ui::MainWindow *ui;MyGraphicsScene *m_scene;int m_timer_z; };#endif // MAINWINDOW_H

mygraphicsitem.h

#ifndef MYGRAPHICSITEM_H #define MYGRAPHICSITEM_H#include <QGraphicsItem> #include <QString> #include <QObject>class MyGraphicsItem;class MyGraphicsItem : public QObject,public QGraphicsItem {Q_OBJECT public:MyGraphicsItem();~MyGraphicsItem();QRectF boundingRect()const;void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);bool startDetection(int &gracType);void timerEvent(QTimerEvent *event);QString remindText;QString imageStr;int graphicsType; };#endif // MYGRAPHICSITEM_H

mygraphicsview.h

#ifndef MYGRAPHICSVIEW_H #define MYGRAPHICSVIEW_H#include <QObject> #include <QGraphicsView>#define HEADTYPE 0 #define CHESTTYPE 1 #define LEFTHANDTYPE 2 #define RIGHTHANDTYPE 3 #define FOOTTYPE 4class MyGraphicsItem;class MyGraphicsView : public QGraphicsView {Q_OBJECT public:MyGraphicsView(QWidget *parent=0);~MyGraphicsView();bool isInEquipment(QDropEvent *event, int &graType);protected:void dragEnterEvent(QDragEnterEvent *event);void dropEvent(QDropEvent *event);void estimateHeadText(MyGraphicsItem *item,const QString &text);void estimateChestText(MyGraphicsItem *item,const QString &text);void estimateLeftHandText(MyGraphicsItem *item,const QString &text);void estimateRightHandText(MyGraphicsItem *item,const QString &text);void estimateFootText(MyGraphicsItem *item, const QString &text);QGraphicsItem *findItem(const int type);void contextMenuEvent(QContextMenuEvent *event);protected slots:void onDischargeAction();private:MyGraphicsItem *m_contextMenuItem;};#endif // MYGRAPHICSVIEW_H

mygraphicsscene.h

#ifndef MYGRAPHICSSCENC_H #define MYGRAPHICSSCENC_H#include <QObject> #include <QGraphicsScene>class MyGraphicsScene : public QGraphicsScene {Q_OBJECT public:MyGraphicsScene(QObject *parent=0);~MyGraphicsScene();protected:void dragEnterEvent(QGraphicsSceneDragDropEvent *event);void dropEvent(QGraphicsSceneDragDropEvent* event);void dragMoveEvent(QGraphicsSceneDragDropEvent *event);};#endif // MYGRAPHICSSCENC_H

mylistwidget.h

#ifndef MYLISTWIDGET_H #define MYLISTWIDGET_H#include <QObject> #include <QListWidget>class MyListWidget : public QListWidget {Q_OBJECT public:MyListWidget(QWidget *parent=0);~MyListWidget();protected:void mouseMoveEvent(QMouseEvent *event);void mousePressEvent(QMouseEvent *event);private:QPoint m_dragPosition; // 拖放起點QListWidgetItem *m_dragItem; // 被拖放的item};#endif // MYLISTWIDGET_H

main.cpp

#include "mainwindow.h" #include <QApplication>int main(int argc, char *argv[]) {QApplication a(argc, argv);MainWindow w;w.show();return a.exec(); }

mainwindow.cpp

#include "mainwindow.h" #include "ui_mainwindow.h" #include <QGraphicsScene> #include "mygraphicsscene.h" #include <QGraphicsLineItem> #include <QTimerEvent> #include "mygraphicsitem.h" #include <QDebug> #include <QGraphicsDropShadowEffect> #include <QRadialGradient> #include <QRect> #include <QPixmap> #include <QDesktopWidget>MainWindow::MainWindow(QWidget *parent) :QMainWindow(parent),ui(new Ui::MainWindow) {ui->setupUi(this);QStringList strList;strList.append("裁決");strList.append("游龍劍");strList.append("祈禱之刃");ui->mylistWidget->insertItems(0,strList);m_scene=new MyGraphicsScene;QRect sceenRect=QApplication::desktop()->screenGeometry();setFixedSize(sceenRect.width()/1.2,sceenRect.height()/1.2);initGraphicsItem();m_scene->setSceneRect(-200.5,-100.5,501,501);ui->graphicsView->setScene(m_scene);m_timer_z=0;startTimer(10);connect(ui->pushButton,SIGNAL(clicked(bool)),this,SLOT(close()));}MainWindow::~MainWindow() {delete ui; }void MainWindow::timerEvent(QTimerEvent *event) {if(m_timer_z==180)m_timer_z=0;QRadialGradient gradient(50,150,m_timer_z++);gradient.setSpread(QGradient::RepeatSpread);m_scene->setBackgroundBrush(gradient); }void MainWindow::initGraphicsItem() {QGraphicsDropShadowEffect *headShadowE=new QGraphicsDropShadowEffect;headShadowE->setOffset(10,10);QGraphicsDropShadowEffect *bodyShadowE=new QGraphicsDropShadowEffect;headShadowE->setOffset(10,10);QGraphicsDropShadowEffect *footShadowE=new QGraphicsDropShadowEffect;headShadowE->setOffset(10,10);QGraphicsDropShadowEffect *leftHandShadowE=new QGraphicsDropShadowEffect;headShadowE->setOffset(10,10);QGraphicsDropShadowEffect *rightHandShadowE=new QGraphicsDropShadowEffect;headShadowE->setOffset(10,10);MyGraphicsItem *head=new MyGraphicsItem;head->remindText="頭盔";head->setPos(0,-100);head->graphicsType=HEAD;head->setGraphicsEffect(headShadowE);m_scene->addItem(head);MyGraphicsItem *body=new MyGraphicsItem;body->remindText="護甲";body->setPos(0,100);body->graphicsType=CHEST;body->setGraphicsEffect(bodyShadowE);m_scene->addItem(body);MyGraphicsItem *foot=new MyGraphicsItem;foot->remindText="護腳";foot->setPos(0,300);foot->graphicsType=FOOT;foot->setGraphicsEffect(footShadowE);m_scene->addItem(foot);MyGraphicsItem *leftHand=new MyGraphicsItem;leftHand->remindText="武器";leftHand->setPos(-200,100);leftHand->graphicsType=LEFTHAND;leftHand->setGraphicsEffect(leftHandShadowE);m_scene->addItem(leftHand);MyGraphicsItem *rightHand=new MyGraphicsItem;rightHand->remindText="武器";rightHand->setPos(200,100);rightHand->graphicsType=RIGHTHAND;rightHand->setGraphicsEffect(rightHandShadowE);m_scene->addItem(rightHand); }

mygraphicsitem.cpp

#include "mygraphicsitem.h" #include <QDebug> #include <QRectF> #include <QPen> #include <QImage> #include <QList> #include <QBrush> #include <QPainter>MyGraphicsItem::MyGraphicsItem() {remindText="";imageStr="";graphicsType=-1; }MyGraphicsItem::~MyGraphicsItem() {}QRectF MyGraphicsItem::boundingRect() const {qreal penWidth=1;return QRectF(0-penWidth/2,0-penWidth/2,100+penWidth,100+penWidth); }void MyGraphicsItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) {Q_UNUSED(option)Q_UNUSED(widget)painter->setBrush(Qt::white);painter->drawRect(0,0,100,100);if(!imageStr.isEmpty()){painter->drawImage(0,0,QImage(imageStr).scaled(100,100));}else{painter->drawText(40,50,remindText);} }bool MyGraphicsItem::startDetection(int &gracType) {QList<QGraphicsItem*> list=collidingItems();if(!list.isEmpty()){gracType=((MyGraphicsItem*)list[0])->graphicsType;return true;}return false; }void MyGraphicsItem::timerEvent(QTimerEvent *event) {Q_UNUSED(event) }

mygraphicsscene.cpp

#include "mygraphicsscene.h" #include <QGraphicsSceneDragDropEvent> #include <QMimeData>MyGraphicsScene::MyGraphicsScene(QObject *parent):QGraphicsScene(parent) {}MyGraphicsScene::~MyGraphicsScene() {}void MyGraphicsScene::dragEnterEvent(QGraphicsSceneDragDropEvent *event) {if(event->mimeData()->hasFormat("text/plain")){event->acceptProposedAction();} }void MyGraphicsScene::dropEvent(QGraphicsSceneDragDropEvent *event) {QGraphicsScene::dropEvent(event); }void MyGraphicsScene::dragMoveEvent(QGraphicsSceneDragDropEvent *event) {event->accept(); }

mygraphicsview.cpp

#include "mygraphicsview.h" #include <QMouseEvent> #include <QDragEnterEvent> #include <QMimeData> #include <QPolygonF> #include <QPointF> #include <QGraphicsItem> #include <QTransform> #include <QDebug> #include <QMenu> #include "mygraphicsitem.h"MyGraphicsView::MyGraphicsView(QWidget *parent):QGraphicsView(parent) {}MyGraphicsView::~MyGraphicsView() {}bool MyGraphicsView::isInEquipment(QDropEvent *event,int &graType) {MyGraphicsItem *tempItem=new MyGraphicsItem;tempItem->setPos(mapToScene(event->pos()));scene()->addItem(tempItem);if(tempItem->startDetection(graType)){delete tempItem;return true;}delete tempItem;return false; }void MyGraphicsView::dragEnterEvent(QDragEnterEvent *event) {if(event->mimeData()->hasFormat("text/plain")){event->acceptProposedAction();}QGraphicsView::dragEnterEvent(event); }void MyGraphicsView::dropEvent(QDropEvent *event) {if(event->mimeData()->hasFormat("text/plain")){int graphicType;if(isInEquipment(event,graphicType)){QString text=event->mimeData()->text();MyGraphicsItem *item=(MyGraphicsItem*)findItem(graphicType);switch(graphicType){case HEADTYPE:break;case CHESTTYPE:break;case LEFTHANDTYPE:estimateLeftHandText(item,text);break;case RIGHTHANDTYPE:estimateRightHandText(item,text);break;case FOOTTYPE:break;default:break;}}QGraphicsView::dropEvent(event);} }void estimateHeadText(MyGraphicsItem *item,const QString &text) {qDebug()<<text; }void MyGraphicsView::estimateChestText(MyGraphicsItem *item,const QString &text) {qDebug()<<text; }void MyGraphicsView::estimateLeftHandText(MyGraphicsItem *item,const QString &text) {if(text=="裁決"){item->imageStr=":/img/sword_1.png";item->update();}else if(text=="游龍劍"){item->imageStr=":/img/sword_2.png";item->update();}else if(text=="祈禱之刃"){item->imageStr=":/img/sword_3.png";item->update();}return; }void MyGraphicsView::estimateRightHandText(MyGraphicsItem *item,const QString &text) {if(text=="裁決"){item->imageStr=":/img/sword_1.png";item->update();}else if(text=="游龍劍"){item->imageStr=":/img/sword_2.png";item->update();}else if(text=="祈禱之刃"){item->imageStr=":/img/sword_3.png";item->update();}return; }void MyGraphicsView::estimateFootText(MyGraphicsItem *item,const QString &text) {qDebug()<<text; }QGraphicsItem *MyGraphicsView::findItem(const int type) {QList<QGraphicsItem*> list=scene()->items();for(int i=0;i<list.size();i++){if(((MyGraphicsItem*)list[i])->graphicsType==type){return list[i];}} }void MyGraphicsView::contextMenuEvent(QContextMenuEvent *event) {QGraphicsScene *scenpt=scene();QTransform transtrom;m_contextMenuItem=(MyGraphicsItem*)(scenpt->itemAt(mapToScene(event->pos()),transtrom));if(m_contextMenuItem==NULL||m_contextMenuItem->imageStr.isEmpty())return;QMenu menu;menu.addAction("卸下",this,&MyGraphicsView::onDischargeAction);menu.exec(event->globalPos());}void MyGraphicsView::onDischargeAction() {m_contextMenuItem->imageStr="";m_contextMenuItem->update(); }

mylistwidget.cpp

#include "mylistwidget.h" #include <QDebug> #include <QDrag> #include <QMouseEvent> #include <QMimeData> #include <QPixmap> #include <QPainter> #include <QApplication>MyListWidget::MyListWidget(QWidget *parent):QListWidget(parent) {}MyListWidget::~MyListWidget() {}void MyListWidget::mouseMoveEvent(QMouseEvent *event) {if(!(event->buttons()&Qt::LeftButton))return;if((event->pos()-m_dragPosition).manhattanLength()<QApplication::startDragDistance())return;if(!m_dragItem)return;QDrag *drag=new QDrag(this);QMimeData *mimeData=new QMimeData;mimeData->setText(currentItem()->text());drag->setMimeData(mimeData);if(mimeData->text()=="裁決"){QPixmap dragImg(":/img/sword_1.png");drag->setPixmap(dragImg.scaled(100,100));}else if(mimeData->text()=="游龍劍"){QPixmap dragImg(":/img/sword_2.png");drag->setPixmap(dragImg.scaled(100,100));}else if(mimeData->text()=="祈禱之刃"){QPixmap dragImg(":/img/sword_3.png");drag->setPixmap(dragImg.scaled(100,100));}else{}drag->exec(Qt::CopyAction|Qt::MoveAction);}void MyListWidget::mousePressEvent(QMouseEvent *event) {m_dragPosition=event->pos();m_dragItem=this->itemAt(event->pos());QListWidget::mousePressEvent(event); }

總結

以上是生活随笔為你收集整理的Qt模仿游戏拖装备(换装备)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。