Bonjour,
J'ai réutilisé le tuto de Qt "FridgeMagnet" pour l'intégrer à mon application.
Lorsque que je drop le widget sélectionné, l'application crash..
Ayant suivit scrupuleusement le tuto, je ne comprend pas d'ou vient le problème.
Classe Bubble (DragLabel dans le tuto) :
Classe DragBubble (DragWidget dans le tuto) :
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 Bubble::Bubble() {} Bubble::Bubble(const QString & text, QWidget * parent) : QLabel(parent) { QFontMetrics metric(font()); QSize size = metric.size(Qt::TextSingleLine, text); QImage image(size.width() + 12, size.height() + 12, QImage::Format_ARGB32_Premultiplied); image.fill(qRgba(0, 0, 0, 0)); QFont font; font.setStyleStrategy(QFont::ForceOutline); QLinearGradient gradient(0, 0, 0, image.height()-1); gradient.setColorAt(0.0, Qt::white); gradient.setColorAt(0.2, QColor(200, 200, 255)); gradient.setColorAt(0.8, QColor(200, 200, 255)); gradient.setColorAt(1.0, QColor(127, 127, 200)); QPainter painter; painter.begin(&image); painter.setRenderHint(QPainter::Antialiasing); painter.setBrush(gradient); painter.drawRoundedRect(QRectF(0.5, 0.5, image.width()-1, image.height()-1), 25, 25, Qt::RelativeSize); painter.setFont(font); painter.setBrush(Qt::black); painter.drawText(QRect(QPoint(6, 6), size), Qt::AlignCenter, text); painter.end(); setPixmap(QPixmap::fromImage(image)); this->bubbleText = text; } QString Bubble::getBubbleText() const { return this->Bubble::bubbleText; }
J’espère que vous pourrez m'aider.
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139 DragBubble::DragBubble(QWidget * parent) : QWidget(parent) { /*TODO: ce qui suit est pour les test. Ecrire le bouclage sur toutes les bulles qui existent et integrer MAJ creation/suppression*/ QTextStream inputStream("TEST TEST", QIODevice::ReadOnly); int x = 5; int y = 5; while (!inputStream.atEnd()) { QString word; inputStream >> word; if (!word.isEmpty()) { Bubble *wordLabel = new Bubble(word, this); wordLabel->move(x, y); wordLabel->show(); wordLabel->setAttribute(Qt::WA_DeleteOnClose); x += wordLabel->width() + 2; if (x >= 245) { x = 5; y += wordLabel->height() + 2; } } } QPalette newPalette = palette(); newPalette.setColor(QPalette::Window, Qt::white); setPalette(newPalette); setMinimumSize(400, qMax(200, y)); setAcceptDrops(true); } //Methodes : Redefinition void DragBubble::dragEnterEvent(QDragEnterEvent *event) { if (event->mimeData()->hasFormat("application/x-DSIBubble")) { if (children().contains(event->source())) { event->setDropAction(Qt::MoveAction); event->accept(); } else { event->acceptProposedAction(); } } else if (event->mimeData()->hasText()) { event->acceptProposedAction(); } else { event->ignore(); } } void DragBubble::dragMoveEvent(QDragMoveEvent *event) { if (event->mimeData()->hasFormat("application/x-DSIBubble")) { if (children().contains(event->source())) { event->setDropAction(Qt::MoveAction); event->accept(); } else { event->acceptProposedAction(); } } else if (event->mimeData()->hasText()) { event->acceptProposedAction(); } else { event->ignore(); } } void DragBubble::dropEvent(QDropEvent *event) { if (event->mimeData()->hasFormat("application/x-DSIBubble")) { const QMimeData * mime = event->mimeData(); QByteArray itemData = mime->data("application/x-DSIBubble"); QDataStream dataStream(&itemData, QIODevice::ReadOnly); QString text; QPoint offset; dataStream >> text >> offset; //QMessageBox::information(NULL, "Titre de la fenêtre", "ABC");//test //Si CETTE LIGNE EST COMMENTE CA NE PLANTE PLUS! Bubble * newLabel = new Bubble("ABC", this); /*newLabel->move(event->pos() - offset); newLabel->show(); newLabel->setAttribute(Qt::WA_DeleteOnClose); if (event->source() == this) { event->setDropAction(Qt::MoveAction); event->accept(); } else { event->acceptProposedAction(); } } else if (event->mimeData()->hasText()) { //Permet le drag&drop depuis une selection de texte d'une autre application QStringList pieces = event->mimeData()->text().split(QRegExp("\\s+"), QString::SkipEmptyParts); QPoint position = event->pos(); foreach (QString piece, pieces) { Bubble *newLabel = new Bubble(piece, this); newLabel->move(position); newLabel->show(); newLabel->setAttribute(Qt::WA_DeleteOnClose); position += QPoint(newLabel->width(), 0); } event->acceptProposedAction();*/ } else { event->ignore(); } } void DragBubble::mousePressEvent(QMouseEvent *event) { Bubble *child = static_cast<Bubble*>(childAt(event->pos())); if (!child) return; QPoint hotSpot = event->pos() - child->pos(); QByteArray itemData; QDataStream dataStream(&itemData, QIODevice::WriteOnly); dataStream << child->getBubbleText() << QPoint(hotSpot); QMimeData * mimeData = new QMimeData; mimeData->setData("application/x-DSIBubble", itemData); mimeData->setText(child->getBubbleText()); QDrag * drag = new QDrag(this); drag->setMimeData(mimeData); drag->setPixmap(*child->pixmap()); drag->setHotSpot(hotSpot); child->hide(); if (drag->exec(Qt::MoveAction | Qt::CopyAction, Qt::CopyAction) == Qt::MoveAction) child->close(); else child->show(); }
Merci d'avance.
Partager