[Kde-bindings] [Qyoto] Casting QObjects
Mach
metator.pt at gmail.com
Thu May 24 21:38:15 UTC 2012
Hi Dimitar,
Thank you for your quick reply. Well, i take back what i said about
traditional casting in C#. Seems like this qt_rubberband object is
somewhat special (i don't have much experience in Qt). For instance,
following my example this line of code returns null: QObject rubberBand
= win.FindChild<QObject>("qt_rubberband");
What i really find strange though, is that qt_rubberband is considered
to be only a QObject. Shouldn't it be a QRubberBand (which is a QWidget...)?
Regards
PS: I take this opportunity to thank you (and also Steven Boswell
regarding building Qyoto in Windows) for making Qt available to csharpers.
Em 24-05-2012 21:42, Dimitar Dobrev escreveu:
> Hi, Mach,
>
> On my machine your sample behaves the same way with the addition that
> traditional casting (that is, replacing
> QWidget widget = (QWidget) child.MetaObject().Cast(child);
>
> with
>
> QWidget widget = (QWidget) child;
>
> work as well). Apparently There is a bug in the IsWidgetType function
> because it allows the "qt_rubberband" which the debugger shows as a
> QObject only, to pass. However, "if (child is QWidget)" works properly
> so you can use that.
>
> ------------------------------------------------------------------------
> *From:* Mach <metator.pt at gmail.com>
> *To:* Kde-bindings at kde.org
> *Sent:* Thursday, May 24, 2012 10:54 PM
> *Subject:* [Kde-bindings] [Qyoto] Casting QObjects
>
> Hi all,
>
> I've managed to compile qyoto on windows and now i'm doing some test
> applications to try it out. I'm having trouble trying to downcast
> QObjects. Traditional casting in C# does not work, but i was expecting
> that. So i started to look for something that could perform a cast and
> found the method Cast(QObject) in QMetaObject but it's not working
> always. Below is an example application that tries to cast all the
> children of the main window to widgets. Notice that i previously check
> if the object is a widget type:
>
> using System;
> using Qyoto;
>
> namespace QtTest {
> class Program {
> [STAThread]
> static void Main(string[] args) {
> QApplication app = new QApplication(args);
> QMainWindow win = new QMainWindow();
> QPushButton btn = new QPushButton(win);
> win.WindowTitle = "Demo Application";
> win.Show();
> foreach (QObject child in win.Children()) {
> if (child.IsWidgetType()) {
> try {
> QWidget widget = (QWidget)
> child.MetaObject().Cast(child);
> Console.WriteLine("Cast succeeded. It's now a
> " + widget.GetType().Name);
> } catch (InvalidCastException e) {
> Console.WriteLine(child.ObjectName + " of type
> " + child.MetaObject().ClassName() +
> " cannot be cast to QWidget");
> }
> }
> }
> QApplication.Exec();
> }
> }
> }
>
> The output on the console is:
>
> qt_rubberband of type QRubberBand cannot be cast to QWidget
> Cast succeeded. It's now a QPushButton
>
> I suspect this has something to do with the fact that qt_rubberband
> was created on the unmanaged side, whereas QPushButton was created by
> the managed side. I also tried calling
> QWidget.StaticMetaObject.Cast(QObject) but it's always returning null.
>
> Can someone help me out solving this problem?
>
> Thanks in advance. Regards
>
>
> _______________________________________________
> Kde-bindings mailing list
> Kde-bindings at kde.org <mailto:Kde-bindings at kde.org>
> https://mail.kde.org/mailman/listinfo/kde-bindings
>
>
>
>
> _______________________________________________
> Kde-bindings mailing list
> Kde-bindings at kde.org
> https://mail.kde.org/mailman/listinfo/kde-bindings
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.kde.org/pipermail/kde-bindings/attachments/20120524/8496201f/attachment-0001.html>
More information about the Kde-bindings
mailing list