QGridlayout 改变行的高度

发布于 2024-09-26 00:08:30 字数 1939 浏览 0 评论 0原文

我的 QGridLayout 有问题。我的布局的一行包含一个通常隐藏的元素(QProgressbar)。当有一些进展需要报告时,我会打电话进行展示。问题是,当我在 QProgressbar 上调用 show 时,包含它的行上方的行的高度将略有调整(1-3 px)。所以整个布局有点“跳跃”,看起来很难看。

我已经给包含 QProgressbar 的行指定了minimalRowHeight,该行比 QProgressbar 的高度大得多,但行的高度仍然会在 show() 上增加。

我附上了一个演示该问题的程序的最小版本。谁能告诉我那里发生了什么?

标题:

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QtGui/QMainWindow>
#include <QLineEdit>
#include <QtWebKit/QWebView>

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = 0);

private:
    QLineEdit* input;
    QWebView *webview;

private slots:
    void slotLoadButton();
};

#endif // MAINWINDOW_H

来源: #include“mainwindow.h”

#include <QProgressBar>
#include <QPushButton>
#include <QGridLayout>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    QGridLayout *grid = new QGridLayout;

    input = new QLineEdit;

    QPushButton *loadButton = new QPushButton("load");
    connect(loadButton, SIGNAL(clicked()),
            this, SLOT(slotLoadButton()));

    webview = new QWebView;
    QProgressBar *progress = new QProgressBar;
    progress->setFixedHeight(25);
    progress->hide();

    connect(webview, SIGNAL(loadStarted()),
            progress, SLOT(show()));

    connect(webview, SIGNAL(loadProgress(int)),
            progress, SLOT(setValue(int)));

    connect(webview, SIGNAL(loadFinished(bool)),
            progress, SLOT(hide()));

    grid->addWidget(input, 0, 0);
    grid->addWidget(loadButton, 0, 1);
    grid->addWidget(webview, 1, 0, 1, -1);
    grid->setRowMinimumHeight(2, 35);
    grid->addWidget(progress, 2, 1);

    QWidget* widget = new QWidget;
    widget->setLayout(grid);
    setCentralWidget(widget);
}

void MainWindow::slotLoadButton()
{
    QUrl url = input->text();
    webview->load(url);
}

I have a problem with a QGridLayout. One row of my layout contains an element (QProgressbar) that is normaly hidden. When there is some progress to report i call show on it. The problem is that when i call show on the QProgressbar the row above the row containing it will be slightly resized in height (1-3 px). So the whole layout does a little "jump" which looks ugly.

I have given a minimalRowHeight to the row that contains the QProgressbar that is much larger then the height of the QProgressbar but still the height of the row will increase on show().

I have attached a very minimal version of my program that demonstrates the problem. Can anyone give me a hint what is going on there?

Header:

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QtGui/QMainWindow>
#include <QLineEdit>
#include <QtWebKit/QWebView>

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = 0);

private:
    QLineEdit* input;
    QWebView *webview;

private slots:
    void slotLoadButton();
};

#endif // MAINWINDOW_H

Source:
#include "mainwindow.h"

#include <QProgressBar>
#include <QPushButton>
#include <QGridLayout>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    QGridLayout *grid = new QGridLayout;

    input = new QLineEdit;

    QPushButton *loadButton = new QPushButton("load");
    connect(loadButton, SIGNAL(clicked()),
            this, SLOT(slotLoadButton()));

    webview = new QWebView;
    QProgressBar *progress = new QProgressBar;
    progress->setFixedHeight(25);
    progress->hide();

    connect(webview, SIGNAL(loadStarted()),
            progress, SLOT(show()));

    connect(webview, SIGNAL(loadProgress(int)),
            progress, SLOT(setValue(int)));

    connect(webview, SIGNAL(loadFinished(bool)),
            progress, SLOT(hide()));

    grid->addWidget(input, 0, 0);
    grid->addWidget(loadButton, 0, 1);
    grid->addWidget(webview, 1, 0, 1, -1);
    grid->setRowMinimumHeight(2, 35);
    grid->addWidget(progress, 2, 1);

    QWidget* widget = new QWidget;
    widget->setLayout(grid);
    setCentralWidget(widget);
}

void MainWindow::slotLoadButton()
{
    QUrl url = input->text();
    webview->load(url);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

落花浅忆 2024-10-03 00:08:30

这可能是由布局的垂直间距和/或边距引起的。您应该尝试使用这些属性。

This is likely caused by the vertical spacing and/or margins of the layout. You should try playing with those properties.

趁微风不噪 2024-10-03 00:08:30

这看起来像是 Qt 中的一个错误。尝试报告

这是一种解决方法:

//grid->addWidget(progress, 2, 1);
QHBoxLayout *l = new QHBoxLayout;
l->addWidget(progress);
QWidget *w = new QWidget;
w->setLayout(l);
grid->addWidget(w, 2, 1);

This looks like a bug in Qt. Try reporting it

This is a workaround:

//grid->addWidget(progress, 2, 1);
QHBoxLayout *l = new QHBoxLayout;
l->addWidget(progress);
QWidget *w = new QWidget;
w->setLayout(l);
grid->addWidget(w, 2, 1);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文