Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How to add QWidget to QTextEdit?



  • I am using class derived from QTextEdit , I can build standard “window” Qt Area window and add Qwidget to it.
    BUT I end up with two independent windows .

    What I need is Qt Area sub-window containing Qwidget .

    12b1d24a-8bac-4460-a91c-ace5b1757367-image.png

    1. What is the standard window area name I want to write the Qwidget "Form" into ?
    2. I coded QWidget as QTextEdit variable and in constructor and it did not accomplish what I wanted.


  • @AnneRanch
    You cannot add a QWidget to a QTextEdit. A QTextEdit contains text, and that's it, no further widgets.

    What is a "Qt Area window"? If you are still talking about a QMdiArea, and you want to be able to place more than one widget on any of the QMdiSubWindows, you will want to create the child window as a plain QWidget and then add subwidgets onto that. The hierarchy will look like:

    QMdiArea
        QMdiSubWindow
            QWidget
                QVBoxLayout
                    widget1 (e.g. QTextEdit)
                    widget2 (e.g. QLabel)
                    widget3 (e.g. QComboBox) etc.
    

    If your Form is a QWidget and you want that as a subwindow itself then you can use that Form as the QWidget in the above hierarchy.

    You started out from an example which illustrated MDI windows by using a QTextEdit for each subwindow, because it was designed for editing the text of multiple files. It was not a suitable example (in its use of QTextEdits as the subwindows) if not to be used just for that.



  • @JonB said in How to add QWidget to QTextEdit?:

    @AnneRanch
    You cannot add a QWidget to a QTextEdit. A QTextEdit contains text, and that's it, no further widgets.

    What is a "Qt Area window"? If you are still talking about a QMdiArea, and you want to be able to place more than one widget on any of the QMdiSubWindows, you will want to create the child window as a plain QWidget and then add subwidgets onto that. The hierarchy will look like:

    QMdiArea
        QMdiSubWindow
            QWidget
                QVBoxLayout
                    widget1 (e.g. QTextEdit)
                    widget2 (e.g. QLabel)
                    widget3 (e.g. QComboBox) etc.
    

    If your Form is a QWidget and you want that as a subwindow itself then you can use that Form as the QWidget in the above hierarchy.

    You started out from an example which illustrated MDI windows by using a QTextEdit for each subwindow, because it was designed for editing the text of multiple files. It was not a suitable example (in its use of QTextEdits as the subwindows) if not to be used just for that.

    Yes, I am back to the MDI window example and YES I want to modify the hierarchy WITHOUT destroying the current child window class.

    I'll will use your hierarchy and hope it will work.

    I may have said this before - but MDI "example "code flow is not that easy to "decode" .

    The authors "code comments" are pretty skimpy...



  • @AnneRanch said in How to add QWidget to QTextEdit?:

    YES I want to modify the hierarchy WITHOUT destroying the current child window class.

    I know. You can keep the class which is derived from QTextEdit if it does something for you. What you cannot do is make that the direct child widget of the QMdiSubWindow IF you want to put some more widgets on the window. Which I think is what you are wanting to do. If you want multiple widgets on a QMdiSubWindow, you need to make its child a Generic QWidget, add a layout to it, and then add whatever widgets you want there (including your QTextEdit). But as long as you keep the QLineEdit as the QMdiSubWindow's widget you're not set up to then add any further widgets.


Log in to reply