You are not logged in.

Dear visitor, welcome to QtForum.org. If this is your first visit here, please read the Help. It explains in detail how this page works. To use all features of this page, you should consider registering. Please use the registration form, to register here or read more information about the registration process. If you are already registered, please login here.

Manivannan_1984

Intermediate

  • "Manivannan_1984" is male
  • "Manivannan_1984" started this thread

Posts: 305

Location: chennai

Occupation: Software Engineer

  • Send private message

1

Tuesday, February 27th 2007, 10:58am

Drag & Drop in ListBox

Hi All,

Now i am trying to implement Drag & Drop in QListBox.

I am using Qt3.3.6... I went thru all the posts reg drag & drop..

I set the following..

Source code

1
2
3
setAcceptDrops(true);
setDragSelect(true);
viewport ()->setAcceptDrops(true);


I didn't get clear idea.

Any body can help me out, if u can post a sample code that will be very useful..

Thanks,
mani
[PHP]

Quoted

U can make anything, but nothing can make U.

2

Tuesday, February 27th 2007, 11:31am

RE: Drag & Drop in ListBox

This should help
sunil.thaha has attached the following file:

Manivannan_1984

Intermediate

  • "Manivannan_1984" is male
  • "Manivannan_1984" started this thread

Posts: 305

Location: chennai

Occupation: Software Engineer

  • Send private message

3

Tuesday, February 27th 2007, 11:48am

RE: Drag & Drop in ListBox

hi Sunil,

Thanks for your sample code...

My problem is, I have designed aa Ui and using this in my class which has a list box and some other stuffs..

There, the below functions are not called when I do drag... I have overloaded the below functions...

Quoted

void contentsDragEnterEvent(QDragEnterEvent *event);
void contentsDropEvent(QDropEvent *event);


I didn't inherit QListbox directly to my class, if do I will get many errors..

How this can be solved..?
[PHP]

Quoted

U can make anything, but nothing can make U.

This post has been edited 1 times, last edit by "Manivannan_1984" (Feb 27th 2007, 12:47pm)


4

Tuesday, February 27th 2007, 2:28pm

RE: Drag & Drop in ListBox

Quoted


I didn't inherit QListbox directly to my class,


You should inherits from the QListBox since the void contentsDragEnterEvent(QDragEnterEvent *event), and the void contentsDropEvent(QDropEvent *event) are virtual functions of QScrollView and QListBox inherits it. you will receive these event in the QListBox subclass

Quoted


if do I will get many errors..
How this can be solved..?

Like what ... ?

Manivannan_1984

Intermediate

  • "Manivannan_1984" is male
  • "Manivannan_1984" started this thread

Posts: 305

Location: chennai

Occupation: Software Engineer

  • Send private message

5

Wednesday, February 28th 2007, 5:32am

RE: Drag & Drop in ListBox

Finally I planned to overwrite the my class files generated from UI..

ie; I am creating a new class which has QListBox as its base and using this class in my UI instead of QListBox..

To perform drag&drop whether Should I insert elements as ListBoxItems? If I do insert elemets as string will it work?

Regards,
mani
[PHP]

Quoted

U can make anything, but nothing can make U.

6

Wednesday, February 28th 2007, 5:37am

RE: Drag & Drop in ListBox

There isn't any need to subclass QListBoxItems for drag drop support. You can define a custom widget in the Designer and use it in place of the ListBox

Manivannan_1984

Intermediate

  • "Manivannan_1984" is male
  • "Manivannan_1984" started this thread

Posts: 305

Location: chennai

Occupation: Software Engineer

  • Send private message

7

Wednesday, February 28th 2007, 8:37am

RE: Drag & Drop in ListBox

Hi Sunil.. Pls find my below code what I used from ur sample...

But it doesn't works correctly for me..

Source code

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
ProjectView::ProjectView(QWidget *parent, const char *name)
    : QListBox(parent, name)
{
	setAcceptDrops(true);	
}

void ProjectView::mousePressEvent(QMouseEvent *a_event)
{
	QListBox::mousePressEvent( a_event );
    dragging = TRUE;
}

void ProjectView::mouseMoveEvent(QMouseEvent *a_event)
{
	if ( dragging ) 
	{
		int distance = (a_event->pos() - dragPos).manhattanLength();
        if (distance > QApplication::startDragDistance())
		{
			QDragObject *d = new QTextDrag( currentText(), this );
			d->drag(); // do NOT delete d.
			dragging = FALSE;
		}
	}     
}

void ProjectView::dragEnterEvent(QDragEnterEvent *a_event)
{
	if ( QTextDrag::canDecode( a_event ) )
	{
		 a_event->accept();
	}
}

void ProjectView::dropEvent(QDropEvent *a_event)
{
    QString text;

    if ( QTextDrag::decode( a_event, text ) )
        insertItem( text );
  
}



While I do drag it simply copies the dragged element at last..

What the issue in this code..?

I tried ur ProjectView class with out doing any changes, but that also doesn't works for me...

Regards,
Mani
[PHP]

Quoted

U can make anything, but nothing can make U.

This post has been edited 2 times, last edit by "Manivannan_1984" (Feb 28th 2007, 8:39am)


8

Wednesday, February 28th 2007, 9:45am

RE: Drag & Drop in ListBox

What exactly are you trying to achieve here. The code I gave just demonstrates how the drag and drop works.

Manivannan_1984

Intermediate

  • "Manivannan_1984" is male
  • "Manivannan_1984" started this thread

Posts: 305

Location: chennai

Occupation: Software Engineer

  • Send private message

9

Wednesday, February 28th 2007, 9:54am

RE: Drag & Drop in ListBox

I want to implement drag&drop in QListBox... ie moving items(chaning positions of Items) in the list box using drag and drop...

I used same example which is available in the Qt-Assistant, but as I told you already, it simply copies items at the last index of list box..
[PHP]

Quoted

U can make anything, but nothing can make U.