Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. How to rewrite MDI example structure C++ code

How to rewrite MDI example structure C++ code

Scheduled Pinned Locked Moved Unsolved C++ Gurus
crea
9 Posts 4 Posters 861 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.
  • A Offline
    A Offline
    Anonymous_Banned275
    wrote on 1 Apr 2021, 00:14 last edited by
    #1

    I am still not sure how to modify my Qt MDI example.
    I am using it because it does what I am after utilizing QMainWindow..

    I am not interested in changing the horse in middle of the race.

    Here is rough description of the process / structure , somewhat in "top to bottom way".

    create central MDI widget - mdiArea
    create sub windows list QMdiSubWindow
    create sun window contents - MDIChild derived from test editor
    insert MDIChild to sub window list

    Now the MDI example works fine as is - happily processing
    mdiArea -> subwindow -> child's file

    The "problem" is I do not need to manipulate "file".
    Unfortunately "file" (text name ) is used to do neat stuff likes tiling multiple subwindows in areaMDI.

    So I have MDIChild QTextEdit derived class and I am stuck on HOW to
    add my C++ code creating form / widget.

    QTextEdit base class is QWidget - how do I physically "append" my
    C++ code creating the form ?

    I was thinking of multiple inheritance , but why when QTextEdit is already derived from QWidget?
    here is stripped down , minimal comments , of createMDIChild

    MdiChild *MainWindow::createMdiChild()
    {
    // change MdiChild from text edit to widget ??
    // MDIChild is derived from QTextEdit and QWidget
    MdiChild *child = new MdiChild;
    QMdiSubWindow *currentSubWindow = mdiArea->addSubWindow(child);
    #ifndef QT_NO_CLIPBOARD
    connect(child, &QTextEdit::copyAvailable, cutAct, &QAction::setEnabled);
    connect(child, &QTextEdit::copyAvailable, copyAct, &QAction::setEnabled);
    #endif
    return child;
    }

    In  other words - how do I "add ' my C++  form creation  code to "MDIChild" 
    and keep original QTextEdit  variables - such as "file name"  for "tile" processing ?  Another "subclass" just for widget creation?
    J 1 Reply Last reply 1 Apr 2021, 08:26
    0
    • A Anonymous_Banned275
      1 Apr 2021, 00:14

      I am still not sure how to modify my Qt MDI example.
      I am using it because it does what I am after utilizing QMainWindow..

      I am not interested in changing the horse in middle of the race.

      Here is rough description of the process / structure , somewhat in "top to bottom way".

      create central MDI widget - mdiArea
      create sub windows list QMdiSubWindow
      create sun window contents - MDIChild derived from test editor
      insert MDIChild to sub window list

      Now the MDI example works fine as is - happily processing
      mdiArea -> subwindow -> child's file

      The "problem" is I do not need to manipulate "file".
      Unfortunately "file" (text name ) is used to do neat stuff likes tiling multiple subwindows in areaMDI.

      So I have MDIChild QTextEdit derived class and I am stuck on HOW to
      add my C++ code creating form / widget.

      QTextEdit base class is QWidget - how do I physically "append" my
      C++ code creating the form ?

      I was thinking of multiple inheritance , but why when QTextEdit is already derived from QWidget?
      here is stripped down , minimal comments , of createMDIChild

      MdiChild *MainWindow::createMdiChild()
      {
      // change MdiChild from text edit to widget ??
      // MDIChild is derived from QTextEdit and QWidget
      MdiChild *child = new MdiChild;
      QMdiSubWindow *currentSubWindow = mdiArea->addSubWindow(child);
      #ifndef QT_NO_CLIPBOARD
      connect(child, &QTextEdit::copyAvailable, cutAct, &QAction::setEnabled);
      connect(child, &QTextEdit::copyAvailable, copyAct, &QAction::setEnabled);
      #endif
      return child;
      }

      In  other words - how do I "add ' my C++  form creation  code to "MDIChild" 
      and keep original QTextEdit  variables - such as "file name"  for "tile" processing ?  Another "subclass" just for widget creation?
      J Online
      J Online
      JonB
      wrote on 1 Apr 2021, 08:26 last edited by JonB 4 Jan 2021, 08:47
      #2

      @AnneRanch
      I'm sorry I don't understand much of the question. But there should be no need for multiple inheritance, or indeed inheritance anything. I don't know why you have any special class MdiChild.

      Normally I would just do all MDI children as

      QTextEdit *textEdit = new QTextEdit;
      QWidget *mdiChild = new QWidget;
      mdiChild->setLayout(new QVBoxLayout);
      mdiChild->layout()->addWidget(textEdit);
      // mdiChild->layout()->addWidget(new QLabel("Type into the TextArea above"));
      // mdiChild->layout()->addWidget(new QPushButton("Click this"));
      QMdiSubWindow *currentSubWindow = mdiArea->addSubWindow(mdiChild);
      

      That creates a QWidget, which is what I add as an MDI subwindow. On that I have put layout and then you can add whatever of your widgets.

      If you want to skip the QWidget layer, you could just go:

      QTextEdit *textEdit = new QTextEdit;
      QMdiSubWindow *currentSubWindow = mdiArea->addSubWindow(textEdit);
      

      That's probably fine with a QTextEdit, which can grow to be big & multiline and fill the window, but would look a little odd if the widget is just some tiny QPushButton or similar. And without a containing widget as per the first example you won't be able to add anything but a single widget to the subwindow, which is why the first example is probably the way to go.

      If you have designed your own Form, perhaps in Qt Designer, with its own layout and sub-widgets on it, you can use that as the widget for the MDI child window:

      Form *myForm = new Form;
      QMdiSubWindow *currentSubWindow = mdiArea->addSubWindow(myForm);
      
      J 1 Reply Last reply 1 Apr 2021, 09:17
      2
      • J JonB
        1 Apr 2021, 08:26

        @AnneRanch
        I'm sorry I don't understand much of the question. But there should be no need for multiple inheritance, or indeed inheritance anything. I don't know why you have any special class MdiChild.

        Normally I would just do all MDI children as

        QTextEdit *textEdit = new QTextEdit;
        QWidget *mdiChild = new QWidget;
        mdiChild->setLayout(new QVBoxLayout);
        mdiChild->layout()->addWidget(textEdit);
        // mdiChild->layout()->addWidget(new QLabel("Type into the TextArea above"));
        // mdiChild->layout()->addWidget(new QPushButton("Click this"));
        QMdiSubWindow *currentSubWindow = mdiArea->addSubWindow(mdiChild);
        

        That creates a QWidget, which is what I add as an MDI subwindow. On that I have put layout and then you can add whatever of your widgets.

        If you want to skip the QWidget layer, you could just go:

        QTextEdit *textEdit = new QTextEdit;
        QMdiSubWindow *currentSubWindow = mdiArea->addSubWindow(textEdit);
        

        That's probably fine with a QTextEdit, which can grow to be big & multiline and fill the window, but would look a little odd if the widget is just some tiny QPushButton or similar. And without a containing widget as per the first example you won't be able to add anything but a single widget to the subwindow, which is why the first example is probably the way to go.

        If you have designed your own Form, perhaps in Qt Designer, with its own layout and sub-widgets on it, you can use that as the widget for the MDI child window:

        Form *myForm = new Form;
        QMdiSubWindow *currentSubWindow = mdiArea->addSubWindow(myForm);
        
        J Offline
        J Offline
        JKSH
        Moderators
        wrote on 1 Apr 2021, 09:17 last edited by
        #3

        @JonB said in How to rewrite MDI example structure C++ code:

        there should be no need for multiple inheritance, or indeed inheritance anything. I don't know why you have any special class MdiChild.

        I agree that multiple inheritance is not the way.

        Regarding MdiChild, it's just how one of the old examples did it: https://code.qt.io/cgit/qt/qtbase.git/tree/examples/widgets/mainwindows/mdi/mdichild.h While I wouldn't inherit QTextEdit personally, this example's MdiChild has the same role as the Form at the end of your post.

        Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

        1 Reply Last reply
        1
        • A Offline
          A Offline
          Anonymous_Banned275
          wrote on 1 Apr 2021, 15:10 last edited by
          #4

          Thanks for replies.
          You are correct the MDIChild just complicates things - but when one starts by hacking / using Qt examples there is not much choice while using it as a base. Wright or (old) wrong.

          I have made some progress since I started , most parts work.

          I am pretty much stuck on implementing Window "Tile" - it requires "file name" as subwindow title etc...
          In my view I am hacking the MDI example going "backward" since my widget is not a file with a name .
          But I can obviously add fake file name to make MDIChild happy.

          However ,for now et me work on elimination the MDIChild from the MDI example.

          1 Reply Last reply
          0
          • A Offline
            A Offline
            Anonymous_Banned275
            wrote on 1 Apr 2021, 17:02 last edited by
            #5

            I am posting my current under construction code to illustrate "the problem".

            The example MDIChild uses real file - with name.
            This part of the code adds such files to MDI example "Window" menu.
            Works fine.

            Now when I build my widget it is NOT a file - hence no name.

            Missing name breaks this "Window" menu code big time - obviously.

            I was thinking to add "fake file" to my widget so I can reuse this part of "Window" menu code.

            Not the best , but does that approach makes some sense?
            I am still trying to reuse as much of the original MDI example code.

            Again to clarify - I do not have much issues changing the "QTextEdit" to QWidget - the "file name " is the issue as far as I can tell.

            // initially create empty QList<QMdiSubWindow *> windows
                void MainWindow::updateWindowMenu()
                {
                    qDebug() << "START void MainWindow::updateWindowMenu()" <<__FUNCTION__<<
                                " @line " <<__LINE__;
                    //  windowMenu initilized (?)
                    qDebug() << "TRACE access pn initialization " <<__FUNCTION__<<
                                " @line " <<__LINE__;
                    windowMenu->clear();
                    windowMenu->addAction(closeAct);
                    windowMenu->addAction(closeAllAct);
                    windowMenu->addSeparator();
                    windowMenu->addSeparator();
                    windowMenu->addSeparator();
                    windowMenu->addAction(tileAct);
                    windowMenu->addAction(cascadeAct);
                    windowMenu->addSeparator();
                    windowMenu->addAction(nextAct);
                    windowMenu->addAction(previousAct);
                    windowMenu->addAction(windowMenuSeparatorAct);
            
                    qDebug() << "TRACE get list of subwindows in mdiArea" <<__FUNCTION__<<
                                " @line " <<__LINE__;
                    qDebug() << "TRACE breakpoint " <<__FUNCTION__<<
                                " @line " <<__LINE__;
                    QList<QMdiSubWindow *> windows = mdiArea->subWindowList();
                    qDebug() << "TRACE breakpoint " <<__FUNCTION__<<
                                " @line " <<__LINE__;
            
                    windowMenuSeparatorAct->setVisible(!windows.isEmpty());
                    qDebug() << "TRACE breakpoint " <<__FUNCTION__<<
                                " @line " <<__LINE__;
                    // check initilal windows size
                    qDebug() << "TRACE check subwindows size / count  " <<windows.size(); // __FUNCTION__<<
                    //               " @line " <<__LINE__;
                    for (int i = 0; i < windows.size(); ++i) {
                        // mdi area subwindow  from list
                        QMdiSubWindow *mdiSubWindow = windows.at(i);
                        //   mdiSubWindow
                        //  retive contents of subwindow as child
                        MdiChild *child = qobject_cast<MdiChild *>(mdiSubWindow->widget());
            
                        //  scan for widget type so far only a label
                        // child->MDIChildType
                        //HERE
            
            
                        // temporary bypass  no file
                        if(  child->MDIChildType == 0)
                            //if(!child->userFriendlyCurrentFile().isEmpty())
                        { // add file info to menu
                            // check initilal windows size
                            qDebug() << "TRACE *****************  process CTextEdit child "<< __FUNCTION__<<__LINE__;
            
                            QString text;
                            if (i < 9) {
                                text = tr("&%1 %2").arg(i + 1)
                                        .arg(child->userFriendlyCurrentFile());
                            } else {
                                text = tr("%1 %2").arg(i + 1)
                                        .arg(child->userFriendlyCurrentFile());
                            }
                            // show files in "windows" menu (?)
                            // create "window" menu action / items
                            QAction *action = windowMenu->addAction(text,
                                                                    mdiSubWindow, [this, mdiSubWindow]()
                            {
                                // why block (?)
                                mdiArea->setActiveSubWindow(mdiSubWindow);
                            }
                            );
                            action->setCheckable(true);
                            action->setChecked(child == activeMdiChild());
                        }
                        else
                        {
                            qDebug() << "TRACE %%%%%%%%%%%%%%%%%%%%%%%%%%*****************  process CTextEdit child "<< __FUNCTION__<<__LINE__;
            
                            // try setting FDB to stop[ here on warning
                            mdiSubWindow->widget()->show();
                        }
                    }
                    qDebug() << "TRACE breakpoint " <<__FUNCTION__<<
                                " @line " <<__LINE__;
                }
            
            1 Reply Last reply
            0
            • A Offline
              A Offline
              Anonymous_Banned275
              wrote on 2 Apr 2021, 03:43 last edited by Anonymous_Banned275 4 Mar 2021, 03:13
              #6

              SOLVED with the help of this forum.
              It was just a matter of putting the code in relevant places.

              Adding widgets to MdiChild
              PARTIALLY SOLVED

              1 Reply Last reply
              0
              • S Offline
                S Offline
                SGaist
                Lifetime Qt Champion
                wrote on 2 Apr 2021, 19:50 last edited by
                #7

                Good, then please mark the thread as solved using the "Topic Tools" button or the three doted menu beside the answer you deem correct so that other forum users may know a solution has been found :-)

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                A 1 Reply Last reply 3 Apr 2021, 03:12
                0
                • S SGaist
                  2 Apr 2021, 19:50

                  Good, then please mark the thread as solved using the "Topic Tools" button or the three doted menu beside the answer you deem correct so that other forum users may know a solution has been found :-)

                  A Offline
                  A Offline
                  Anonymous_Banned275
                  wrote on 3 Apr 2021, 03:12 last edited by
                  #8
                  This post is deleted!
                  J 1 Reply Last reply 3 Apr 2021, 08:11
                  0
                  • A Anonymous_Banned275
                    3 Apr 2021, 03:12

                    This post is deleted!

                    J Online
                    J Online
                    JonB
                    wrote on 3 Apr 2021, 08:11 last edited by
                    #9

                    @AnneRanch said in How to rewrite MDI example structure C++ code:

                    I now have two processes running and the "myProcess" dialog works fine - what I wnat is to insert the dialog from one process to another .

                    You have asked this previously, and the answer remains the same. So long as you have "separate processes" --- something run via QProcess --- you cannot "insert the dialog from one process to another ." MDI or not makes no difference. If you wish to have a dialog from one set of code appear in another set of code, they must both be in the same application, no independent processes.

                    On a separate matter, if you do wish to use QProcess you still presently have:

                    myProcess->start();
                    int result_execute  = myProcess->execute(program); //, arguments);
                    

                    We have said before that this will run two instances of the btscanner program, and so you should find there are two btscanner windows opened, which is not right.

                    1 Reply Last reply
                    2

                    7/9

                    2 Apr 2021, 19:50

                    • Login

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