質問

aのサイズ変更に反応するにはどうすればよいですか QMainWindow?私は持っています QTextBrowsersQScrollArea そして、私はそれらを作成する際にコンテンツのサイズにそれらを調整します(スクロールすべき唯一のものは 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を返します。

役に立ちましたか?

解決

テキストまたはHTMLのみがある場合、使用できます QLabel 代わりに、それはすでにそのサイズを利用可能なスペースに適合させているためです。使用する必要があります。

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

aとほぼ同じ動作をすること 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();
    }
};
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top