void MainWindow::mousePressEvent(QMouseEvent *event) { //1,获取图片 QLabel *child = static_cast<QLabel*>(childAt(event->pos())); if(!child->inherits("QLabel")) return; QPixmap pixmap = *child->pixmap(); //2,自定义MIME类型 QByteArray itemData; QDataStream dataStream(&itemData, QIODevice::WriteOnly); dataStream<<pixmap<<QPoint(event->pos()-child->pos()); //3,将数据放入QMimeData中 QMimeData *mimeData = new QMimeData; mimeData->setData("image/png", itemData); //4,将数据放到QDrag中 QDrag *drag = new QDrag(this); drag->setMimeData(mimeData); drag->setPixmap(pixmap); drag->setHotSpot(event->pos()-child->pos()); //5,原图添加阴影 QPixmap tempPixmap = pixmap; QPainter painter; painter.begin(&tempPixmap); painter.fillRect(pixmap.rect(),QColor(127,127,127,127)); painter.end(); child->setPixmap(tempPixmap); //6,执行拖放操作 if(drag->exec(Qt::CopyAction|Qt::MoveAction, Qt::CopyAction) ==Qt::MoveAction) child->close(); else{ child->show(); child->setPixmap(pixmap); } }
void DragDropArea::mousePressEvent(QMouseEvent *event) { QLabel *child = static_cast<QLabel*>(childAt(event->pos())); if (!child) return; // Only drag children with dynamic property: "drag" if (!child->property("drag").toBool()) return; QPoint hotSpot = event->pos() - child->pos(); QMimeData *mimeData = new QMimeData; mimeData->setText(child->text()); mimeData->setData("application/x-hotspot", QByteArray::number(hotSpot.x()) + " " + QByteArray::number(hotSpot.y())); QPixmap pixmap(child->size()); child->render(&pixmap); QDrag *drag = new QDrag(this); drag->setMimeData(mimeData); drag->setPixmap(pixmap); drag->setHotSpot(hotSpot); drag->exec(); }
/*! \internal */ QRect QAccessibleDisplay::imagePosition() const { QLabel *label = qobject_cast<QLabel *>(widget()); if (!label) return QRect(); const QPixmap *pixmap = label->pixmap(); if (!pixmap) return QRect(); return QRect(label->mapToGlobal(label->pos()), label->size()); }
void MainWindow::mousePressEvent(QMouseEvent *event){ // imprimindo mensagem de debug qDebug()<<"MainWindow::mousePressEvent(QMouseEvent *event)"; QLabel *label = static_cast<QLabel*>(childAt(event->pos())); if (!label || label->objectName()=="centralWidget" || label->objectName()=="" || label->objectName()=="diagrama"){ return; } // imprimindo mensagem para debug qDebug()<<label->objectName(); QPixmap pixmap = *label->pixmap(); QByteArray itemData; QDataStream dataStream(&itemData, QIODevice::WriteOnly); dataStream << pixmap << QPoint(event->pos() - label->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() - label->pos()); QPoint pointAtual = event->pos(); if(pointAtual.x() > ui->diagrama->geometry().x() && pointAtual.y() > ui->diagrama->geometry().y() && pointAtual.x() < ui->diagrama->width()) { label->close(); } if (drag->exec(Qt::CopyAction | Qt::MoveAction, Qt::CopyAction) == Qt::MoveAction) { label->show(); label->setPixmap(pixmap); } }
void TopBar::fadeOutDude( unsigned int i ) { // qDebug() << Q_FUNC_INFO << i; QLabel* dude = m_dudes.at( i ); QPropertyAnimation* ani = new QPropertyAnimation( dude, "pos" ); ani->setDuration( 1000 ); ani->setEasingCurve( QEasingCurve::OutQuad ); ani->setStartValue( dude->pos() ); ani->setEndValue( QPoint( -10, 0 ) ); qDebug() << "Animating from" << ani->startValue() << "to" << ani->endValue(); connect( ani, SIGNAL( finished() ), ani, SLOT( deleteLater() ) ); ani->start(); }
//--------------------------------------------------------------------------- // dropEvent // //! The event handler that receives drop events. // //! @param event the drop event //--------------------------------------------------------------------------- void GraphicalRack::dropEvent (QDropEvent* event) { if (event->mimeData()->hasFormat (mime_type)) { QByteArray itemData = event->mimeData()->data (mime_type); QDataStream dataStream(&itemData, QIODevice::ReadOnly); QPixmap pixmap; QPoint sourcePos; QPoint offset; dataStream >> pixmap >> sourcePos >> offset; QLabel* droppedTile = new QLabel; droppedTile->setPixmap(pixmap); droppedTile->setAttribute (Qt::WA_DeleteOnClose); QPoint dropPos = event->pos() - offset; // Move the tile an extra half tile width in the direction of the // move. This allows the tile to assume a new spot if it is dragged // more than halfway onto the spot. int extraMove = (sourcePos.x() < dropPos.x() ? 50 / 2 : -50 / 2); dropPos.setX(dropPos.x() + extraMove); for(int i = 0; i < m_layout->count(); ++i) { QLabel *label = qobject_cast<QLabel*>(m_layout->itemAt(i)->widget()); if (!label) { // hit the stretcher m_layout->insertWidget(i, droppedTile); break; } if (dropPos.x() > label->pos().x()) { continue; } m_layout->insertWidget(i, droppedTile); break; } if (event->source() == this) { event->setDropAction (Qt::MoveAction); event->accept(); } else { event->acceptProposedAction(); } }
/*! \internal */ QRect QAccessibleDisplay::imagePosition(QAccessible2::CoordinateType coordType) { QLabel *label = qobject_cast<QLabel *>(widget()); if (!label) return QRect(); const QPixmap *pixmap = label->pixmap(); if (!pixmap) return QRect(); switch (coordType) { case QAccessible2::RelativeToScreen: return QRect(label->mapToGlobal(label->pos()), label->size()); case QAccessible2::RelativeToParent: return label->geometry(); } return QRect(); }