void Widget::mousePressEvent(QMouseEvent *event){ QLabel *child = static_cast<QLabel*>(childAt(event->pos())); if (!child) return; QPixmap pixmap = *child->pixmap(); QByteArray itemData; QDataStream dataStream(&itemData, QIODevice::WriteOnly); dataStream << pixmap << QPoint(event->pos() - child->pos()); QMimeData *mimeData = new QMimeData; mimeData->setData("application/x-dnditemdata", itemData); QDrag *drag = new QDrag(this); drag->setMimeData(mimeData); drag->setPixmap(pixmap); drag->setHotSpot(event->pos() - child->pos()); if (drag->exec(Qt::CopyAction | Qt::MoveAction, Qt::CopyAction) == Qt::MoveAction) child->close(); else { child->show(); child->setPixmap(pixmap); }} void Widget::dropEvent(QDropEvent *event) { if (event->mimeData()->hasFormat("application/x-dnditemdata")) { QByteArray itemData = event->mimeData()->data("application/x-dnditemdata"); QDataStream dataStream(&itemData, QIODevice::ReadOnly); QPixmap pixmap; QPoint offset; dataStream >> pixmap >> offset; QLabel *newIcon = new QLabel(this); newIcon->setPixmap(pixmap); newIcon->move(event->pos() - offset); newIcon->show(); newIcon->setAttribute(Qt::WA_DeleteOnClose); if (event->source() == this) { event->setDropAction(Qt::MoveAction); event->accept(); } else { event->acceptProposedAction(); } } else { event->ignore(); } } void Widget::dragEnterEvent(QDragEnterEvent *event) { if (event->mimeData()->hasFormat("application/x-dnditemdata")) { if (event->source() == this) { event->setDropAction(Qt::MoveAction); event->accept(); } else { event->acceptProposedAction(); } } else { event->ignore(); } }
QLabel *Sun = new QLabel(this); Sun->move(10,10); Sun->setMinimumSize(50,50); Sun->setPixmap(QPixmap("1.bmp")); Sun->setToolTip("Солнце"); Sun->show(); Sun->setAttribute(Qt::WA_DeleteOnClose);
void Widget::dropEvent(QDropEvent *event) { if (event->mimeData()->hasFormat("application/x-dnditemdata")) { QByteArray itemData = event->mimeData()->data("application/x-dnditemdata"); QDataStream dataStream(&itemData, QIODevice::ReadOnly); QPixmap pixmap; QPoint offset; dataStream >> pixmap >> offset; QLabel *newIcon = new QLabel(this); newIcon->setPixmap(pixmap); newIcon->move(event->pos() - offset); newIcon->show(); newIcon->setAttribute(Qt::WA_DeleteOnClose); if (event->source() == this) { event->setDropAction(Qt::MoveAction); event->accept(); } else { event->acceptProposedAction(); } } else { event->ignore(); } }
Widget::Widget(QWidget *parent) : QWidget(parent = 0){ QPoint *rightSunPos = new QPoint; QPalette palette; palette.setBrush(this->backgroundRole(), QBrush(QImage("planet.jpg"))); this->setPalette(palette); setAcceptDrops(true); //-----------------------------------------------------------------------------------/ QLabel *Sun = new QLabel(this); Sun->move(10,10); Sun->setMinimumSize(50,50); Sun->setPixmap(QPixmap("1.bmp")); Sun->setToolTip("Солнце"); Sun->show(); Sun->setAttribute(Qt::WA_DeleteOnClose); //-----------------------------------------------------------------------------------/ QLabel *Mercury = new QLabel(this); Mercury->move(10,60); Mercury->setMinimumSize(50,50); Mercury->setPixmap(QPixmap("2.bmp")); Mercury->setToolTip("Меркурий"); Mercury->show(); Mercury->setAttribute(Qt::WA_DeleteOnClose); //-----------------------------------------------------------------------------------/ QLabel *Venus = new QLabel(this); Venus->move(10,120); Venus->setMinimumSize(50,50); Venus->setPixmap(QPixmap("3.bmp")); Venus->setToolTip("Венера"); Venus->show(); Venus->setAttribute(Qt::WA_DeleteOnClose); //-----------------------------------------------------------------------------------/ QLabel *Earth = new QLabel(this); Earth->move(10,180); Earth->setMinimumSize(50,50); Earth->setPixmap(QPixmap("4.bmp")); Earth->setToolTip("Земля"); Earth->show(); Earth->setAttribute(Qt::WA_DeleteOnClose); //----------------------------------------------------------------------------------/ QLabel *Mars = new QLabel(this); Mars->move(10,240); Mars->setMinimumSize(50,50); Mars->setPixmap(QPixmap("5.bmp")); Mars->setToolTip("Марс"); Mars->show(); Mars->setAttribute(Qt::WA_DeleteOnClose); //----------------------------------------------------------------------------------/ QLabel *Jupiter = new QLabel(this); Jupiter->move(10,300); Jupiter->setMinimumSize(50,50); Jupiter->setPixmap(QPixmap("6.bmp")); Jupiter->setToolTip("Юпитер"); Jupiter->show(); Jupiter->setAttribute(Qt::WA_DeleteOnClose); //----------------------------------------------------------------------------------/ QLabel *Saturn = new QLabel(this); Saturn->move(10,360); Saturn->setMinimumSize(50,50); Saturn->setPixmap(QPixmap("7.bmp")); Saturn->setToolTip("Сатурн"); Saturn->show(); Saturn->setAttribute(Qt::WA_DeleteOnClose); //----------------------------------------------------------------------------------/ QLabel *Uran = new QLabel(this); Uran->move(10,420); Uran->setMinimumSize(50,50); Uran->setPixmap(QPixmap("8.bmp")); Uran->setToolTip("Уран"); Uran->show(); Uran->setAttribute(Qt::WA_DeleteOnClose); Uran->setToolTip("Уран"); //----------------------------------------------------------------------------------/ QLabel *Neptun = new QLabel(this); Neptun->move(10,480); Neptun->setMinimumSize(50,50); Neptun->setPixmap(QPixmap("9.bmp")); Neptun->setToolTip("Нептун"); Neptun->show(); Neptun->setAttribute(Qt::WA_DeleteOnClose); //----------------------------------------------------------------------------------/ QLabel *Pluton = new QLabel(this); Pluton->move(10,540); Pluton->setMinimumSize(50,50); Pluton->setPixmap(QPixmap("10.bmp")); Pluton->setToolTip("Плутон"); Pluton->show(); Pluton->setAttribute(Qt::WA_DeleteOnClose); //----------------------------------------------------------------------------------/ QPushButton *quit = new QPushButton("Выход",this); quit->setFont(QFont("Times", 10, QFont::Bold)); quit->move(775,5); quit->show(); QObject::connect(quit, SIGNAL(clicked()), this, SLOT(close())); //----------------------------------------------------------------------------------/ //----------------------------------------------------------------------------------/}