Important: Please read the Qt Code of Conduct -

Modifying some features in an application

  • Hi all,

    Here is the program from the book "C++-GUI-Programming-with-Qt-4-2nd Edition".
    I want to make some changes that when I have multiple windows/files opened they are synchronized. Please take a look at this screenshot or better if you read the page 84 of that book.

    There is some code:

      foreach (QWidget* win, QApplication::topLevelWidgets())
            if(MainWindow* mainWin = qobject_cast<MainWindow *>(win))

    We have three functions that should be updated or have the same situation, among all opened files:

    void updateRecentFileActions() in mainwindow.h
    bool autoRecalculate() const in spreadsheet.h
    bool showGrid() in qtableview.h

    I think all of these functions should sit in place of the someFucntion in the above code to achieve the goal. E.g., like this:

      foreach (QWidget* win, QApplication::topLevelWidgets())
            if(MainWindow* mainWin = qobject_cast<MainWindow *>(win)) {

    A question, do you agree until now?

    The problem is that where to put that foreach code/loop?
    My guess is inside the constructor of MainWindow().
    Do you agree, again?

  • No, don't need to do that since you only have to update the recent menu when it appears, that is when the user clicks on the File menu.

    This is what i'm doing in my apps.
    Creating the Open Recent menu:

    action=menu->addAction(tr("Open Recent"));
    action->setMenu(new QMenu(menuBar));

    Updadating the menu:

    void Application::updateRecentMenu()
    	QMenu* menu=qobject_cast<QMenu*>(QObject::sender());
    	if(!menu) return;
    // retrieve the menu list previously recorded
    QStringList files=QSettings().value(Prefs_RecentFiles).value<QStringList>();
    // manage the changes here

    As you can see i manage this in a QApplication subclass, but you can do it in your MainWindow as well.

    [edit] synchronization is an other problem.
    You need a master object (maybe QApplication subclass) that emit a signal when synchronization is needed.
    And each window registers itself to receive that signal.

  • @mpergand

    Well, thank you for the reply. I appreciate it but I can't understand it well. It seems to have a different structure from the book and chapters I've read. It might be right like a different way to solve the problems, but I want to use the book's guiding code, if possible.
    I'm also not that advanced and it may be the reason for not figuring your code out correctly.
    If possible, please read that page of the book and also the code, and guide me through them. Thanks.

  • There is no so advanced code here, it's about signals and slots that are at the heart of Qt paradigm.
    There's no escaping it ...

    Try to learn more about it.

    Looking at your code, seems your updateRecentFileActions() function doesn't change the File menu itself...
    You don't use a sub menu as i do, it's more difficult i think.
    (example of a sub menu)
    alt text

    The key point to understand is the SIGNAL(aboutToShow())
    If you do:


    You can update this menu in your updateRecentFileActions() function.

    It's difficult to be very clear for me, because the way the main menu is managed is very different from the way i do (I don't record actions at all)

Log in to reply