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. [SOLVED] Function works, can't add it to slot
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] Function works, can't add it to slot

Scheduled Pinned Locked Moved General and Desktop
slotsqt5c++helpqt application
8 Posts 2 Posters 2.9k Views 1 Watching
  • 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
    Adept
    wrote on last edited by Adept
    #1

    I have a function that gets the current QStackedWidget as a QMdiArea and adds a subwindow to it (this function works), BUT when I add it to a connect() / SLOT() it does nothing.

    Here is the function to get the current QStackedWidget as a QMdiArea and add a subwindow to it:

    void MainWindow::addWindow()
    {

    QMdiArea *a = (QMdiArea *) layout->currentWidget();
    a->addSubWindow(new QMdiSubWindow);
    

    }

    So again this function works if I call it like this:
    addWindow()
    BUT if I put it in a connect it does NOT work:
    connect(addWindow, SIGNAL(triggered()), this, SLOT(addwindow()));

    1 Reply Last reply
    0
    • mistralegnaM Offline
      mistralegnaM Offline
      mistralegna
      wrote on last edited by
      #2

      Have you declared the macro Q_OBJECT in your MainWindow implementation ? Have you declared the addWidow method of your MainWindow in the slots section ?

      class MainWindow : public QMainWindow
      {
      Q_OBJECT
      ...
      public slots:
      void addWindow();
      }
      

      By the way, when you are instantiating yourself the QMdiSubwindow, "you must set the Qt::WA_DeleteOnClose widget attribute if you want the window to be deleted when closed in the MDI area. If not, the window will be hidden and the MDI area will not activate the next subwindow."

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

        Hey thanks @macgab for the suggestions, unfortunately both of them had been done. Here is the full code to the app.

        main.cpp
        http://pastebin.com/QeZ90LCS

        mainwindow.cpp
        http://pastebin.com/LBzMYKRV

        mainwindow.h
        http://pastebin.com/e9HEEY2s

        1 Reply Last reply
        0
        • mistralegnaM Offline
          mistralegnaM Offline
          mistralegna
          wrote on last edited by mistralegna
          #4

          It seems your QMdiSubWindow keeps hidden, try this in the addwindow method:

          void MainWindow::addwindow()
          {
          ...
          subWindow->show();
          }
          
          A 2 Replies Last reply
          0
          • mistralegnaM mistralegna

            It seems your QMdiSubWindow keeps hidden, try this in the addwindow method:

            void MainWindow::addwindow()
            {
            ...
            subWindow->show();
            }
            
            A Offline
            A Offline
            Adept
            wrote on last edited by
            #5

            @macgab ahhh, thank you so much!!! I appreciate you taking your time to help :)

            1 Reply Last reply
            0
            • mistralegnaM Offline
              mistralegnaM Offline
              mistralegna
              wrote on last edited by
              #6

              You're welcome ! Please don't forget to mark the topic as solved ;-)

              1 Reply Last reply
              0
              • mistralegnaM mistralegna

                It seems your QMdiSubWindow keeps hidden, try this in the addwindow method:

                void MainWindow::addwindow()
                {
                ...
                subWindow->show();
                }
                
                A Offline
                A Offline
                Adept
                wrote on last edited by
                #7

                @macgab one quick question though:
                when I do this:
                QMdiArea *mdi = (MdiArea *) getSelectedTabAsWidget();
                how do I assert() that the getSelectedTabAsWidget() is a QMdiArea? If I run it and it's not a QMdiArea, it will crash, so I need to assert() it.

                1 Reply Last reply
                0
                • mistralegnaM Offline
                  mistralegnaM Offline
                  mistralegna
                  wrote on last edited by
                  #8

                  You can first retrieve the getSelectedTabAsWidget() as a QWidget, check that it's not null, and if qobject_cast<QMdiArea*>() doesn't return NULL.

                  Snippet 1:

                  QWidget* selectedWidget = getSelectedTabAsWidget();
                  if ( selectedWidget == NULL ) {
                  return;
                  }
                  
                  QMdiArea* mdiArea = qobject_cast<QMdiArea*>(selectedWidget);
                  
                  if ( mdiArea == NULL ) {
                  return;
                  }
                  // Here, we know that mdiArea is indeed a QMdiArea.
                  

                  Snippet 2:

                  QWidget* selectedWidget = getSelectedTabAsWidget();
                  if ( selectedWidget == NULL ) {
                  return;
                  }
                  
                  if ( ! selectedWidget->inherits(QMdiArea::staticMetaObject.classname())
                  return;
                  
                  // Here, we know that mdiArea is indeed a QMdiArea.
                  
                  QMdiArea* mdiArea = qobject_cast<QMdiArea*>(selectedWidget);
                  
                  
                  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