| Trolltech Home | Qt-interest Home | Recent Threads | All Threads | Author | Date | |
| All threads index page 1 | |
Hello, I have simple dialog application with interface (ui) made in designer. I would like to add drag and drop capability to some widgets, QLineEdit and QTreeWidget. I tried promoting to custom widget and subclassing, but with no success. My expirience with QT is not very extensive and QT distribution doesn't contain any example of this kind (ui examples are generally missing). I've read what documentation says about dnd and customizing widgets, however I was unable to make it in practice. As an attachement there is a trivial ui app, just two QLineEdits. I want to add dnd capability to them, so if one drags a file and drops it on QLineEdit, path to the file will appear in it. I would be grateful if somebody could adjust the app to achieve this. Many thanks. Qt opensource 4.1.4, mingw, windows2000, A. Cabel
Attachment:
Attachment:
ui_trivial_app.rar
Description: Binary data
Message 2 in thread
A. Cabel wrote:
> I have simple dialog application with interface (ui) made in designer. I
> would like to add drag and drop capability to some widgets, QLineEdit
> and QTreeWidget. I tried promoting to custom widget and subclassing, but
> with no success.
This is the correct way to do it. :-)
> My expirience with QT is not very extensive and QT distribution doesn't
> contain any example of this kind (ui examples are generally missing).
> I've read what documentation says about dnd and customizing widgets,
> however I was unable to make it in practice.
We'll think about adding more documentation about drag and drop to help
explain what needs to be done for common cases like the one you have.
> As an attachement there is a trivial ui app, just two QLineEdits. I want
> to add dnd capability to them, so if one drags a file and drops it on
> QLineEdit, path to the file will appear in it. I would be grateful if
> somebody could adjust the app to achieve this. Many thanks.
What I did was to promote the QLineEdit widgets in Qt Designer to MyLineEdit
custom widgets. MyLineEdit is a drop-enabled line edit widget that I created
by subclassing QLineEdit. I created a file called mylineedit.h containing
the following definition:
#include <QLineEdit>
class QDropEvent;
class MyLineEdit : public QLineEdit
{
Q_OBJECT
public:
MyLineEdit(QWidget *parent = 0);
protected:
void dropEvent(QDropEvent *event);
};
The implementation in the mylineedit.cpp file looks like this:
#include <QtGui>
#include "mylineedit.h"
MyLineEdit::MyLineEdit(QWidget *parent)
: QLineEdit(parent)
{
}
void MyLineEdit::dropEvent(QDropEvent *event)
{
const QMimeData *data = event->mimeData();
if (data->hasText()) {
setText(data->text());
event->accept();
} else
event->ignore();
}
The key to getting dropped text into our QLineEdit subclass is to
reimplement the dropEvent() handler and check the MIME data supplied in
the event for textual information.
If there's text available, we use it by calling setText() and accept the
event. If no text is available, we just ignore the event.
I'll send a modified version of the original example back to the original
poster. The above code should be enough to help anyone else with a similar
problem.
--
[ signature omitted ]
Message 3 in thread
David Boddie wrote:
> I'll send a modified version of the original example back to the original
> poster. The above code should be enough to help anyone else with a similar
> problem.
Actually, I can't do that because the original author didn't supply a valid
e-mail address. It's available on request to anyone who wants a Rar archive
with the two extra files in it!
--
[ signature omitted ]