Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Auto adjust size (width) of QDockWidget

Auto adjust size (width) of QDockWidget

Scheduled Pinned Locked Moved General and Desktop
8 Posts 3 Posters 7.9k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    sjaacko
    wrote on last edited by
    #1

    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?

    1 Reply Last reply
    0
    • AlicemirrorA Offline
      AlicemirrorA Offline
      Alicemirror
      wrote on last edited by
      #2

      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.)

      Enrico Miglino (aka Alicemirror)
      Balearic Dynamics
      Islas Baleares, Ibiza (Spain)
      www.balearicdynamics.com

      1 Reply Last reply
      0
      • A Offline
        A Offline
        andre
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        • AlicemirrorA Offline
          AlicemirrorA Offline
          Alicemirror
          wrote on last edited by
          #4

          @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?

          Enrico Miglino (aka Alicemirror)
          Balearic Dynamics
          Islas Baleares, Ibiza (Spain)
          www.balearicdynamics.com

          1 Reply Last reply
          0
          • S Offline
            S Offline
            sjaacko
            wrote on last edited by
            #5

            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);
            dock->setAllowedAreas(Qt::RightDockWidgetArea);
            dock->setWidget(editWindow);
            addDockWidget(Qt::RightDockWidgetArea, dock);
            viewMenu->addAction(dock->toggleViewAction());
            }@

            1 Reply Last reply
            0
            • AlicemirrorA Offline
              AlicemirrorA Offline
              Alicemirror
              wrote on last edited by
              #6

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

              Enrico Miglino (aka Alicemirror)
              Balearic Dynamics
              Islas Baleares, Ibiza (Spain)
              www.balearicdynamics.com

              1 Reply Last reply
              0
              • S Offline
                S Offline
                sjaacko
                wrote on last edited by
                #7

                OK, tnx.

                1 Reply Last reply
                0
                • S Offline
                  S Offline
                  sjaacko
                  wrote on last edited by
                  #8

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

                  1 Reply Last reply
                  0

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved