Well, I've solved in this way:
added the following to the constructor:
m_Painter->setPen(pen);
QPen pen(QColor("blue"));
m_Pixmap = new QPixmap(QSize(180, 20));
m_Painter = new QPainter(m_Pixmap);
m_Painter->setPen(pen);
To copy to clipboard, switch view to plain text mode
and the mousePressEvent is now:
{
if ( currentItem()->text().isEmpty() )
{
clearSelection();
return;
}
dataStream << currentItem()->text();
m_Painter
->drawText
(QRect(10,
0,
170,
20), currentItem
()->text
() );
// ADDED
mimeData->setData("application/x-dnditemdata", itemData);
mimeData->setText(currentItem()->text());
drag->setPixmap(*m_Pixmap); // ADDED
drag->setMimeData(mimeData);
drag->exec(Qt::CopyAction);
}
void CListWidget::mousePressEvent(QMouseEvent *event)
{
QListWidget::mousePressEvent(event);
if ( currentItem()->text().isEmpty() )
{
clearSelection();
return;
}
QByteArray itemData;
QDataStream dataStream(&itemData, QIODevice::WriteOnly);
dataStream << currentItem()->text();
m_Painter->fillRect(QRect(0, 0, 180, 20), QBrush(QColor("orange"))); // ADDED
m_Painter->drawText(QRect(10, 0, 170, 20), currentItem()->text() ); // ADDED
QMimeData *mimeData = new QMimeData;
mimeData->setData("application/x-dnditemdata", itemData);
mimeData->setText(currentItem()->text());
QDrag *drag = new QDrag(this);
drag->setPixmap(*m_Pixmap); // ADDED
drag->setMimeData(mimeData);
drag->exec(Qt::CopyAction);
}
To copy to clipboard, switch view to plain text mode
Bookmarks