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. Using connect() for objects in if() statements?
Forum Updated to NodeBB v4.3 + New Features

Using connect() for objects in if() statements?

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

    Hello,

    I have a part of my program that creates and places modified QActions through a bunch of nested if() statements. I would like to hook each of these actions up to a connect() function, but I am having trouble. Where in my code should I put the connect() functions? Heres my code:
    @
    void NBhistoryMenu::populate()
    {
    qDebug() << "SLOT: NBhistoryMenu::populate() STATUS: Called";

    this->removeAction(action1);
    this->removeAction(action2);
    this->removeAction(action3);
    this->removeAction(action4);
    this->removeAction(action5);
    this->removeAction(action6);
    this->removeAction(action7);
    this->removeAction(action8);
    this->removeAction(action9);
    this->removeAction(action10);
    
    action1 = new NBhistoryAction(this);
    qDebug() << "historyTitle(0) Called";
    QString text = m_history->historyTitle(0);
    qDebug() << "historyTitle(0) Complete";
    action1->setText(text);
    connect(action1, SIGNAL(historyRequest(QString)), SLOT(emitHistoryActionClicked(QString)));
    addAction(action1);
    
    if(m_history->countTitle() > 1)
    {
        action2 = new NBhistoryAction(this);
        action2->setText(m_history->historyTitle(1));
        action2->setHistoryItem(m_history->historyUrl(1));
        //connect(action2, SIGNAL(historyRequest(QString)), SLOT(emitHistoryActionClicked(QString)));
        addAction(action2);
    
        if(m_history->countTitle() > 2)
        {
            action3 = new NBhistoryAction(this);
            action3->setText(m_history->historyTitle(2));
            //action3->setHistoryItem(m_history->historyUrl(2));
            //connect(action3, SIGNAL(historyRequest(QString)), SLOT(emitHistoryActionClicked(QString)));
            addAction(action3);
    
            if(m_history->countTitle() > 3)
            {
                action4 = new NBhistoryAction(this);
                action4->setText(m_history->historyTitle(3));
                //action4->setHistoryItem(m_history->historyUrl(3));
                //connect(action4, SIGNAL(historyRequest(QString)), SLOT(emitHistoryActionClicked(QString)));
                addAction(action4);
    
                if(m_history->countTitle() > 4)
                {
                    action5 = new NBhistoryAction(this);
                    action5->setText(m_history->historyTitle(4));
                    //action5->setHistoryItem(m_history->historyUrl(4));
                    //connect(action5, SIGNAL(historyRequest(QString)), SLOT(emitHistoryActionClicked(QString)));
                    addAction(action5);
    
                    if(m_history->countTitle() > 5)
                    {
                        action6 = new NBhistoryAction(this);
                        action6->setText(m_history->historyTitle(5));
                        //action6->setHistoryItem(m_history->historyUrl(5));
                        //connect(action6, SIGNAL(historyRequest(QString)), SLOT(emitHistoryActionClicked(QString)));
                        addAction(action6);
    
                        if(m_history->countTitle() > 6)
                        {
                            action7 = new NBhistoryAction(this);
                            action7->setText(m_history->historyTitle(6));
                            //action7->setHistoryItem(m_history->historyUrl(6));
                            //connect(action7, SIGNAL(historyRequest(QString)), SLOT(emitHistoryActionClicked(QString)));
                            addAction(action7);
    
                            if(m_history->countTitle() > 7)
                            {
                                action8 = new NBhistoryAction(this);
                                action8->setText(m_history->historyTitle(7));
                                //action8->setHistoryItem(m_history->historyUrl(7));
                                //connect(action8, SIGNAL(historyRequest(QString)), SLOT(emitHistoryActionClicked(QString)));
                                addAction(action8);
    
                                if(m_history->countTitle() > 8)
                                {
                                    action9 = new NBhistoryAction(this);
                                    action9->setText(m_history->historyTitle(8));
                                    //action9->setHistoryItem(m_history->historyUrl(8));
                                    //connect(action9, SIGNAL(historyRequest(QString)), SLOT(emitHistoryActionClicked(QString)));
                                    addAction(action9);
    
                                    if(m_history->countTitle() > 9)
                                    {
                                        action10 = new NBhistoryAction(this);
                                        action10->setText(m_history->historyTitle(9));
                                        //action10->setHistoryItem(m_history->historyUrl(9));
                                        //connect(action10, SIGNAL(historyRequest(QString)), SLOT(emitHistoryActionClicked(QString)));
                                        addAction(action10);
    
                                        if(m_history->countTitle() > 10)
                                        {
                                            m_history->saveHistory();
                                            this->populateOlder();
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    qDebug() << "SLOT: NBhistoryMenu::populate() STATUS: Completed";
    

    }

    @

    You can see where I've tried to implement them, they are commented out. Where do the connect() statements go?

    Thanks!

    1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      They are where they should be. The problem, I think, is that you are not deleting action* objects before assigning new values using new operator.

      (Z(:^

      1 Reply Last reply
      0
      • Chris KawaC Offline
        Chris KawaC Offline
        Chris Kawa
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Btw. This code would be 10 lines long if you used a for loop.
        What if number of actions grows to 100? Can you imagine changing or debugging that code?

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

          Is deleting the actions different then removing them?

          1 Reply Last reply
          0
          • Chris KawaC Offline
            Chris KawaC Offline
            Chris Kawa
            Lifetime Qt Champion
            wrote on last edited by
            #5

            Yes. Deleting an action destroys the object. Removing it only detaches it from the given widget. Single action can be added to several menus, widgets, toolbars etc. so removing it from one of them doesn't affect the others and the action object itself.

            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