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. Modifying some features in an application
Forum Updated to NodeBB v4.3 + New Features

Modifying some features in an application

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 2 Posters 865 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.
  • tomyT Offline
    tomyT Offline
    tomy
    wrote on last edited by
    #1

    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))
                mainWin->someFunction();
    

    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)) {
                mainWin->updateRecentFileActions();
                mainWin->autoRecalculate();
                mainWin->showGrid();
    }
    

    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?

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mpergand
      wrote on last edited by mpergand
      #2

      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));
      action->menu()->setObjectName("recent");
      connect(action->menu(),SIGNAL(aboutToShow()),this,SLOT(updateRecentMenu()));
      

      Updadating the menu:

      void Application::updateRecentMenu()
      { 
      	QMenu* menu=qobject_cast<QMenu*>(QObject::sender());
      	if(!menu) return;
      
      	menu->clear();
      	
      // 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.

      tomyT 1 Reply Last reply
      1
      • M mpergand

        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));
        action->menu()->setObjectName("recent");
        connect(action->menu(),SIGNAL(aboutToShow()),this,SLOT(updateRecentMenu()));
        

        Updadating the menu:

        void Application::updateRecentMenu()
        { 
        	QMenu* menu=qobject_cast<QMenu*>(QObject::sender());
        	if(!menu) return;
        
        	menu->clear();
        	
        // 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.

        tomyT Offline
        tomyT Offline
        tomy
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0
        • M Offline
          M Offline
          mpergand
          wrote on last edited by mpergand
          #4

          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.

          [EDIT]
          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:

          connect(action->menu(),SIGNAL(aboutToShow()),this,SLOT(updateRecentFileActions()));
          

          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)

          1 Reply Last reply
          1

          • Login

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