C++ (Qt)void MainWindow::on_actionName1_triggered(){Pixmap = scene ->addPixmap(QPixmap("1.png"));Pixmap -> setFlags(QGraphicsItem::ItemIsSelectable|QGraphicsItem::ItemIsMovable);Pixmap -> setPos(ui->XspinBox->value(),ui->YspinBox->value());Pixmap -> setScale(0.4);ui->label->setText(QString::number(Pixmap->pos().x())+" "+QString::number(Pixmap->pos().y()));}
C++ (Qt)ellipse = scene->addEllipse(x,y,d,d,QPen(Qt::black),QBrush(Qt::white));
C++ (Qt)#include <QWidget>#include <QPainter>#include <QRectF>#include <QMouseEvent>#include <QDebug> class PainterWindow : public QWidget{ Q_OBJECTpublic: explicit PainterWindow(QWidget *parent = 0) : QWidget(parent) { rect = QRectF(30, 30, 60, 60); press = false; paintNewRect = false; } signals: public slots: public: QRectF rect; QPointF lastPos; bool press; bool paintNewRect; QPointF startPos; protected: void paintEvent(QPaintEvent *) { QPainter p(this); p.setBrush(Qt::red); p.drawRect(rect); } void mouseMoveEvent(QMouseEvent * e) { if (press) { QPointF delta = e->posF() - lastPos; // Меняем размеры фигуры мышкой if (paintNewRect) { rect.setTopLeft(startPos); rect.setBottomRight(e->posF()); } else { // Передвигаем фигуру rect.setX(rect.x() + delta.x()); rect.setY(rect.y() + delta.y()); rect.setWidth(rect.width() + delta.x()); rect.setHeight(rect.height() + delta.y()); } update(); } lastPos = e->posF(); } void mousePressEvent(QMouseEvent * e) { press = true; lastPos = e->posF(); // Если мимо фигуры попали, включаем режим изменения // размера фигуры мышкой, иначе передвигаем фигуру paintNewRect = !rect.contains(lastPos); startPos = lastPos; } void mouseReleaseEvent(QMouseEvent *) { press = false; paintNewRect = false; }};
C++ (Qt) //mainwindow.h void paintEvent(QPaintEvent *){ QPainter p(this); p.setPen(Qt::red); QPoint point(200,200); p.drawEllipse(point,70,70); p.setPen(Qt::black); p.drawLine(QPointF(200,130),QPointF(200,270)); p.drawLine(QPointF(130,200),QPointF(270,200)); }
C++ (Qt) connect(ui->button1,SIGNAL(clicked(bool)),this,SLOT(update(paintEvent(QPaintEvent *))));