如何调整 QWidget 中嵌入的 QML 小部件的大小?
如何自动调整 QML 小部件的大小?
我有手工创建的 QWidget。 在此小部件中创建了 QML 组件。
但是当我调整 QWidget 的大小时,QML 组件不会调整大小。
一些代码...
我有 MyCustomQWidget 类
标题:
Class MyCustomQWidget : public QWidget
{
Q_OBJECT
public:
QDeclarativeView* view;
private:
QWidget* m_GUI;
public:
QWidget* getGUI() {return m_GUI;};
}
来源:
MyCustomQWidget:: MyCustomQWidget (QWidget *parent) :QWidget(parent)
{
m_GUI = new QWidget();
view = new QDeclarativeView(m_GUI);
view->setSource(QUrl("qrc:/qml/gui.qml"));
//view->setResizeMode(QDeclarativeView::SizeRootObjectToView);
}
在主 gui 框架小部件中
QWidget* pCustomGUI = new MyCustomQWidget(…)
pVLayoutLeft->addWidget(pCustomGUI->getGUI);
How can i resize automatically QML widget?
I have QWidget created by hand.
In this widget created QML component.
But when i resize QWidget, QML component doesn't resize.
Some code...
I have MyCustomQWidget class
Header:
Class MyCustomQWidget : public QWidget
{
Q_OBJECT
public:
QDeclarativeView* view;
private:
QWidget* m_GUI;
public:
QWidget* getGUI() {return m_GUI;};
}
Source:
MyCustomQWidget:: MyCustomQWidget (QWidget *parent) :QWidget(parent)
{
m_GUI = new QWidget();
view = new QDeclarativeView(m_GUI);
view->setSource(QUrl("qrc:/qml/gui.qml"));
//view->setResizeMode(QDeclarativeView::SizeRootObjectToView);
}
In main gui frame widget
QWidget* pCustomGUI = new MyCustomQWidget(…)
pVLayoutLeft->addWidget(pCustomGUI->getGUI);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
问题中没有太多细节,但如果您使用 QDeclarativeView 来显示 QML,请查看其 setResizeMode() 成员。将其设置为 QDeclarativeView::SizeRootObjectToView 可能会满足您的需求:它会自动将 QML 的根对象调整为视图的大小。
There is not much detail in the question, but if you are using a
QDeclarativeView
to show the QML, have a look at itssetResizeMode()
member. Setting this toQDeclarativeView::SizeRootObjectToView
might just do what you are looking for: it resizes QML's root object automatically to the size of the view.当您将一个 Qt 小部件放入另一个 Qt 小部件中时,您必须手动调整其大小或使用布局自动执行此操作。
创建没有显式父级的小部件并在添加小部件时让布局分配父级是有点传统的做法。
我不太确定为什么这里有 3 层小部件,但假设您不能只为自定义小部件创建 QDeclarativeView 子类,那么您最终可能会得到如下结果
:
When you put a Qt widget inside another Qt widget, you must manually resize it or use a layout to do this automatically.
It is somewhat traditional to create a widget with no explicit parent and let the layout assign the parent when you add the widget.
I'm not really sure why you have 3 layers of widgets here but assuming you can't just sub-class QDeclarativeView for your custom widget, you might end up with something like this:
and
这使 FocusScope 适合父对象的大小,
在本例中为 QDeclarativeView。
This fits the FocusScope to the size of the parent object,
in this case the QDeclarativeView.