对调整 QMainWindow 的大小做出反应以调整小部件的大小 [英] react on the resizing of a QMainWindow for adjust widget's size

查看:126
本文介绍了对调整 QMainWindow 的大小做出反应以调整小部件的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何对 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,无论实际大小如何.

解决方案

如果只有 text 或 html,你可以使用 QLabel 代替,因为它已经适应了可用空间的大小.你必须使用:

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

具有与 QTextBrowser 几乎相同的行为.

<小时>

如果你真的想使用QTextBrowser,你可以尝试这样的东西(改编自QLabel源代码):

class TextBrowser : public QTextBrowser {Q_OBJECT上市:显式 TextBrowser(QWidget *parent) : QTextBrowser(parent) {//每当大小改变时应调用 updateGeometry//当文档改变时大小也会改变连接(这个,信号(textChanged()),插槽(onTextChanged()));QSizePolicy 策略 = sizePolicy();//足够明显吗?policy.setHeightForWidth(true);setSizePolicy(policy);}int heightForWidth(int width) const {int 左、上、右、下;getContentsMargins(&left, &top, &right, &bottom);QSize边距(左+右,上+下);//因为处理真实文档似乎会导致无限递归,//我们创建一个克隆来计算宽度QScopedPointertempDoc(document()->clone());tempDoc->setTextWidth(width - margins.width());返回 qMax(tempDoc->size().toSize().height() + margins.height(),最小高度());}私人插槽:无效 onTextChanged() {更新几何();}};

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.

解决方案

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

这篇关于对调整 QMainWindow 的大小做出反应以调整小部件的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