jijiz | Bonjour,
Je suis en train de reprendre un exemple fournis avec QT4 pour déssiner des objets que l'on peut saisir et déplacer (drag and drop).
Je galère à passer du code c++ vers Python, Pourriez vous m'aider petit à petit... Merci
Je pense qu'il serait interessant de mettre à disposition des internautes des exemples python/Qt4 un peu évolués (sions graphique), car c'est vraiment la dèche sur internet.
Code C++ :
Code :
- 
-
- Home · All Classes · Main Classes · Grouped Classes · Modules · Functions
- 
- dragwidget.cpp Example File
- draganddrop/draggableicons/dragwidget.cpp
- /****************************************************************************
- **
- ** Copyright (C) 2005-2006 Trolltech ASA. All rights reserved.
- **
- ** This file is part of the documentation of the Qt Toolkit.
- **
- ** This file may be used under the terms of the GNU General Public
- ** License version 2.0 as published by the Free Software Foundation
- ** and appearing in the file LICENSE.GPL included in the packaging of
- ** this file. Please review the following information to ensure GNU
- ** General Public Licensing requirements will be met:
- ** http://www.trolltech.com/products/qt/opensource.html
- **
- ** If you are unsure which license is appropriate for your use, please
- ** review the following information:
- ** http://www.trolltech.com/products/qt/licensing.html or contact the
- ** sales department at sales@trolltech.com.
- **
- ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
- ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
- **
- ****************************************************************************/
- #include <QtGui>
- #include "dragwidget.h"
- DragWidget::DragWidget(QWidget *parent)
- : QFrame(parent)
- {
- setMinimumSize(200, 200);
- setFrameStyle(QFrame::Sunken | QFrame::StyledPanel);
- setAcceptDrops(true);
- QLabel *boatIcon = new QLabel(this);
- boatIcon->setPixmap(QPixmap(":/images/boat.png" ));
- boatIcon->move(20, 20);
- boatIcon->show();
- boatIcon->setAttribute(Qt::WA_DeleteOnClose);
- QLabel *carIcon = new QLabel(this);
- carIcon->setPixmap(QPixmap(":/images/car.png" ));
- carIcon->move(120, 20);
- carIcon->show();
- carIcon->setAttribute(Qt::WA_DeleteOnClose);
- QLabel *houseIcon = new QLabel(this);
- houseIcon->setPixmap(QPixmap(":/images/house.png" ));
- houseIcon->move(20, 120);
- houseIcon->show();
- houseIcon->setAttribute(Qt::WA_DeleteOnClose);
- }
- void DragWidget::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();
- }
- }
- void DragWidget::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 DragWidget::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());
- QPixmap tempPixmap = pixmap;
- QPainter painter;
- painter.begin(&tempPixmap);
- painter.fillRect(pixmap.rect(), QColor(127, 127, 127, 127));
- painter.end();
- child->setPixmap(tempPixmap);
- if (drag->start(Qt::CopyAction | Qt::MoveAction) == Qt::MoveAction)
- child->close();
- else {
- child->show();
- child->setPixmap(pixmap);
- }
- }
- Copyright © 2006 Trolltech
- Trademarks
- Qt 4.1.4
|
Code Python :
Code :
- from PyQt4 import QtGui, QtCore
- # Questions :
- # A t on besoin de faire QtCore.QPoint() pour creer un QPoint, ou peut on directement appeler QPoint?
- def DragWidget(self, QFrame):
- def __init__(self, parent):
- self.setMinimumSize(200, 200)
- self.setAcceptDrops(True)
-
-
- boatIcon = QtGui.QLabel(self)
- boatIcon.setPixMap(QtGui.QPixmap("./image.png" ))
- boatIcon.move(20,20)
- boatIcon.show()
- # boatIcon.setAttribute(QtGui.WA_DeleteOnClose)
-
- def dragEnterEvent(self, event):
- if (event.mimeData().hasFormat("application/x-dnditemdata" )):
- if (event.source()==self):
- event.setDropAction(QtCore.Qt.MoveAction)
- event.accept()
- else:
- event.acceptProposedAction()
- else:
- event.ignore()
- def dropEvent(event):
- if (event.mimeData().hasFormat("application/x-dnditemdata" )):
- itemData = event.mimeData().data("application/x-dnditemdata" )
- dataStream = QFrame.QDataStream(itemData, self.QIODevice.ReadOnly)
- # pixmap = QtGui.QPixmap(item.data(QtCore.Qt.DecorationRole))
- pixmap = QtGui.QPixmap
- offset = QtCore.QPoint()
-
-
- dataStream >> pixmap >> offset
-
- newIcon = QtGui.QLabel(self)
- newIcon.setPixmap(self.pixmap)
- newIcon.move(event.pos() - self.offset)
- newIcon.show()
- newIcon.setAttribute(self.WA_deleteOnClose)
-
- if (event.source() == self):
- event.setDropAction(QtCore.Qt.MoveAction)
- event.accept()
- else:
- event.acceptProposedAction()
- else:
- event.ignore()
-
- def mousePressEvent(self,event):
- child = static_cast<QtGui.QLabel>(QFrame.childAt(event.pos()))
- if (child == False):
- return
-
- pixmap = child.pixmap()
- itemData = QtCore.QByteArray()
- dataStream = QtCore.QDataStream(itemData, QtCore.QIODevice.writeOnly)
- dataStream << pixmap << QtCore.QPoint(event.pos() - child.pos())
-
- mimeData = QtCore.QMimeData()
- mimeData.setData("application/x-dnditemdata", itemData)
-
- drag = QtGui.QDrag(self)
- drag.setMimeData(mimeData)
- drag.setPixMap(pixmap)
- drag.setHotSpot(event.pos() - child.pos())
-
- tempPixmap = QtGui.QPixmap
- painter = QtGui.QPainter
- painter.begin(tempPixmap)
-
- painter.fillRect(pixmap.rect(), QtGui.QColor(127, 127, 127, 127))
- painter.end()
-
- child.setPixMap(tempPixmap)
-
- if (drag.start(QtCore.Qt.CopyAction | QtCore.Qt.MoveAction) == QtCore.Qt.MoveAction):
- child.close()
- else:
- child.show()
- child.setPixMap(pixmap)
|
MAJ Code Message édité par jijiz le 17-04-2007 à 16:05:53
|