Qt World Summit: Submit your Presentation

expanding QPlainTextEdit boxes in QTabWidgets

  • Hi all -

    I'm starting to clean up the UI of an app I've been using for awhile. Part of my goal is to eliminate as many magic numbers as possible within the code for the UI.

    I have a QTabWidget with 4 tabs:
    I'd like the QPlainTextEdit objects to fill the available space, but I can't figure out how. Setting their properties to expanding doesn't do it. Can someone tell me what I'm missing?


  • Lifetime Qt Champion


    Why not make the QPlainTextEdit directly the content of the tab ?

  • Hi SGaist -

    From the docs:

    The normal way to use QTabWidget is to do the following:
    Create a QTabWidget.
    Create a QWidget for each of the pages in the tab dialog, but do not specify parent widgets for them.
    Insert child widgets into the page widget, using layouts to position them as normal.
    Call addTab() or insertTab() to put the page widgets into the tab widget, giving each tab a suitable label with an optional keyboard shortcut.

    I understood this to mean I had to do it the way I described above. Am I misinterpreting that?

  • Lifetime Qt Champion

    That's valid and written so because most of the time, you'll have complex widgets there. Since you are using only one widget, there's nothing wrong at adding it directly to the QTabWidget.

    AFAICT, in your example, you are not using layouts, that's one thing you should do.

  • OK, I don't know how to do this, at least not in Designer. Are you suggesting that I have a QTabWidget, and add 4 QPlainTextEdits to it? I can't seem to avoid getting that intermediate (layout) QWidget inserted, nor do I see how to delete them.

  • Lifetime Qt Champion

    I don't know either, I usually use Qt directly when dealing with that kind of stuff.

    [edit: reworded to be clearer SGaist]

  • So...

    1. create the QTabWidget
    2. create the pages (as QPlainTextEdit objects?) as necessary
    3. setSizePolicy on the pages to expanding, expanding
      4 call addTab() for each page

    Is that about right?

  • Lifetime Qt Champion

    That's the thing, you shouldn't need to do anything special. If you call addTab directly with QPlainTextEdit, it should fill the available space.
    In your case with designer, you have to put a layout on the widget that is inside the tab and then the layout the QPlainTextEdit in it.

  • @SGaist very slick.

    enum LOG_TABS
    const QString logtabText[NBR_LOGTABS] =
        "Heartbeat Log",
        "Message Log",
        "Button Log",
        "AP Scan Log"
    QPlainTextEdit m_logTabs[NBR_LOGTABS];
    for (int i = 0; i < NBR_LOGTABS; ++i)
        ui->tabWidget->addTab(&(m_logTabs[i]), logtabText[i]);

    (I know I probably should have used QVectors instead of a C array, but hey, I'm mostly a firmware programmer.)

    I'm curious: what controls the height of the QTabWidget now? Is it the geometry/size policy values of the QTabWidget, or is it the content of the QPlainTextEdits of the tabs? I ask because this implementation causes my QTableView (in another area of the display) to default to a slightly smaller value, and I don't understand why.


  • Lifetime Qt Champion

    Usually, it's the bigger widget added that sets the overall size of the QTabWidget.