Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 来自CodeBehind的QML中的QtSharp | SetProperty_C#_Qt_Qml - Fatal编程技术网

C# 来自CodeBehind的QML中的QtSharp | SetProperty

C# 来自CodeBehind的QML中的QtSharp | SetProperty,c#,qt,qml,C#,Qt,Qml,我正在使用QtSharp在单独的“窗口”中显示QWidget”。现在我的问题是,如何更新属性(在我的示例中是projector.name),它是在qml中从代码隐藏中定义的 代码隐藏 main.qml } 在Qt中,我必须定义Q_属性,该属性在QtSharp库中不可用。也许我只是错过了什么?似乎它实际上不受支持(): 恐怕你目前不能。尚未添加与QML的交互 我当然希望它会很近,但我不能说。与QML集成是必须的,没有第二种意见,但我只是担心在上个月我真的无法在绑定上抽出任何时间。我还有一些模板的工

我正在使用
QtSharp
在单独的“
窗口
”中显示
QWidget
”。现在我的问题是,如何更新
属性(在我的示例中是
projector.name
),它是在
qml
中从代码隐藏中定义的

代码隐藏 main.qml }


Qt
中,我必须定义
Q_属性
,该属性在
QtSharp
库中不可用。也许我只是错过了什么?

似乎它实际上不受支持():

恐怕你目前不能。尚未添加与QML的交互

我当然希望它会很近,但我不能说。与QML集成是必须的,没有第二种意见,但我只是担心在上个月我真的无法在绑定上抽出任何时间。我还有一些模板的工作要完成,在此之前我不会从QML开始。所以欢迎任何帮助。例如,它将帮助我知道如何在理论上做到这一点。我知道PyQt是通过手动修补Qt的元对象系统来实现的,但我仍然没有得到细节


似乎它实际上还不受支持():

恐怕你目前不能。尚未添加与QML的交互

我当然希望它会很近,但我不能说。与QML集成是必须的,没有第二种意见,但我只是担心在上个月我真的无法在绑定上抽出任何时间。我还有一些模板的工作要完成,在此之前我不会从QML开始。所以欢迎任何帮助。例如,它将帮助我知道如何在理论上做到这一点。我知道PyQt是通过手动修补Qt的元对象系统来实现的,但我仍然没有得到细节


事实上,它是有可能遵循的指导方针

您必须在根QML对象中定义一个属性,如Mohammad所指出的(此处
componentName
),如下所示:

然后,您可以通过调用
Info.RootObject.SetProperty(“componentName”,“lorem ipsum”);通过
QQuickWidget
中包含的
RootObject
访问定义的属性


事实上,它是有可能遵循的指导方针

您必须在根QML对象中定义一个属性,如Mohammad所指出的(此处
componentName
),如下所示:

然后,您可以通过调用
Info.RootObject.SetProperty(“componentName”,“lorem ipsum”);通过
QQuickWidget
中包含的
RootObject
访问定义的属性


该属性在QML中是如何定义的?请查看:
text:projector.name
因此您将该元素的文本设置为属性值?直接在qmlSame结果中声明该属性如何。获取初始值,但与代码隐藏的绑定不起作用。请参见下面的
QtSharp
开发人员的回答。该属性在QML中是如何定义的?请查看:
text:projector.name
因此您将该元素的文本设置为属性值?直接在qmlSame结果中声明该属性如何。获取初始值,但与代码隐藏的绑定不起作用。请参见下面的
QtSharp
开发人员提供的我的答案。
public partial class MainWindow : Window
{
    int count = 0;
    private Projector _Projector;

    public MainWindow()
    {
        InitializeComponent();
        Loaded += _OnLoaded;
    }

    private void _OnLoaded(object sender, RoutedEventArgs routedEventArgs)
    {
        unsafe
        {
            var qtApp = new QApplication(ref count, null);
        }
        _Projector = new Projector();

    }

    private void ButtonBase_OnClickOpen(object sender, RoutedEventArgs e)
    {
        _Projector.Show();
    }

    private void ButtonBase_OnClickPaint(object sender, RoutedEventArgs e)
    {
        _Projector.X += 10;
        _Projector.Y += 5;
        if(_Projector.QuickWidget.Hidden)
            _Projector.QuickWidget.Show();
        else
        {
            _Projector.QuickWidget.Hide();                
        }
        _Projector.Paint();
    }
}

public class Projector : QWidget
{
    public int X { get; set; } = 123;
    public int Y { get; set; } = 12;
    public QQuickWidget QuickWidget;

    public Projector()
    {
        WindowTitle = "Paint Demo";

        Resize(800, 800);
        Show();

        QuickWidget = new QQuickWidget(this);
        QuickWidget.Source = new QUrl(@"\QML\main.qml");
        QuickWidget.RootContext.SetContextProperty("projector", this);            
        QuickWidget.SetProperty("name", new QVariant("Hello"));
        QuickWidget.resizeMode = QQuickWidget.ResizeMode.SizeRootObjectToView;
        QuickWidget.Geometry = new QRect(50, 10, 100, 300);
        QuickWidget.UpdatesEnabled = true;
        QuickWidget.Show();
    }

