Qt-jambi-interest Archive, December 2006
Custom drag'n drop
Message 1 in thread
Hello,
I try to create a custom drag'n drop, but I have lots of troubles.
I didn't find a suitable example, so I tried to mimic a C++ example.
This is the code for creating the mimedata :
QPolygonF polygon = new QPolygonF();
....
QMimeData mimeData = new QMimeData();
QByteArray encodedData = new QByteArray();
QDataStream stream = new DataStream(encodedData.nativePointer(),
new QIODevice.OpenMode(QIODevice.OpenModeFlag.WriteOnly));
stream.operator_shift_left(polygon.nativePointer());
mimeData.setData("application/x-polygon", encodedData);
return mimeData;
And this the code to read the data :
QByteArray polygonData = event.mimeData().data("application/x-polygon");
QDataStream stream = new QDataStream(polygonData.nativePointer(),
new QIODevice.OpenMode(QIODevice.OpenModeFlag.ReadOnly));
QPolygonF polygon = new QPolygonF();
stream.operator_shift_right(polygon.nativePointer());
...
event.accept();
It seems that I tried to read a null pointer. Did I make something wrong
? How can I do it ?
As a side note, I would like to give a little feedback on Qt jambi from
a C++/Qt developer (I have done a little bit of Java/SWT).
Qt Jambi is far better than SWT. I find my way easily between the
different classes of Qt jambi. This are the problems I have faced :
- no assistant... I know Java developers are used to Javadoc, but it
is really difficult for me to use it,
- the doc is for C++ (ok, I know, it's just a TP),
- I really missed QVariant, maybe the biggest problem for me.
The integration in Eclipse is perfect for me, no problem for the moment.
Signals/slots are different, but easily understandable.
Maybe you should make a special chapter in the doc for C++ developer,
and explain the difference (and how to transform an Object in double for
example...).
Nicolas
PS : sorry for my english...
Message 2 in thread
Hi, Nicolas.
Nicolas Arnaud-Cormos wrote:
> Hello,
>
> I try to create a custom drag'n drop, but I have lots of troubles.
> I didn't find a suitable example, so I tried to mimic a C++ example.
>
> This is the code for creating the mimedata :
>
> QPolygonF polygon = new QPolygonF();
> ....
> QMimeData mimeData = new QMimeData();
> QByteArray encodedData = new QByteArray();
> QDataStream stream = new DataStream(encodedData.nativePointer(),
> new QIODevice.OpenMode(QIODevice.OpenModeFlag.WriteOnly));
>
> stream.operator_shift_left(polygon.nativePointer());
Unfortunately, working with QNativePointer can be very confusing, so in
the API of the next Qt Jambi release, it will not be visible except for
in a very few special cases. I'll give you some code to help you work
with the current version, though.
The QDataStream.operator_shift_left() that takes a QNativePointer in Qt
Jambi TP3 is, in C++ terms, the mapping of the function operator<<(const
char *str), which is why your code does not work as expected. In C++
there are stream operators for QDataStream which takes QPolygonF
objects, but these are not included in the current version of Qt Jambi.
We'll see what we can do about that.
For now, to use the QDataStream API, you will unfortunately have to
unpack your polygon yourself:
QPolygonF polygon = new QPolygonF();
...
QMimeData mimeData = new QMimeData();
QByteArray encodedData = new QByteArray();
QDataStream stream = new QDataStream(encodedData.nativePointer(),
new QIODevice.OpenMode(QIODevice.OpenModeFlag.WriteOnly));
stream.operator_shift_left(polygon.size());
for (int i=0; i<polygon.size(); ++i) {
stream.operator_shift_left(polygon.at(i).x());
stream.operator_shift_left(polygon.at(i).y());
}
mimeData.setData("application/x-polygon", encodedData);
return mimeData;
and to read it in you will unfortunately have to write some arduous
QNativePointer code:
QByteArray polygonData = event.mimeData().data("application/x-polygon");
QDataStream stream = new QDataStream(polygonData);
QPolygonF polygon = new QPolygonF();
QNativePointer size = new QNativePointer(QNativePointer.Type.Int);
stream.operator_shift_right_unsigned_int(size);
QNativePointer np = new QNativePointer(QNativePointer.Type.Double);
for (int i=0; i<size.intValue(); ++i) {
stream.operator_shift_right_double(np);
double x = np.doubleValue();
stream.operator_shift_right_double(np);
double y = np.doubleValue();
polygon.append(new QPointF(x, y));
}
event.accept();
>
> - I really missed QVariant, maybe the biggest problem for me.
java.lang.Object in Java does in principle serve most of the same
purposes as QVariant does in C++. One particular thing it doesn't
support is conversions between types, so we have
com.trolltech.qt.QVariant for this purpose. This class is, admittedly,
difficult to find using the current documentation. We'll work to improve
this.
Is there any other functionality you require which is not handled by the
combination of these two classes? We'd be very interested to hear about
it :-)
Hope this helps, and thank you for all your comments. I'll make a note
of all this, and also note the need for a drag-and-drop-example in Qt
Jambi.
-- Eskil
Message 3 in thread
Hi Eskil,
> Unfortunately, working with QNativePointer can be very confusing, so in
> the API of the next Qt Jambi release, it will not be visible except for
> in a very few special cases. I'll give you some code to help you work
> with the current version, though.
OK, I must admit I don't understand the concept of QNativePointer, maybe
it's something special in Java... I found it really difficult to use
(why do I need a native pointer ?).
Your code is working like a charm ;) Thank you very much.
> java.lang.Object in Java does in principle serve most of the same
> purposes as QVariant does in C++. One particular thing it doesn't
> support is conversions between types, so we have
> com.trolltech.qt.QVariant for this purpose. This class is, admittedly,
> difficult to find using the current documentation. We'll work to improve
> this.
OK, I didn't find QVariant (I must admit I didn't search for, I thought
Object replaced it). It's exactly what I need.
Just one thing, in your examples, you should use it. I found a toDouble
function in one example (this is why I didn't search for QVariant, but
copy and paste this function).
> Is there any other functionality you require which is not handled by the
> combination of these two classes? We'd be very interested to hear about
> it :-)
In it's actual state, QVariant is perfect (for me) : static functions to
make conversions between types. I miss a toColor/toPolygon functions,
but it's ok.
> Hope this helps, and thank you for all your comments. I'll make a note
> of all this, and also note the need for a drag-and-drop-example in Qt
> Jambi.
May I suggest you to use the puzzle example in Qt. I really like the
idea to have the same examples in Qt and Qt jambi, to compare the code.
Thank you for your help.
Nicolas
Message 4 in thread
Gunnar Sletta a écrit :
> The class with the static conversion functions is:
> com.trolltech.qt.QVariant
I wanted to say I didn't find the class before Eskil poit it to me. And
I'm not used to the naming of Java class, I use QVariant for
com.trolltech.qt.QVariant...
>> Just one thing, in your examples, you should use it. I found a
>> toDouble function in one example (this is why I didn't search for
>> QVariant, but copy and paste this function).
>
> We use the QVariant concept in for instance our modelview examples where
> the QAbstractItemModel.data() function actually returns a QVariant, but
> the signature is only Object so you won't see it unless you do a side by
> side comparrison with the C++ API.
In the ItemViewChart example, line 189, you use toDouble() that you
defined at the end like that :
private double toDouble(Object o) {
if (o instanceof String) {
try {
return Double.parseDouble((String) o);
} catch (NumberFormatException e) {
}
}
return 0;
}
I just wanted to say that you should use QVariant.toDouble in this
example. I used this example as model for my code, this is why I didn't
notice QVariant.
Sorry if I'm not clear.
Nicolas
Message 5 in thread
Nicolas Arnaud-Cormos wrote:
> In the ItemViewChart example, line 189, you use toDouble() that you
> defined at the end like that :
>
...
>
> I just wanted to say that you should use QVariant.toDouble in this
> example. I used this example as model for my code, this is why I didn't
> notice QVariant.
> Sorry if I'm not clear.
We'll definilty update that, thanks ;-)
Message 6 in thread
On 12/4/06, Nicolas Arnaud-Cormos <nikikko@xxxxxxx> wrote:
>
>
>
>
> This are the problems I have faced :
> - no assistant... I know Java developers are used to Javadoc, but it
> is really difficult for me to use it,
> - the doc is for C++ (ok, I know, it's just a TP),
>
> I found it very hard to use html java documentation and wrote a simple
program to create a content file for Qt Assistant, so you can use it for
java documentation. It can be found
athttp://sergey.melderis.googlepages.com/javadoctoqt
Sergejs Melderis.
Message 7 in thread
Sergejs Melderis wrote:
>> This are the problems I have faced :
>> - no assistant... I know Java developers are used to Javadoc, but it
>> is really difficult for me to use it,
>> - the doc is for C++ (ok, I know, it's just a TP),
>>
>> I found it very hard to use html java documentation and wrote a simple
>
> program to create a content file for Qt Assistant, so you can use it for
> java documentation. It can be found
> at
> http://sergey.melderis.googlepages.com/javadoctoqt
Cool stuff ;-)
-
Gunnar