首页 文章

Qml / Qt / C:在Widget中的QQuickView,无法获得正确的位置

提问于
浏览
1

我正在用QT5.5实现一个简单的程序,它包含一个HTML窗口(QWebview),如下面的截图:

HTML window on the right side

现在我想要为iOS安装该程序,例如对于iPad . 我发现QWebview类不适用于移动系统,因此我不得不将我的HTML窗口更改为带有QML文件的QQuickView(或者有更好的方法吗?) . 我在网上找到了以下代码:

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);

然后我将容器添加到mainWindow . 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
}
}

但不知怎的,我无法正确布局 . HTML window on iOS容器后面有一个空白/白色矩形,容器也不在右侧的HTML窗口中 . 当我在qml文件中更改矩形的宽度和高度时,它只会更改webview的大小,而不是白色背景 .

谁能告诉我,我怎么能做到这一点?我也使用了 container->setParent() 但视图总是在左边 .

1 回答

  • 0

    作者代码的可见性可能不够,但总体而言,更容易在布局中设置具有特定对齐的小部件 . 这样,您可以命令窗口小部件位于某一侧,并使放置不依赖于坐标/主机窗口小部件大小等 . 假设 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
    

    请注意,其余的水平布局应该按照相对位置来填充 .

相关问题