对 QMainWindow 的大小调整做出反应以调整小部件的大小

发布于 2024-12-03 04:37:22 字数 702 浏览 2 评论 0原文

我如何对 QMainWindow 的大小调整做出反应?我在 QScrollArea 中有 QTextBrowsers,我在创建它们时将它们调整为内容的大小(唯一应该滚动的是 QScrollArea )。

现在一切正常,但如果我调整 mainWindow 的大小,QTextBrowsers 的高度不会改变,因为不会触发回流函数。

您是否有更好的想法来调整 QTextBrowser 以适应其内容?我当前的代码是:

void RenderFrame::adjustTextBrowser(QTextBrowser* e) const {
    e->document()->setTextWidth(e->parentWidget()->width());
    e->setMinimumHeight(e->document()->size().toSize().height());
    e->setMaximumHeight(e->minimumHeight());
}

parentWidget() 是必要的,因为在小部件本身上运行 width() 始终返回 100,无论实际大小如何。

How do I react on the resize of a QMainWindow? I have QTextBrowsers in a QScrollArea and I adjust them to the size of the content on creating them (the only thing that should scroll is the QScrollArea).

Everything works for now, but if I resize the mainWindow, the height of the QTextBrowsers isn't changed, because the reflow function isn't triggered.

Do you have any better idea to adjust a QTextBrowser to it's content? My current code is:

void RenderFrame::adjustTextBrowser(QTextBrowser* e) const {
    e->document()->setTextWidth(e->parentWidget()->width());
    e->setMinimumHeight(e->document()->size().toSize().height());
    e->setMaximumHeight(e->minimumHeight());
}

The parentWidget() is necessary because running width() on the widget itself returns always 100, regardless of the real size.

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

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

发布评论

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

评论(1

木有鱼丸 2024-12-10 04:37:22

如果只有文本或 html,您可以使用 QLabel 代替,因为它已经根据可用空间调整其大小。您必须使用:

label->setWordWrap(true);        
label->setTextInteractionFlags(Qt::TextBrowserInteraction); 

才能获得与 QTextBrowser 几乎相同的行为。


如果您确实想使用QTextBrowser,您可以尝试这样的操作(改编自QLabel源代码):

class TextBrowser : public QTextBrowser {
    Q_OBJECT
public:
    explicit TextBrowser(QWidget *parent) : QTextBrowser(parent) {
        // updateGeometry should be called whenever the size changes
        // and the size changes when the document changes        
        connect(this, SIGNAL(textChanged()), SLOT(onTextChanged()));

        QSizePolicy policy = sizePolicy();
        // Obvious enough ? 
        policy.setHeightForWidth(true);
        setSizePolicy(policy);
    }

    int heightForWidth(int width) const {
        int left, top, right, bottom;
        getContentsMargins(&left, &top, &right, &bottom);
        QSize margins(left + right, top + bottom);

        // As working on the real document seems to cause infinite recursion,
        // we create a clone to calculate the width
        QScopedPointer<QTextDocument> tempDoc(document()->clone());
        tempDoc->setTextWidth(width - margins.width());

        return qMax(tempDoc->size().toSize().height() + margins.height(),
                    minimumHeight());
    }
private slots:
    void onTextChanged() {
        updateGeometry();
    }
};

If there is only text or html, you could use QLabel instead, because it already adapts its size to the available space. You'll have to use:

label->setWordWrap(true);        
label->setTextInteractionFlags(Qt::TextBrowserInteraction); 

to have almost the same behavior as a QTextBrowser.


If you really want to use a QTextBrowser, you can try something like this (adapted from QLabel source code):

class TextBrowser : public QTextBrowser {
    Q_OBJECT
public:
    explicit TextBrowser(QWidget *parent) : QTextBrowser(parent) {
        // updateGeometry should be called whenever the size changes
        // and the size changes when the document changes        
        connect(this, SIGNAL(textChanged()), SLOT(onTextChanged()));

        QSizePolicy policy = sizePolicy();
        // Obvious enough ? 
        policy.setHeightForWidth(true);
        setSizePolicy(policy);
    }

    int heightForWidth(int width) const {
        int left, top, right, bottom;
        getContentsMargins(&left, &top, &right, &bottom);
        QSize margins(left + right, top + bottom);

        // As working on the real document seems to cause infinite recursion,
        // we create a clone to calculate the width
        QScopedPointer<QTextDocument> tempDoc(document()->clone());
        tempDoc->setTextWidth(width - margins.width());

        return qMax(tempDoc->size().toSize().height() + margins.height(),
                    minimumHeight());
    }
private slots:
    void onTextChanged() {
        updateGeometry();
    }
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文