    protected override void OnPaintEvent(QPaintEvent e)
    {
        base.OnPaintEvent(e);
        var painter = new QPainter(this);
        painter.SetRenderHint ( QPainter.RenderHint.Antialiasing );

        DrawPatternsEx ( painter );
        painter.End();
    }

    void DrawPatternsEx(QPainter ptr)
    {
        ptr.SetPen(PenStyle.SolidLine);
        ptr.SetPen(QColor.FromRgb(255,0,0));

        ptr.DrawLine(0, Y, Size.Width, Y);
        ptr.DrawLine(X, 0, X, Size.Height);
    }

    public void Paint()
    {
        QuickWidget.Update();
        Update();
    }
}
import QtQuick 2.4
import QtQuick.Layouts 1.1

Rectangle {
    id: root
    width: 300
    height: 600

    Rectangle {
        id: headerSchritt
        width: 300
        height: 50
        color: "#ff8629"
        anchors.right: parent.right
        anchors.rightMargin: 0
        anchors.left: parent.left
        anchors.leftMargin: 0
        anchors.top: parent.top
        anchors.topMargin: 0

        Text {
            y: 9
            text: qsTr("Schritt:")
            anchors.left: parent.left
            anchors.leftMargin: 10
            anchors.verticalCenter: parent.verticalCenter
            font.bold: false
            font.pixelSize: projector.fontSize
        }
    }


    Text {
        id: currentStepLabel
        anchors.top: headerSchritt.bottom
        anchors.left: parent.left
        anchors.margins: 10
        text: qsTr("Aktuell:")
        font.pixelSize: projector.fontSize
    }
    Text {
        id: currentStep
        font.pixelSize: projector.fontSize
        text: projector.currentStep
        font.bold: true
        anchors.top: headerSchritt.bottom
        anchors.right: parent.right
        anchors.margins: 10
    }

    Text {
        id: numStepsLabel
        anchors.top: currentStep.bottom
        anchors.left: parent.left
        anchors.margins: 10
        text: qsTr("Gesamt:")
        font.pixelSize: projector.fontSize
    }
    Text {
        id: numSteps
        font.pixelSize: projector.fontSize
        text: projector.numSteps
        anchors.top: currentStep.bottom
        anchors.right: parent.right
        anchors.margins: 10
    }

    Rectangle {
        id: headerComponent
        height: 50
        color: "#ff8629"
        anchors.right: parent.right
        anchors.rightMargin: 0
        anchors.left: parent.left
        anchors.leftMargin: 0
        anchors.top: numSteps.bottom
        anchors.topMargin: 10

        Text {
            y: 9
            text: qsTr("Komponente:")
            anchors.left: parent.left
            anchors.leftMargin: 10
            anchors.verticalCenter: parent.verticalCenter
            font.pixelSize: projector.fontSize
        }
    }

    Text {
        id: name
        text: projector.name
        wrapMode: Text.WrapAtWordBoundaryOrAnywhere
        font.family: "Courier"
        anchors.top: headerComponent.bottom
        anchors.right: parent.right
        anchors.left: parent.left
        anchors.margins: 10
        font.italic: false
        font.pixelSize: projector.fontSize
    }

    Text {
        id: componentCode
        text: projector.cCode
        wrapMode: Text.WrapAtWordBoundaryOrAnywhere
        anchors.top: name.bottom
        anchors.right: parent.right
        anchors.left: parent.left
        anchors.margins: 10
        font.bold: true
        font.pixelSize: projector.fontSize
    }

    Text {
        id: componentName
        font.pixelSize: projector.fontSize
        text: projector.cName
        wrapMode: Text.WrapAtWordBoundaryOrAnywhere
        anchors.top: componentCode.bottom
        anchors.right: parent.right
        anchors.left: parent.left
        anchors.margins: 10
    }

    Image {
        id: componentPicture
        width: height
        anchors.bottom: parent.bottom
        anchors.top: componentName.bottom
        anchors.margins: 10
        anchors.horizontalCenter: parent.horizontalCenter
        source: "image://componentpictures/" + projector.cId
        fillMode: Image.PreserveAspectFit
    }
import QtQuick 2.4
import QtQuick.Layouts 1.1

Rectangle {
    id: root
    width: 300
    height: 600

    property string componentName: "test"

    Rectangle {
        id: headerSchritt
        width: 300
        height: 50
        color: "#ff8629"
        anchors.right: parent.right
        anchors.rightMargin: 0
        anchors.left: parent.left
        anchors.leftMargin: 0
        anchors.top: parent.top
        anchors.topMargin: 0

        Text {
            y: 9
            text: qsTr("Schritt:")
            anchors.left: parent.left
            anchors.leftMargin: 10
            anchors.verticalCenter: parent.verticalCenter
            font.bold: false
            font.pixelSize: projector.fontSize
        }
    }


    Text {
        id: currentStepLabel
        anchors.top: headerSchritt.bottom
        anchors.left: parent.left
        anchors.margins: 10
        text: qsTr("Aktuell:")
        font.pixelSize: projector.fontSize
    }
    Text {
        id: currentStep
        font.pixelSize: projector.fontSize
        text: projector.currentStep
        font.bold: true
        anchors.top: headerSchritt.bottom
        anchors.right: parent.right
        anchors.margins: 10
    }

    Text {
        id: numStepsLabel
        anchors.top: currentStep.bottom
        anchors.left: parent.left
        anchors.margins: 10
        text: qsTr("Gesamt:")
        font.pixelSize: projector.fontSize
    }
    Text {
        id: numSteps
        font.pixelSize: projector.fontSize
        text: projector.numSteps
        anchors.top: currentStep.bottom
        anchors.right: parent.right
        anchors.margins: 10
    }

    Rectangle {
        id: headerComponent
        height: 50
        color: "#ff8629"
        anchors.right: parent.right
        anchors.rightMargin: 0
        anchors.left: parent.left
        anchors.leftMargin: 0
        anchors.top: numSteps.bottom
        anchors.topMargin: 10

        Text {
            y: 9
            text: qsTr("Komponente:")
            anchors.left: parent.left
            anchors.leftMargin: 10
            anchors.verticalCenter: parent.verticalCenter
            font.pixelSize: projector.fontSize
        }
    }

    Text {
        id: name
        text: root.componentName
        wrapMode: Text.WrapAtWordBoundaryOrAnywhere
        font.family: "Courier"
        anchors.top: headerComponent.bottom
        anchors.right: parent.right
        anchors.left: parent.left
        anchors.margins: 10
        font.italic: false
        font.pixelSize: projector.fontSize
    }

    Text {
        id: componentCode
        text: projector.cCode
        wrapMode: Text.WrapAtWordBoundaryOrAnywhere
        anchors.top: name.bottom
        anchors.right: parent.right
        anchors.left: parent.left
        anchors.margins: 10
        font.bold: true
        font.pixelSize: projector.fontSize
    }

    Text {
        id: componentName
        font.pixelSize: projector.fontSize
        text: projector.cName
        wrapMode: Text.WrapAtWordBoundaryOrAnywhere
        anchors.top: componentCode.bottom
        anchors.right: parent.right
        anchors.left: parent.left
        anchors.margins: 10
    }

    Image {
        id: componentPicture
        width: height
        anchors.bottom: parent.bottom
        anchors.top: componentName.bottom
        anchors.margins: 10
        anchors.horizontalCenter: parent.horizontalCenter
        source: "image://componentpictures/" + projector.cId
        fillMode: Image.PreserveAspectFit
    }
}
public partial class MainWindow : Window
{
    int count = 0;
    private Projector _Projector;

    public MainWindow()
    {
        InitializeComponent();
        Loaded += _OnLoaded;
    }

    private void _OnLoaded(object sender, RoutedEventArgs routedEventArgs)
    {
        unsafe
        {
            var qtApp = new QApplication(ref count, null);
        }
        _Projector = new Projector();

    }

    private void ButtonBase_OnClickOpen(object sender, RoutedEventArgs e)
    {
        _Projector.Show();
    }

    private void ButtonBase_OnClickPaint(object sender, RoutedEventArgs e)
    {
        _Projector.X += 10;
        _Projector.Y += 5;
        if(_Projector.Info.Hidden)
            _Projector.Info.Show();
        else
        {
            _Projector.Info.Hide();                
        }
        _Projector.Paint();
    }
}

public class Projector : QWidget
{
    public int X { get; set; } = 123;
    public int Y { get; set; } = 12;

    public QQuickWidget Info;
    public QObject qmlRoot;

    public Projector()
    {
        WindowTitle = "Paint Demo";
        Palette.SetColor(QPalette.ColorRole.Background, QColor.FromRgb(0,0,0));

        Resize(800, 800);
        Show();

        Info = new QQuickWidget(this);
        Info.Source = new QUrl(@"\QML\main.qml");
        Info.RootObject.SetProperty("componentName", "lorem ipsum");
        Info.resizeMode = QQuickWidget.ResizeMode.SizeRootObjectToView;
        Info.Geometry = new QRect(50, 10, 100, 300);
        Info.Show();
    }

    protected override void OnPaintEvent(QPaintEvent e)
    {
        base.OnPaintEvent(e);
        var painter = new QPainter(this);
        painter.SetRenderHint ( QPainter.RenderHint.Antialiasing );

        DrawPatternsEx ( painter );
        painter.End();
    }

    void DrawPatternsEx(QPainter ptr)
    {
        ptr.SetPen(PenStyle.SolidLine);
        ptr.SetPen(QColor.FromRgb(255,0,0));

        ptr.DrawLine(0, Y, Size.Width, Y);
        ptr.DrawLine(X, 0, X, Size.Height);
    }

    public void Paint()
    {
        Update();
    }
}