qtgraph
#include "mainwindow.h" #include "ui_mainwindow.h" #include <QGraphicsRectItem> #include <QGraphicsSceneWheelEvent> #include <QGraphicsSceneDragDropEvent> #include <QDrag> #include <QMimeData> MainWindow * win = 0; class MyItem : public QGraphicsRectItem { private: QColor clr; int cnt; public: void setClr () { clr = QColor::fromHsl(abs (cnt) % 360, 250, 150); setBrush (clr); setToolTip (QString::number(cnt)); // setRotation(abs (cnt) % 360); } MyItem (int p = 0) : cnt (p) { setClr (); setPen (QColor ("red")); setOpacity (0.5); setFlag (QGraphicsItem::ItemIsMovable); setAcceptDrops (true); } protected: virtual void wheelEvent ( QGraphicsSceneWheelEvent * event ) { cnt += event->delta() / 8; setClr (); // setBrush (Qt::green); } virtual void mousePressEvent ( QGraphicsSceneMouseEvent * event ) { // #include <QDrag> // #include <QMimeData> QDrag * d = new QDrag (win); QMimeData * m = new QMimeData; m->setText ("abc"); m->setColorData (clr); d->setMimeData (m); d->exec (); } virtual void dropEvent ( QGraphicsSceneDragDropEvent * event ) { setBrush (Qt::green); const QMimeData * m = event->mimeData(); if (m->hasText()) setToolTip(m->text()); if (m->hasColor()) setBrush (qvariant_cast<QColor>(m->colorData())); } }; MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); win = this; QGraphicsScene * scene = new QGraphicsScene; scene->setSceneRect (0, 0, 300, 200); ui->graphicsView->setScene (scene); for (int i = 1; i <= 5; i++) { MyItem * item = new MyItem (45*i); item->setRect (80*i, 40*i, 100, 200); scene->addItem (item); } /* scene->addText ("Hello"); scene->addLine (0, 0, 100, 100, QColor ("red")); scene->addRect (100, 10, 40, 40, QColor ("blue"), QColor ("yellow")); QGraphicsRectItem * item = new QGraphicsRectItem; item->setPen (QPen (QColor ("blue"), 3)); item->setBrush (QBrush(QColor ("cornflowerblue"), Qt::FDiagPattern)); item->setRect (40, 40, 100, 200); item->setToolTip ("abc"); item->setFlags(QGraphicsItem::ItemIsMovable); // item->setFlags(QGraphicsItem::ItemIsSelectable); scene->addItem (item); */ } MainWindow::~MainWindow() { delete ui; }
qtgraph.txt · Last modified: 2014/03/18 10:13 by 127.0.0.1