you need to reimplement paintEvent and draw that pixmap.
you need to reimplement paintEvent and draw that pixmap.
Qt Assistant -- rocks!
please, use tags [CODE] & [/CODE].
I tried QPixmap::grabWidget and it only grabs the background. What I draw on the frame in paintEvent is lost.
Is there really no other way? If I reimplement paintEvent then I have to write the painting code for every different kind of widget twice. Plus the style sheet is not applied, so I have to figure out a way to transfer the style from the widget to the QImage. It's ugly and cumbersome. There must be a generic way.
take a look at this example QTDIR/examples/draganddrop/draggableicons
Qt Assistant -- rocks!
please, use tags [CODE] & [/CODE].
In that example the pixmaps are loaded from images.
yes, but you can create needed image and then use it like in that example.
Qt Assistant -- rocks!
please, use tags [CODE] & [/CODE].
Have a look at QWidget::render
You can render on to a pixmap which is a paintdevice![]()
That would be perfect! But again, it doesn't work.
Qt Code:
{ if (event->button() == Qt::LeftButton) { // Prepare the mime data for the drag. mimeData->setText("hello"); this->render(&pixmap); // Create the drag object. drag->setMimeData(mimeData); drag->setPixmap(pixmap); Qt::DropAction dropAction = drag->exec(); } }To copy to clipboard, switch view to plain text mode
The drag starts and all I see is a blank rectangle of the right size, but with the window background color. Not even the frame background color actually. Neither the style is applied, nor are the contents of the frame rendered. Am I not doing something right?
try this (modificated draggableicons example)
NOTE: you must set setAcceptDrops(true) in Test::ctor.Qt Code:
{ QByteArray itemData; mimeData->setData("application/x-dnditemdata", itemData); drag->setMimeData(mimeData); drag->setPixmap(pixmap); drag->setHotSpot(event->pos()); painter.drawText(pixmap.rect().center().x(), pixmap.rect().center().y(), "hello Qt"); drag->exec(Qt::CopyAction | Qt::MoveAction, Qt::CopyAction); }To copy to clipboard, switch view to plain text mode
PS. read more about Drag&Drop
Qt Assistant -- rocks!
please, use tags [CODE] & [/CODE].
I tried it by copy&paste. It also doesn't work. All I see is a rectangle of the right size and the wrong background color and nothing written on it. Also tried not grabbing the pixmap, but just initializing a blank one and then drawing on it: nothing. Not working. Here is the simplest example I can image:
Qt Code:
{ painter.drawText(pixmap.rect().center().x(), pixmap.rect().center().y(), "hello Qt"); mimeData->setText("hello"); drag->setMimeData(mimeData); drag->setPixmap(pixmap); drag->setHotSpot(event->pos()); drag->exec(Qt::CopyAction | Qt::MoveAction, Qt::CopyAction); }To copy to clipboard, switch view to plain text mode
Not working. I tried amoving the pixmap to the heap, but that doesn't help either. I'm out of ideas.
did you set 'setAcceptDrops(true)'?
Qt Assistant -- rocks!
please, use tags [CODE] & [/CODE].
I can drag and drop the frame into a QLineEdit and it will show whatever I set as text into the MimeData. So I assume this line edit has its accept drops set to true. The drag itself works fine. The problem is only related to what the drag looks like. In particular I don't seem to be able to draw on the pixmap. I don't thing the accept drops has anyhting to do with it.
this example works fine for me, but it is not complite, just demo for creating and moving grabbed image.
Qt Code:
#include <QtGui> #include "test.h" { pb->installEventFilter(this); cb->installEventFilter(this); chb->installEventFilter(this); rb->installEventFilter(this); sb->installEventFilter(this); dsp->installEventFilter(this); grid->addWidget(pb, 0, 0); grid->addWidget(cb, 0, 1); grid->addWidget(chb, 0, 2); grid->addWidget(rb, 1, 0); grid->addWidget(sb, 1, 1); grid->addWidget(dsp, 1, 2); setCentralWidget(widget); setAcceptDrops(true); } { processDrag(this, event); } { painter.setPen(Qt::red); painter.drawText(0, pixmap.rect().center().y(), "hello Qt"); QByteArray itemData; mimeData->setData("application/x-dnditemdata", itemData); drag->setMimeData(mimeData); drag->setPixmap(pixmap); drag->setHotSpot(event->pos()); drag->exec(); } { if (widget && me->button() == Qt::LeftButton) { processDrag(widget, me); return true; } } }To copy to clipboard, switch view to plain text mode
Qt Assistant -- rocks!
please, use tags [CODE] & [/CODE].
Cruz (19th January 2009)
When creating the images I'm facing the same problem: how to create them in a generic way.
I'm trying this approach:
Qt Code:
/* A paint function, that takes a painter as an argument. * It's supposed to paint the contents of the frame either * on the frame itself or on a pixmap. */ { painter->setPen(Qt::black); painter->drawText(0, 0, "hello Qt"); } /* The frame is drawing itself on the gui. */ { myPaint(&painter); // Pass it to my painter. } /* Drag action starting. */ { /* Prepare a pixmap for the drag that will be moved along with the mouse. */ // Grab the widget so you get a styled background. // Initialize a painter that draws on the pixmap.... QPainter painter; painter.begin(&pixmap); // ...and pass it to my painter. myPaint(&painter); painter.end(); // Create the drag object. drag->setPixmap(pixmap); }To copy to clipboard, switch view to plain text mode
But it doesn't work. What I paint on the pixmap is not shown. Do you see an error?
Bookmarks