IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Discussion :

Drag and drop d'un label dans un rectangle

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre confirmé
    Femme Profil pro
    Apprentie Développeuse
    Inscrit en
    Février 2013
    Messages
    106
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Localisation : France

    Informations professionnelles :
    Activité : Apprentie Développeuse
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Février 2013
    Messages : 106
    Par défaut Drag and drop d'un label dans un rectangle
    Bonjour ,

    j'ai une fenêtre avec un rectangle puis avec un label à l'intérieur.
    mon objectif est d'insérer ou je veut mon label dans le rectangle afin d'en récupérer la positions par la suite.
    Or je suis bloquée je n'arrive pas à déplacer mon label .. Quelqu'un pourrais me débloquer svp merci . Je sais qu'il faut utiliser un drag'n'drop mais je ne n'arrive pas à le mettre en place.


    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        string chaine = "coco";
        string test = "test";
        chaine = argv[0];
        //chaine = argv[1];
        if(argv[0]==0){
                    //MessageBox(NULL,"message","ok",MB_OK);
                   //QMessageBox::warning("",L"Message", L"Information",MB_OK);
                }
        QWidget fenetre;
        QPixmap pixels(1024,768);
        pixels.fill() ;
        QPainter crayon(&pixels) ;
        crayon.drawRect(0,0,1000,400) ;
        //crayon.drawRect(120,150,200,100) ;
        crayon.end() ;
        QLabel *label2 = new QLabel(&fenetre);
        label2->setPixmap(pixels);
        label2->show();
     
     
        fenetre.show();
            QLabel *label = new QLabel(&fenetre);
            label->setMinimumSize(100,100);
            label ->setText(QString::fromStdString(chaine));
            label->show();
     
            label->setAcceptDrops(true);
     
     
     
     
     
         return a.exec();
    }

  2. #2
    Membre confirmé
    Femme Profil pro
    Apprentie Développeuse
    Inscrit en
    Février 2013
    Messages
    106
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Localisation : France

    Informations professionnelles :
    Activité : Apprentie Développeuse
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Février 2013
    Messages : 106
    Par défaut
    j'ai crée 2 classes supplémentaires : DragLabel et DragWidget

    DragLabel :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
     DragLabel::DragLabel(const QString &text, QWidget *parent)
         : QLabel(text, parent)
     {
         setAutoFillBackground(true);
         setFrameShape(QFrame::Panel);
         setFrameShadow(QFrame::Raised);
     }
    DRAGWIDGET :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    DragWidget::DragWidget(QWidget *parent)
        : QWidget(parent)
    {
     
        QPalette newPalette = palette();
        newPalette.setColor(QPalette::Window, Qt::white);
        setPalette(newPalette);
     
        setAcceptDrops(true);
        setMinimumSize(400, qMax(200, y));
        //setWindowTitle(tr("Draggable Text"));
    }
     
    //évenement lors du déplacement du label
    void DragWidget::dragEnterEvent(QDragEnterEvent *event)
    {
        if (event->mimeData()->hasText()) {
            if (event->source() == this) {
                event->setDropAction(Qt::MoveAction);
                event->accept();
            } else {
                event->acceptProposedAction();
            }
        } else {
            event->ignore();
        }
    }
     
    void DragWidget::dropEvent(QDropEvent *event)
    {
        if (event->mimeData()->hasText()) {
            const QMimeData *mime = event->mimeData();
            QStringList pieces = mime->text().split(QRegExp("\\s+"),
                                 QString::SkipEmptyParts);
            QPoint position = event->pos();
            QPoint hotSpot;
     
            QList<QByteArray> hotSpotPos = mime->data("application/x-hotspot").split(' ');
            if (hotSpotPos.size() == 2) {
                hotSpot.setX(hotSpotPos.first().toInt());
                hotSpot.setY(hotSpotPos.last().toInt());
            }
     
            foreach (QString piece, pieces) {
                DragLabel *newLabel = new DragLabel(piece, this);
                newLabel->move(position - hotSpot);
                newLabel->show();
                newLabel->setAttribute(Qt::WA_DeleteOnClose);
     
                position += QPoint(newLabel->width(), 0);
            }
     
            if (event->source() == this) {
                event->setDropAction(Qt::MoveAction);
                event->accept();
            } else {
                event->acceptProposedAction();
            }
        } else {
            event->ignore();
        }
        foreach (QObject *child, children()) {
            if (child->inherits("QWidget")) {
                QWidget *widget = static_cast<QWidget *>(child);
                if (!widget->isVisible())
                    widget->deleteLater();
            }
        }
    }
     
    //lorsqu'on clique sur la souris
    void DragWidget::mousePressEvent(QMouseEvent *event)
    {
        QLabel *child = static_cast<QLabel*>(childAt(event->pos()));
        if (!child)
            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);
     
        Qt::DropAction dropAction = drag->exec(Qt::CopyAction | Qt::MoveAction, Qt::CopyAction);
     
        if (dropAction == Qt::MoveAction)
            child->close();
    }

    Mais je n'arrive pas à faire bouger mon label , j'ai essayer ça (bien sûre ça ne marche pas ..)
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
     QMouseEvent ev = new QMouseEvent();
            DragWidget wid = new DragWidget(this);
            DragLabel *wordLabel = new DragLabel(QString::fromStdString(bougerlab),wid);
            wordLabel->mousePressEvent(ev);
            label3->setAcceptDrops(true);

Discussions similaires

  1. Drag and Drop d'un élément dans un TreePanel
    Par major68 dans le forum Ext JS / Sencha
    Réponses: 0
    Dernier message: 01/03/2011, 10h20
  2. pb dans un drag and drop lors de sélection d'un élément a bouger
    Par WalidNat dans le forum Général JavaScript
    Réponses: 3
    Dernier message: 18/10/2006, 12h56
  3. [VB.NET]Drag and Drop dans une Listview
    Par gégécap dans le forum Windows Forms
    Réponses: 5
    Dernier message: 23/08/2006, 18h41
  4. Drag and drop dans un TTreeView
    Par BigBenQ dans le forum C++Builder
    Réponses: 3
    Dernier message: 07/10/2005, 14h57
  5. Savoir ou est deposé un fichier dans un drag and drop ?
    Par mkdual dans le forum API, COM et SDKs
    Réponses: 2
    Dernier message: 24/08/2005, 17h52

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo