Bonjour à tous,

Je suis actuellement en train de m'entraîner sur un code de Qt, et j'aimerai réussir une réalisation, mais je bloque tout simplement sur un élément, et ce malgré le fait que je consulte la doc.
Je travaille sur l'exemple fourni par Qt: http://doc.qt.io/qt-5/qtwidgets-drag...t-example.html

Mon but est de réaliser un aperçu avant impression de la fenêtre affichée ainsi que des petits widgets dedans qui sont des DragLabel, via la classe définit dans le code.

J'arrive à ouvrir après bonne recherche et manipulation l'aperçu avant impression, mais il ne me montre pas la page avec les DragLabel. Voici les modifications que j'ai effectué dans le dragwidget.cpp:

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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#include <QtWidgets>
 
#include "draglabel.h"
#include "dragwidget.h"
 
DragWidget::DragWidget(QWidget *parent)
    : QWidget(parent)
{
    setupAction();
 
    QFile dictionaryFile(":/dictionary/words.txt");
    dictionaryFile.open(QIODevice::ReadOnly);
    QTextStream inputStream(&dictionaryFile);
 
    int x = 5;
    int y = 5;
 
    while (!inputStream.atEnd()) {
        QString word;
        inputStream >> word;
        if (!word.isEmpty()) {
            DragLabel *wordLabel = new DragLabel(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);
    */
 
    setAcceptDrops(true);
    setMinimumSize(400, qMax(200, y));
    setWindowTitle(tr("Draggable Text"));
}
 
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();
        }
    }
}
 
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();
}
 
void DragWidget::mouseDoubleClickEvent(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->setTextInteractionFlags(Qt::TextEditorInteraction);
    child->render(&pixmap);
 
 
   /* Qt::TextFormat actiontext = child->setTextInteractionFlags(Qt::TextEditorInteraction);
    if (actiontext == Qt::Key_Enter)
    child->close();*/
 
    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();*/
}
 
//Méthode pour définir l'action
void DragWidget::setupAction()
{
    QAction *a;
    a = new QAction(this);
    a->setShortcut(Qt::CTRL + Qt::Key_P);
    connect(a, SIGNAL(triggered()), this, SLOT(filePrintPreview()));
    addAction(a);
}
 
//Méthode pour faire l'aperçu avant impression
void DragWidget::filePrintPreview()
{
#ifndef QT_NO_PRINTER
    QPrinter *printer  = new QPrinter(QPrinter::HighResolution);
    printer->setPaperSize(QSizeF(8.5, 5.4), QPrinter::Inch);
    QPrintPreviewDialog *preview = new QPrintPreviewDialog(printer, this);
    connect(preview, SIGNAL(paintRequested(QPrinter*)), this, SLOT(printPreview(QPrinter*)));
    preview->exec();
#endif
}
 
//Cette méthode permet de créer ce qu'on veut afficher dans l'aperçu avant impression
void DragWidget::printPreview(QPrinter *printer)
{
#ifdef QT_NO_PRINTER
    Q_UNUSED(printer);
#else
    textDocument = new QTextDocument;
    textDocument->print(printer);
#endif
}
Je pense bien que c'est dans la méthode printPreview(QPrinter *printer) qu'il faut que j'arrive à dire que je veux que ma fenêtre actuelle soit celle affichée en aperçu avant impression, mais je n'y arrive pas et je vois pas trop comment réaliser ça. Le QTextDocument textDocument que j'ai crée c'était pour afficher du coup une page dans la boite de dialogue de l'aperçu avant impression.

Je vous remercie d'avance pour les réponses que vous pourrez m'apporter.