简体   繁体   English

Qml / Qt / C ++:小部件中的QQuickView,位置不正确

[英]Qml/Qt/C++: QQuickView in a Widget, can't get the position right

I'am implementing a simple program with QT5.5 which contains a HTML window (QWebview) like the following screenshot: 我正在用QT5.5实现一个简单的程序,其中包含一个HTML窗口(QWebview),如以下屏幕截图所示:

HTML window on the right side 右侧的HTML窗口

Now I want the program also installed for iOS eg for iPad. 现在,我想也为iOS安装该程序,例如iPad。 I found that the QWebview class isn't available for mobile system, so I had to change my HTML window to QQuickView with QML files (or is there a better way?). 我发现QWebview类不适用于移动系统,因此我不得不将HTML窗口更改为带有QML文件的QQuickView(或者有更好的方法吗?)。 I found the following code online: 我在网上找到了以下代码:

QQuickView *view = new QQuickView();
QWidget *container = QWidget::createWindowContainer(view, this);
container->setMinimumSize(200,400);
container->setMaximumSize(200,400);
container->setFocusPolicy(Qt::TabFocus);
view->setSource(QUrl("qrc:///webview.qml"));
layout->addWidget(container);

Then i added the container to the mainWindow. 然后我将容器添加到mainWindow中。 The webview.qml: webview.qml:

import QtQuick 2.5
import QtWebView 1.0
Rectangle {
id: rectangle
width: 200
height: 400
WebView {
    id: webview
    url: "file:///test.html"
    anchors.fill: parent
    width: 200
    height: 400
}
}

But somehow I can't get the layout right. 但是不知何故我无法获得正确的布局。 HTML window on iOS There is a blank/white rectangle behind the container, and the container is also not in the HTML window on the right side where it should be. iOS上的HTML窗口容器后面有一个空白/白色矩形,并且该容器也不位于应位于其右侧的HTML窗口中。 When I change the width and the height of the rectangle in the qml file, it only changes the size of the webview, not the white background. 当我更改qml文件中矩形的宽度和高度时,它仅更改webview的大小,而不更改白色背景。

Can anyone tell me, how can I get this right? 谁能告诉我,我该怎么做? I have also used container->setParent() but the view is always on the left. 我还使用了container->setParent()但视图始终在左侧。

The visibility of the author's code is maybe not enough but overall it is easier to set the widget in layout with certain alignment. 作者代码的可见性可能还不够,但是总的来说,以一定的对齐方式将小部件设置为布局更容易。 This way you command the widget to be on certain side and make the placement not to depend on coordinates / host widget size etc. The layout variable is assumed to be horizontal box layout or QHBoxLayout. 这样,您可以命令小部件位于特定的一侧,并使放置位置不依赖于坐标/宿主小部件尺寸等。 layout变量假定为水平框布局或QHBoxLayout。

// make sure that QHBoxLayout* layout = new QHBoxLayout(this);
// or set the horizontal layout somehow else
QQuickView *view = new QQuickView();
QWidget *container = QWidget::createWindowContainer(view, this);

container->serFixedWidth(200); // the layout is horizontal
                               // and we *may* want to fix the width

container->setFocusPolicy(Qt::TabFocus);
view->setSource(QUrl("qrc:///webview.qml"));
layout->addWidget(container, Qt::AlignRight); // align to the right

Mind that the rest of horizontal layout should be filled like that with respecting relative positions in it. 请注意,其余水平布局应像相对于其中的相对位置一样填充。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM