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



  • 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()));



  • 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."



  • 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



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

    void MainWindow::addwindow()
    {
    ...
    subWindow->show();
    }
    


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



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



  • @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.



  • 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);
    
    


Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.