Using connect() for objects in if() statements?
-
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!
-
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.
-
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? -
Is deleting the actions different then removing them?
-
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.