Open Window from Sibling Window



  • Hi. Apologies for the somewhat inaccurate wording in the thread title; having some difficulty describing what I need in a sentence.

    I have a program structure like this:

    main (.cpp)
    -> Application Manager Class (.h and .cpp)
    ----> Main Window (.ui, .h and .cpp)
    ----> Settings Manager (.h and .cpp)
    ---------> Settings Window (.ui, .h and .cpp)

    Now, this is a simplification; the managers are there as there is a lot of behind the scenes going on; file i/o etc.
    What I need, is for a button on the Main Window to be able to open the settings window.

    What would be the best way of achieving this?

    Many Thanks in advance,
    Cosford.



  • Hi,
    assuming SettingsWindow is QDialog descentant I would connect button's clicked signal with method that does something like:

    SettingsWindow->exec();
    

    Please read QDialog class documentation, there are also hints how to determine which button was used to close the dialog (through signal/slot mechanism).

    Kind Regards,
    Artur



  • Hi, thanks for your reply.

    SettingsWindow is indeed derived from QDialog.

    I assume I should make the slot/signal connection from MainWindow (where said button is a child of), although I'm unsure how to do this as SettingsWindow is an object held by the SettingsManager object, which along with MainWindow are both objects held by the ApplicationManager. Therefore, I don't believe that MainWindow can see the SettingsWindow, nor the SettingsManager object.

    I hope I've made myself clear enough?

    Regards,
    Cosford.



  • @Cosford Yes, I got it. I will look at it later this evening, since I have to go offline for couple of hours.



  • @Cosford
    Hi, I apologize for late reply.
    I have read the documentation on session management and I would solve your problem by using applicationStateChanged signals to notify session manager that there are new data available for other applications in session and then other signal to MainWindow to let it know of the changes.
    Documentation suggests use of QSettings to store the data.

    Maybe there is someone with more experience in working with sessions that may have better solution but this is how would I try to make it work.



  • Hi, thanks for the reply once again.

    I've had a look through the session manager documentation, but I'm not sure that will really be the sort of solution I'm looking for. All I believe I need to do is somehow expose the button signal from the main window UI object to the Application Manager which holds the MainWindow object, such that the Application Manager can connect that signal to the open slot on the SettingsWindow (if one exists).

    Regards,
    Cosford.



  • @Cosford As far as I understand you can do something like:
    button's signal --> triggers mainwindow's signal --> which is received by the app manager and triggers signal --> received by settings window

    At least that is what I understood from docs.

    I am sorry I cannot help more or test that solution for you but got urgent project on desktop right now.



  • Hi, thanks again.

    Trying to implement as you suggested. Slapping myself for not thinking of this before.

    I created in MainWindow a slot to receive the buttonPressed signal, which then emits a signal from the MainWindow. I'm attempting to connect this signal to the settingswindow show() slot.

    However, I'm running into a compilation issue;

    "C:\Users\Admin\Dropbox\QT\testProj\untitled\applicationmanager.cpp:10: error: C2665: 'QObject::connect' : none of the 3 overloads could convert all the argument types"

    The line of code causing this error is:
    QObject::connect(windowMain, SIGNAL(settingsButtonPressed()), managerSettings.windowSettings, SLOT(showWindow()));

    I'm somewhat confused on this one. It seems that the connect() function isn't recognising the SIGNAL(settingsButtonPressed()) argument. At least, I'm assuming that is the argument it's having an issue with as it doesn't show up in QtCreator's suggestions as I type it.

    The Signals and slots are defined correctly and work from within their own respective classes. It's just getting the ApplicationManager to see those signals/slots.



  • @Cosford
    In docs (QSessionManager::allowsInteraction) there is example:

    MyMainWidget::MyMainWidget(QWidget *parent)
        :QWidget(parent)
    {
        connect(qApp, SIGNAL(commitDataRequest(QSessionManager)), SLOT(commitData(QSessionManager)));
    }
    

    but i don't know if that approach will solve your problem.
    In other place (QApplication docs) may be a direct solution to what you've got:

    void QApplication::commitDataRequest ( QSessionManager & manager ) [signal]
    This signal deals with session management. It is emitted when the QSessionManager wants the application to commit all its data.

    Usually this means saving all open files, after getting permission from the user. Furthermore you may want to provide a means by which the user can cancel the shutdown.

    You should not exit the application within this signal. Instead, the session manager may or may not do this afterwards, depending on the context.

    Warning: Within this signal, no user interaction is possible, unless you ask the manager for explicit permission. See QSessionManager::allowsInteraction() and QSessionManager::allowsErrorInteraction() for details and example usage.

    Note: You should use Qt::DirectConnection when connecting to this signal.

    This function was introduced in Qt 4.2.

    See also isSessionRestored(), sessionId(), saveState(), and Session Management.

    So, you shouldn't use QObject's connect but QApplication's (or QGuiApplication's) directConnect method.

    Hope that helps a bit.



  • Hi, thanks once again.

    I've now solved my issue. I achieved this by doing the following:

    MainWindow:
    -> Created Slot to receive button clicked signal and emit a signal.
    -> Connected button click signal to above slot.

    ApplicationManager:
    -> Connected signal from MainWindow to SettingsWindow show slot.

    Thanks for all your help and suggestions.

    Regards,
    Cosford.



  • @Cosford
    I am happy to hear you made it.

    If I helped in any way - my pleasure.


Log in to reply
 

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