我打算用QML创建一个应用程序。 我在c ++中实现了很多qt小部件。 c ++小部件应该在QML中使用。
如何整合它们? - 通往dlls的路径 - 是否存在一些qml容器?
我没有找到这个问题的好文档
答案 0 :(得分:-1)
除非有问题的小部件有很多图形(可能是你有一个基于QGraphivsView的类),否则它是可能的,但不是可以实现的。如果您要引用的小部件是"正常" QWidgets,请不要尝试这样做,因为它比它的价值更麻烦。
您需要创建一个继承QQuickPaintedItem并覆盖几个方法的新类:
部首:
class QQmlWidget : public QQuickPaintedItem
{
Q_OBJECT
public:
explicit QMLProfile(QWidget *internalWidget, QQuickItem *parent = 0) : QQuickPaintedItem(parent), internalWidget(internalWidget){}
virtual ~QMLProfile();
void paint(QPainter *painter) override;
protected:
void mouseMoveEvent(QMouseEvent *event);
private:
QWidget *internalWidget;
};
.cpp的: (下面的代码来自Subsurface代码,它在QML上使用QGraphivsView)
void paint(QPainter* painter) {
// let's look at the intended size of the content and scale our scene accordingly
QRect painterRect = painter->viewport();
QRect profileRect = internalWidget->viewport()->rect();
qreal sceneSize = 104; // that should give us 2% margin all around (100x100 scene)
qreal dprComp = devicePixelRatio() * painterRect.width() / profileRect.width();
qreal sx = painterRect.width() / sceneSize / dprComp;
qreal sy = painterRect.height() / sceneSize / dprComp;
// next figure out the weird magic by which we need to shift the painter so the widget is shown
int dpr = rint(devicePixelRatio());
qreal magicShiftFactor = (dpr == 2 ? 0.25 : (dpr == 3 ? 0.33 : 0.0));
// now set up the transformations scale the profile and
// shift the painter (taking its existing transformation into account)
QTransform profileTransform = QTransform();
profileTransform.scale(sx, sy);
QTransform painterTransform = painter->transform();
painterTransform.translate(-painterRect.width() * magicShiftFactor ,-painterRect.height() * magicShiftFactor);
// apply the transformation
painter->setTransform(painterTransform);
internalWidget->setTransform(profileTransform);
// finally, render the profile
internalWidget->render(painter);
}
QQmlWidget::mouseMoveEvent(QMouseEvent *ev){
/* Map the eveent to the Widget */
QQuickPaintedItem(ev);
internalWidget->mouseEvent(ev);
}
正如我所说的那样,可行,但并非真正的直截了当。只有在无法在QML中重新创建小部件时才能这样做。