前言:新手写代码,其中许多都是从网上自己搜的,虽然简单,自己也是花了一部分事件才写出来
二、使用步骤
1.重写鼠标事件
代码如下(示例):customscene.h
#ifndef CUSTOMSCENE_H #define CUSTOMSCENE_H #include <QGraphicsScene> #include <QWheelEvent> #include <QResizeEvent> class CustomScene : public QGraphicsScene { public: CustomScene(); protected: //拖入进入事件 void dragEnterEvent(QGraphicsSceneDragDropEvent *event) Q_DECL_OVERRIDE; //离开事件 void dragLeaveEvent(QGraphicsSceneDragDropEvent *) Q_DECL_OVERRIDE; //拖动事件 void dragMoveEvent(QGraphicsSceneDragDropEvent *) Q_DECL_OVERRIDE; //放下事件 void dropEvent(QGraphicsSceneDragDropEvent *event) Q_DECL_OVERRIDE; void mousePressEvent(QGraphicsSceneMouseEvent *event) override; void show(); }; #endif // CUSTOMSCENE_H
customscene.cpp#include "customscene.h" #include <QGraphicsSceneDragDropEvent> #include <QMimeData> #include <QListWidget> #include <QGraphicsPixmapItem> #include <QDebug> #include "imageitem.h" #include <QLabel> #include <QWidget> #include <QDateTime> #include <QMimeData> #include <QPointF> #include <QGraphicsProxyWidget> #include <QGraphicsDropShadowEffect> CustomScene::CustomScene() { } //拖入进入事件 void CustomScene::dragEnterEvent(QGraphicsSceneDragDropEvent *event) { //qDebug() << "dragEnterEvent"; // 获取是否包含接受的 MIME 类型,例如 application/x-qabstractitemmodeldatalist if (event->mimeData()->hasFormat("application/x-qabstractitemmodeldatalist")) { event->acceptProposedAction(); } else { event->ignore(); } } //离开事件 void CustomScene::dragLeaveEvent(QGraphicsSceneDragDropEvent *) { //qDebug() << "dragLeaveEvent"; } //拖动事件 void CustomScene::dragMoveEvent(QGraphicsSceneDragDropEvent *) { //qDebug() << "dragMoveEvent"; } //放下事件 QGraphicsScene要经过 dragMoveEvent 才能触发这个 dropEvent void CustomScene::dropEvent(QGraphicsSceneDragDropEvent *event) { if (!event->mimeData()->hasFormat("application/x-qabstractitemmodeldatalist")) { return ; } QListWidget *pListwidget = qobject_cast<QListWidget *>(event->source()); QString strPixmapPath = ":/images/" + pListwidget->currentItem()->text() + ".png"; if ((pListwidget->currentItem()->text()) == "boy") { //qDebug() << pListwidget->currentItem()->text(); QGraphicsPixmapItem *pPixmapItem = new ImageItem(QPixmap(strPixmapPath)); pPixmapItem->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable); pPixmapItem->setPos(event->scenePos()); addItem(pPixmapItem); } else if ((pListwidget->currentItem()->text()) == "girl") { //qDebug() << pListwidget->currentItem()->text(); QGraphicsPixmapItem *pPixmapItem = new ImageItem(QPixmap(strPixmapPath)); pPixmapItem->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable); pPixmapItem->setPos(event->scenePos()); addItem(pPixmapItem); } else if ((pListwidget->currentItem()->text()) == "men") { //qDebug() << pListwidget->currentItem()->text(); QGraphicsPixmapItem *pPixmapItem = new ImageItem(QPixmap(strPixmapPath)); pPixmapItem->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable); pPixmapItem->setPos(event->scenePos()); addItem(pPixmapItem); } else if ((pListwidget->currentItem()->text()) == "women") { //qDebug() << pListwidget->currentItem()->text(); QGraphicsPixmapItem *pPixmapItem = new ImageItem(QPixmap(strPixmapPath)); pPixmapItem->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable); pPixmapItem->setPos(event->scenePos()); addItem(pPixmapItem); } else { // qDebug() << pListwidget->currentItem()->text(); QGraphicsPixmapItem *pPixmapItem = new ImageItem(QPixmap(strPixmapPath)); // 创建 QDateTime 对象,并根据当前时间设置日期时间值 QDateTime dateTime = QDateTime::currentDateTime(); // 创建 QLabel 对象以显示时间戳 QLabel *pLabel = new QLabel(dateTime.toString(), 0); pLabel->setStyleSheet("background-color: white;"); // 创建 QGraphicsProxyWidget 对象,并将 QLabel 添加到其中 QGraphicsProxyWidget *pProxyWidget = new QGraphicsProxyWidget(pPixmapItem); pProxyWidget->setWidget(pLabel); pProxyWidget->setPos(event->scenePos()); //落在鼠标最后松开的地方 //设置坐标 // 将 QLabel 与 QPixmapItem 相关联以确保它始终处于正确的位置 pPixmapItem->setGraphicsEffect(new QGraphicsDropShadowEffect()); pPixmapItem->setData(Qt::UserRole, QVariant::fromValue(pProxyWidget)); addItem(pPixmapItem); } } void CustomScene::mousePressEvent(QGraphicsSceneMouseEvent *event) { //qDebug()<<"mousePressEvent Button" << event->scenePos().x() << event->scenePos().y();//鼠标的坐标点。注意用scenePos() QGraphicsScene::mousePressEvent(event); } graphsimple.h #ifndef GRAPHSIMPLE_H #define GRAPHSIMPLE_H #include <QWidget> #include "customscene.h" #include <QMouseEvent> #include <QPushButton> namespace Ui { class GraphSimple; } class GraphSimple : public QWidget { Q_OBJECT public: explicit GraphSimple(QWidget *parent = nullptr); ~GraphSimple(); private: Ui::GraphSimple *ui; }; #endif // GRAPHSIMPLE_H graphsimple.cpp #include "graphsimple.h" #include "ui_graphsimple.h" #include "customscene.h" #include <QListWidgetItem> #include <QDebug> #include <QVariant> #include <QDateTime> #include <QSplashScreen> #include <QDateTime> #include <QListWidget> GraphSimple::GraphSimple(QWidget *parent) : QWidget(parent), ui(new Ui::GraphSimple) { ui->setupUi(this); // QLabel显示时间 QTime time = QTime::currentTime(); ui->label->setText(time.toString("hh:mm")); CustomScene *pCustomScene = new CustomScene(); ui->graphicsView->setScene(pCustomScene); ui->graphicsView->setSceneRect(ui->graphicsView->rect()); QListWidgetItem *pItemBoy = new QListWidgetItem(QIcon(":/images/boy.png"), "boy"); QListWidgetItem *pItemGirl = new QListWidgetItem(QIcon(":/images/girl.png"), "girl"); QListWidgetItem *pItemMen = new QListWidgetItem(QIcon(":/images/men.png"), "men"); QListWidgetItem *pItemWomen = new QListWidgetItem(QIcon(":/images/women.png"), "women"); // 创建 QDateTime 对象 QDateTime *pDateTime = new QDateTime(QDateTime::currentDateTime()); QListWidgetItem *pItem = new QListWidgetItem(); //pItem->setText(pDateTime->toString("yyyy-MM-dd HH:mm:ss")); pItem->setText(pDateTime->toString("时间")); QVariant variant; variant.setValue<QDateTime>(*pDateTime); pItem->setData(Qt::UserRole, variant); ui->listWidget->setViewMode(QListView::IconMode); ui->listWidget->setFlow(QListView::TopToBottom); //设置横向展示 ui->listWidget->setIconSize(QSize(300, 150)); ui->listWidget->setMovement(QListWidget::Static); //可移动,Free: QListWidget 内 可移动 ui->listWidget->setWrapping(true); //自动换行 ui->listWidget->setDragEnabled(true); ui->listWidget->addItem(pItemBoy); //将列表QListWidgetItem添加至QListWidget ui->listWidget->addItem(pItemGirl); ui->listWidget->addItem(pItemMen); ui->listWidget->addItem(pItemWomen); ui->listWidget->addItem(pItem); ui->graphicsView->setAcceptDrops(true); } GraphSimple::~GraphSimple() { delete ui; } ImageItem.h #ifndef IMAGEITEM_H #define IMAGEITEM_H #include <QGraphicsPixmapItem> #include <QGraphicsItem> class ImageItem : public QGraphicsPixmapItem { public: ImageItem(const QPixmap& pixmap, QGraphicsItem* parent = nullptr); protected: virtual void mousePressEvent(QGraphicsSceneMouseEvent *) override; virtual void wheelEvent(QGraphicsSceneWheelEvent *event)override; }; #endif // IMAGEITEM_H ImageItem.cpp #include "imageitem.h" #include <QDebug> #include <QGraphicsSceneMouseEvent> #include <QGraphicsSceneWheelEvent> #include <QPoint> #include <QSize> ImageItem::ImageItem(const QPixmap &pixmap, QGraphicsItem *parent):QGraphicsPixmapItem(pixmap, parent) { } void ImageItem::mousePressEvent(QGraphicsSceneMouseEvent *) { } void ImageItem::wheelEvent(QGraphicsSceneWheelEvent *event) { static double scale = 1; int num = event->delta(); if (num > 0) { //qDebug() << "上滚"; scale += 0.1; } else { scale -= 0.1; //qDebug() << "下滚"; } setScale(scale); } main.c #include "graphsimple.h" #include <QApplication> #include <QSplashScreen> #include <QDateTime> int main(int argc, char *argv[]) { QApplication a(argc, argv); QPixmap pixmap(":/images/psplash-poky.png"); //读取图片 QSplashScreen splash(pixmap); // splash.setWindowOpacity(20.8); // 设置窗口透明度 splash.show(); splash.showMessage("程序正在加载......", Qt::AlignCenter, Qt::red); //显示文字 QDateTime time = QDateTime::currentDateTime(); QDateTime currentTime = QDateTime::currentDateTime(); //记录当前时间 while (time.secsTo(currentTime) <= 3) // 5为需要延时的秒数 { currentTime = QDateTime::currentDateTime(); a.processEvents(); }; GraphSimple w; w.show(); splash.finish(&w); //在主体对象初始化完成后结束启动动画 return a.exec(); }
总结
提示:这里对文章进行总结:
例如:以上就是今天要讲的内容,本文仅仅简单介绍了pandas的使用,而pandas提供了大量能使我们快速便捷地处理数据的函数和方法。