<html><body><div style="color:#000; background-color:#fff; font-family:times new roman, new york, times, serif;font-size:12pt"><div><span>Hi, Mach,</span></div><div><br><span></span></div><div><span>On my machine your sample behaves the same way with the addition that traditional casting (that is, replacing <br></span></div><div>QWidget widget = (QWidget) child.MetaObject().Cast(child);</div><div><span><br></span></div><div><span>with</span></div><div><span><br></span></div><div><span></span><span>QWidget widget = (QWidget) child;</span></div><div><span><br></span></div><div><span>work as well). </span><span>Apparently </span><span>There is a bug in the </span>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.<br></div><div><br></div>  <div style="font-family: times new roman, new york, times, serif; font-size: 12pt;"> <div
 style="font-family: times new roman, new york, times, serif; font-size: 12pt;"> <div dir="ltr"> <font size="2" face="Arial"> <hr size="1">  <b><span style="font-weight:bold;">From:</span></b> Mach <metator.pt@gmail.com><br> <b><span style="font-weight: bold;">To:</span></b> Kde-bindings@kde.org <br> <b><span style="font-weight: bold;">Sent:</span></b> Thursday, May 24, 2012 10:54 PM<br> <b><span style="font-weight: bold;">Subject:</span></b> [Kde-bindings] [Qyoto] Casting QObjects<br> </font> </div> <br>
Hi all,<br><br>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:<br><br>using System;<br>using Qyoto;<br><br>namespace QtTest {<br>    class Program {<br>        [STAThread]<br>        static void Main(string[] args) {<br>            QApplication app = new QApplication(args);<br>            QMainWindow win = new QMainWindow();<br>            QPushButton btn =
 new QPushButton(win);<br>            win.WindowTitle = "Demo Application";<br>            win.Show();<br>            foreach (QObject child in win.Children()) {<br>                if (child.IsWidgetType()) {<br>                    try {<br>                        QWidget widget = (QWidget) child.MetaObject().Cast(child);<br>                        Console.WriteLine("Cast succeeded. It's now a " + widget.GetType().Name);<br>                    } catch (InvalidCastException e) {<br>                        Console.WriteLine(child.ObjectName + " of type "
 + child.MetaObject().ClassName() +<br>                                          " cannot be cast to QWidget");<br>                    }<br>                }<br>            }<br>            QApplication.Exec();<br>        }<br>    }<br>}<br><br>The output on the console is:<br><br>qt_rubberband of type QRubberBand cannot be cast to QWidget<br>Cast succeeded. It's now a QPushButton<br><br>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.<br><br>Can someone help me out solving this
 problem?<br><br>Thanks in advance. Regards<br><br><br>_______________________________________________<br>Kde-bindings mailing list<br><a ymailto="mailto:Kde-bindings@kde.org" href="mailto:Kde-bindings@kde.org">Kde-bindings@kde.org</a><br><a href="https://mail.kde.org/mailman/listinfo/kde-bindings" target="_blank">https://mail.kde.org/mailman/listinfo/kde-bindings</a><br><br><br> </div> </div>  </div></body></html>