Qt World Summit: Submit your Presentation

Auto adjust size (width) of QDockWidget

  • My program has a QDockWidget with a QTextEdit. Actions in the program will add lines to the textfile loaded there. The user can also edit the text.
    What I would like is that the width of window adjusts to longest line of text. The window is set to not wrap the text.
    I found many questions and answers but none of them seem to do anything.
    I tried this like sizeHint() and QWidget::resizeEvent() but I have no clue where to put the code.
    Any thoughts?

  • Hi,

    I think that you should manage the windows size bounding the width of it on the paintedWidth pixel sizes of the text. I am using this method - fast and reliable - for the text height in text objects.

    Note that I think that you should add a correction factor (i.e. fixed percentage of the value) if the size does not match exactly to your desired size (e.g. margins extra pixel size etc.)

  • The resizing methods you tried before do not work, because QTextEdit is a QScrollArea-decendant class. That class (by design) decouples the size of its content from the size of its viewport.

    What you could do, is try to monitor the width of the contents, and resize the dockwidget based on that. You could try two angles for this: you could try to keep tabs on the range of the horizontal scroll bar of the QTextEdit. It has a rangeChanged() signal for that you could connect to. Another approach would be to install an eventfilter on the viewport widget of QAbstractScrollArea (the base of your QTextEdit). I would try the first approach first.

  • @Andre: correct, I see. But @sjaacko says that are all single non wrapped lines. It seems as a set of text elements, or means that are single-lines in the single editable area?

  • OK, that makes sense. Still, I have no idea what code to write or where to put it.
    And the content of the window is a simple text file with more lines of text.
    I just started working on this project which was created by someone else (who does not know either ;-)

    We use this code to create the window:

    @void Gui::createDockWindows()
    QDockWidget *dock = new QDockWidget(tr("File"), this);
    addDockWidget(Qt::RightDockWidgetArea, dock);

  • hi, in this moment I am going to airport... If no one helps you this evening i search for a decent snippet. :)

  • OK, tnx.

  • So, the theory is this:
    As long as there is a horizontal scrollbar increase the width of dockwidget.
    Now how to implement it?

Log in to reply