我使用QtSharp
在单独的" QWidget
"中显示window
。现在我的问题是,如何更新property
(在我的示例中为projector.name
),这是qml
中从后面的代码中定义的。
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
}
}
在Qt
中,我必须定义Q_PROPERTY
库中不可用的QtSharp
。也许我只是错过了一些东西?
答案 0 :(得分:2)
似乎它实际上不受支持(https://github.com/ddobrev/QtSharp/issues/74):
恐怕你现在不能。尚未添加与QML的交互。
我绝对希望它会接近,但我不知道。与QML集成是必须的,没有第二意见,但我只是担心我在上个月的任何时候都无法在绑定上花费时间。我还有一些工作要完成模板,在此之前我不会从QML开始。所以欢迎任何帮助。例如,它将帮助我知道理论上如何做到这一点。我知道PyQt是通过手动修补Qt的元对象系统来实现的,但我还没有了解细节。
答案 1 :(得分:1)
实际上可以遵循http://doc.qt.io/qt-5/qtqml-cppintegration-interactqmlfromcpp.html的指南。
你必须在根QML对象中定义一个属性,如Mohammad(这里为componentName
)所指出的那样:
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
}
}
然后,您可以通过调用RootObject
QQuickWidget
中包含的Info.RootObject.SetProperty("componentName", "lorem ipsum");
访问已定义的媒体资源
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();
}
}