我使用QtSharp在单独的"window“中显示一个QWidget。现在,我的问题是如何更新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();
}
}main.qml
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中,我必须定义一个在QtSharp库中不可用的Q_PROPERTY。也许我只是漏掉了什么?
发布于 2018-03-14 08:35:57
实际上,遵循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();
}
}发布于 2018-03-12 14:24:53
似乎它实际上还不受支持(https://github.com/ddobrev/QtSharp/issues/74):
恐怕你现在不行。还没有添加与QML的交互。 我当然希望它会接近,但我不知道。与QML的集成是必须的,没有其他的意见,但我只是担心在过去的一个月里,我实在无法抽出任何时间在绑定上。我还需要完成一些模板的工作,在此之前我不会从QML开始。所以任何帮助都是受欢迎的。例如,它将帮助我知道如何在理论上做到这一点。我知道PyQt是通过手工修改Qt的元对象系统来做到这一点的,但我仍未了解细节。
https://stackoverflow.com/questions/49237167
复制相似问题