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. Creating QActions on the fly for history
QtWS25 Last Chance

Creating QActions on the fly for history

Scheduled Pinned Locked Moved General and Desktop
21 Posts 3 Posters 5.6k Views
  • 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.
  • N Offline
    N Offline
    nicky j
    wrote on last edited by
    #5

    I don't have a file, how do I put the sender() into the slot?

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #6

      I meant "Recent File" menu like you have in e.g. Qt Creator

      @
      void MyClass::mySlot()
      {
      QObject *senderObject = sender();
      // rest of your code
      }
      @

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • N Offline
        N Offline
        nicky j
        wrote on last edited by
        #7

        Oh okay, sorry I misunderstood you.

        So would I call senderObject in the connect() as well as the slot? Would it have to be in both slots?

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #8

          Have a look at the "Recent File" example in Qt's documentation it shows a variation of the technique nicely

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • N Offline
            N Offline
            nicky j
            wrote on last edited by
            #9

            I checked out the 'Recent File' example, and it is really helpful! I have a small problem though that I don't know how to fix.
            Here is my code thus far:
            @void browseTab::addToHistory()
            {
            QString url = webView->url().toString();

            QString historyEntry = url;
            
            QString title = webView->title();
            
            QIcon icon = webView->icon();
            
            int numRecentHistory = 1;
            
            for (int i = 0; i < numRecentHistory; ++i)
            {
                QString text = tr("&%1").arg(title);
            
                historyAction = new QAction(this);
                historyAction->setText(text);
                historyAction->setData(historyEntry);
                historyAction->setIcon(webView->icon());
                historyAction->setIconVisibleInMenu(true);
                HistoryMenu->addAction(historyAction);
                connect(historyAction, SIGNAL(triggered()), SLOT(goToHistoryURL()));
            }
            

            }

            void browseTab::goToHistoryURL()
            {
            QAction *action = qobject_cast<QAction *>(sender());
            if (action)
            loadHistory(action->data().toString());
            }

            void browseTab::loadHistory(const QString &name)
            {
            webView->setUrl(QUrl(QString(name)));
            }
            @

            When my webView finishes loading a URL, it calls addToHistory() which adds a new QAction. When this QAction is triggered, it calls goToHistoryURL(). goToHistoryURL() in turn calls loadHistory() which sets the webView to the URL enclosed in the QActions data() property. When I run the program, it runs great until I click one of the historyActions to go to that page. My entire application freezes up and crashes. How can I make it just go to the URL without crashing?

            Thanks for your help, I know these are n00bie problems, but I appreciate your help nonetheless!

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #10

              You are probably creating a loop

              Why are you creating numRecentHistory actions each time you call addToHistory ?

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0
              • N Offline
                N Offline
                nicky j
                wrote on last edited by
                #11

                What would be a better way to do it?

                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #12

                  Like I said, follow the Recent File Example, it shows how to handle that

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  0
                  • N Offline
                    N Offline
                    nicky j
                    wrote on last edited by
                    #13

                    I have tried to follow that example, and based on it thats what I have so far.
                    How is it creating a loop and how do I fix it?

                    1 Reply Last reply
                    0
                    • SGaistS Offline
                      SGaistS Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on last edited by
                      #14

                      You're code doesn't reflect the example, you are creating each time numRecentHistory actions.

                      The best way to see where the problem lies is to run your application through the debugger

                      Interested in AI ? www.idiap.ch
                      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                      1 Reply Last reply
                      0
                      • N Offline
                        N Offline
                        nicky j
                        wrote on last edited by
                        #15

                        As in 'debug' vs 'release' when i hit the run button?

                        1 Reply Last reply
                        0
                        • N Offline
                          N Offline
                          nicky j
                          wrote on last edited by
                          #16

                          Would having numRecentHistory outside of the function so it isn't created every time be better?

                          1 Reply Last reply
                          0
                          • SGaistS Offline
                            SGaistS Offline
                            SGaist
                            Lifetime Qt Champion
                            wrote on last edited by
                            #17

                            As in use the button with a little bug on it with a debug build of your application

                            Interested in AI ? www.idiap.ch
                            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                            1 Reply Last reply
                            0
                            • N Offline
                              N Offline
                              nicky j
                              wrote on last edited by
                              #18

                              Okay now if I click on one one of the historyActions, nothing happens. It doesn't load the page, crash, or anything.

                              Code:

                              @void browseTab::addToHistory()
                              {
                              QString url = webView->url().toString();

                              QString historyEntry = url;
                              
                              QString title = webView->title();
                              
                              QIcon icon = webView->icon();
                              
                              int numRecentHistory = 1;
                              
                              for (int i = 0; i < numRecentHistory; ++i)
                              {
                                  QString text = tr("&%1").arg(title);
                              
                                  historyAction = new QAction(this);
                                  historyAction->setText(text);
                                  historyAction->setData(historyEntry);
                                  historyAction->setIcon(webView->icon());
                                  historyAction->setIconVisibleInMenu(true);
                                  HistoryMenu->addAction(historyAction);
                                  connect(historyAction, SIGNAL(triggered()), SLOT(goToHistoryURL()));
                              }
                              

                              }

                              //determines which historyAction has been clicked, calls loadHistory().
                              void browseTab::goToHistoryURL()
                              {
                              QAction *action = qobject_cast<QAction *>(sender());
                              if (action)
                              loadHistory(action->data().toString());
                              }

                              //sets the webView to the historyAction's url, called by goToHistoryURL().
                              void browseTab::loadHistory(const QString &name)
                              {
                              webView->setUrl(QUrl(QString(name)));
                              }
                              @

                              1 Reply Last reply
                              0
                              • N Offline
                                N Offline
                                nicky j
                                wrote on last edited by
                                #19

                                Okay I restarted Qt and now it crashes again like it has been. I ran the debugger and it seemed to have an issue with the following code:
                                @loadHistory(action->data().toString());@

                                How can I fix this?
                                Thanks! I appreciate your help SGaist!

                                1 Reply Last reply
                                0
                                • N Offline
                                  N Offline
                                  nicky j
                                  wrote on last edited by
                                  #20

                                  Sorry to resurrect the thread,

                                  I am still having trouble with my application crashing when I click one of the historyActions, I have read the examples provided, and I still don't understand why it crashes. Could someone please help me?

                                  1 Reply Last reply
                                  0
                                  • A Offline
                                    A Offline
                                    andre
                                    wrote on last edited by
                                    #21

                                    If it crashes, the first thing to do is to get out your debugger to figure out where it crashes.

                                    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