如何在调整大小时保持 SVG 背景和小部件对齐?

问题描述 投票:0回答:1

我正在开发一个 Qt 桌面应用程序,我想要一个 SVG 作为背景。当窗口大小调整时,UI 中的小部件应保持在相对于 SVG 坐标的位置。

我已成功使用

QUiLoader
加载 SVG 和 UI,并将 SVG 设置为背景。

但是,当我调整窗口大小时,小部件不会保持其相对于 SVG 背景的位置。

如何确保窗口小部件和 SVG 一起缩放并在调整窗口大小时保持它们的相对位置?

这是我的代码片段:

// ... some code

QGraphicsSvgItem *svgItem = new QGraphicsSvgItem(":/vectorised-color2.svg");
scene->addItem(svgItem);
view->setScene(scene);
setCentralWidget(view);

QFile file(":/view.ui");
file.open(QFile::ReadOnly);

QUiLoader loader;
QWidget *widget = loader.load(&file, this);
// ... some code to add widget to scene

widget->resize(svgBounds.width(), svgBounds.height());

// ... rest of the code
c++ qt svg background resize
1个回答
0
投票

通过 QUILoader 加载相同的 .ui 文件就可以了,用 SVG 缩放 UI。我不知道为什么。如果您加载单独的 .ui 文件,这似乎不起作用。请注意,如果您有工具栏或状态栏,则需要手动隐藏它们。请注意,您需要手动连接来自 UI 小部件的信号。

#include "mainwindow.h"
#include <QFile>
#include <QGraphicsProxyWidget>
#include <QGraphicsScene>
#include <QGraphicsSvgItem>
#include <QGraphicsView>
#include <QtUiTools/QUiLoader>
#include "./ui_mainwindow.h"
#include "resizinggraphicsview.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    QWidget *spacer = new QWidget(this);
    spacer->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    ui->toolBar->addWidget(spacer);
    ui->toolBar->addAction(ui->actionAbout);

    ResizingGraphicsView *view = new ResizingGraphicsView(this);
    view->setResizeAnchor(QGraphicsView::AnchorViewCenter);
    view->setTransformationAnchor(QGraphicsView::AnchorViewCenter);
    QGraphicsScene *scene = new QGraphicsScene();
    QGraphicsSvgItem *svgItem = new QGraphicsSvgItem(":/vectorised-color2.svg");

    scene->addItem(svgItem);
    view->setScene(scene);
    setCentralWidget(view);

    QFile file(":/mainwindow.ui");
    file.open(QFile::ReadOnly);

    QUiLoader loader;
    QWidget *widget = loader.load(&file, this);

    removeToolbarAndStatusBar(widget);
    QRectF svgBounds = svgItem->boundingRect();
    widget->resize(svgBounds.width(), svgBounds.height());
    file.close();
    widget->setAttribute(Qt::WA_TranslucentBackground);
    QGraphicsProxyWidget *proxy = scene->addWidget(widget);
    proxy->setAutoFillBackground(false);

    // signals need to be explicitly connected
    QPushButton *pushButton = widget->findChild<QPushButton *>("pushButton");
    if (pushButton) {
        connect(pushButton, &QPushButton::clicked, this, &MainWindow::clickbutton);
    }
}

/*
 * This is a hack: For some reason by loading the SAME .ui file (mainwindow),
 * widgets resize in sync while keeping the svg image centered
 * But without this we would get duplicated toolbar and statusbar
 * */

void MainWindow::removeToolbarAndStatusBar(QWidget *widget)
{
    QToolBar *toolbar = widget->findChild<QToolBar *>("toolBar");
    if (toolbar) {
        toolbar->hide(); // or delete toolbar;
    }
    QStatusBar *statusBar = widget->findChild<QStatusBar *>("statusbar");
    if (statusBar) {
        statusBar->hide(); // or delete statusBar;
    }
}
MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::clickbutton()
{
    qDebug() << "button click";
}
© www.soinside.com 2019 - 2024. All rights reserved.