[Kde-bindings] [Qyoto] Casting QObjects

Dimitar Dobrev dpldobrev at yahoo.com
Thu May 24 20:42:56 UTC 2012

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);


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 {
        static void Main(string[] args) {
            QApplication app = new QApplication(args);
            QMainWindow win = new QMainWindow();
            QPushButton btn = new QPushButton(win);
            win.WindowTitle = "Demo Application";
            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");

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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.kde.org/pipermail/kde-bindings/attachments/20120524/498f31cd/attachment.html>

More information about the Kde-bindings mailing list