PDA

View Full Version : Drag and Drop QTableWidget and QTableView



scorpion11
7th April 2008, 14:34
Hi,

I am new to Qt.

I have to implement Drag and Drop feature on QTableWidget and QTableView in my application. I need to drag the table and drop it into another application such as ms-word.

I have already implemented something which works partially...in the sense that, when i drag the table and drop it in ms-word, it gets copied there, but the problem is, if the table contains any scroll bars, the cells behind the scroll bars dont get copied, only that part of the table which is visible gets copied into ms-word.

The code which i use to achieve this is,

QDrag *drag = new QDrag(this);
QMimeData *mimeData = new QMimeData;

QPixmap img = QPixmap::grabWidget(this);

mimeData->setImageData(img.toImage());
drag->setMimeData(mimeData);

I have subclassed QTableWidget and QTableView and implemented the above code in the mouseMoveEvent.

I was hoping the grabWidget() would get the entire TableWidget.

I think the mimeData has to be used properly, but I am not sure how to proceed further on this.

Any help would be very much appreciated.
Thanks and Regards.

wysota
7th April 2008, 14:41
Are you sure you want to copy an image of the table? Maybe it's better to copy the contents instead? By the way, QTableWidget and QAbstractItemModel have dedicated methods for handing drags.

scorpion11
7th April 2008, 14:47
Hi,

My requirement is to get the entire table into ms-word, when i do a drag and drop....even the cells hidden behind the scroll bars.

I am not sure whether to copy as an image or its contents.

Thanks.

wysota
7th April 2008, 15:15
If you want to edit or format the table, I really suggest you copy the contents.

scorpion11
8th April 2008, 09:23
Hi,

Actually, I would like to have the image of the table which will ne non-editable along with its header's also visible in the image.

wysota
8th April 2008, 09:33
In that case you can prepare a pixmap large enough to hold the whole table and use QWidget::render() to render the table on it.