用枚举填充QMenu

时间:2017-04-28 16:17:34

标签: c++ qt enums qmenu

我有一些structenumstd::string我曾经使用QComboBox循环填充for() es。像这样:

struct Struct
{
    enum Enum
    {
        ENTRY1,
        ENTRY2,
        ...
        ENTRY_ALL
    };
    std::string names[ENTRY_ALL] {...};
};
for(int i=0; i<Struct::ENTRY_ALL; ++i)
    comboBox->insertItem(i, names[i].c_str());

虽然有效,但考虑到最近的进展,我需要更多的条目,更多的struct s,更多的组合框,并且它变得丑陋,所以我决定切换到QMenu

查看文档和示例,我看到我必须为每个条目提供connect(),为每个条目提供QAction等,如果我的enum很大,这会成为一个问题,所以我想知道是否有办法自动化这个过程。

我的问题主要来自于我为每个connect()只需要一个QComboBox以及QComboBox如何知道,如果我填写它们,哪个条目可以做什么?< / p>

如果有帮助的话,这就是我所拥有的一些虚假代码:

// tank class, header:
class Class
{
private:
    // main entries
    struct MainStruct
    {
        enum Enum {MAIN1, MAIN2, MAIN3, MAIN_ALL};
        std::string names[MAIN_ALL] {"Main 1", "Main 2", "Main 3"};
    };
    // secondary entries
    struct SubStruct
    {
        enum Enum {SUB1, SUB2, SUB3, SUB_ALL};
        std::string names[SUB_ALL] {"Sub 1", "Sub 2", "Sub 3"};
    };
    // family of entries for SUB2
    struct FamStruct
    {
        enum Enum {FAM1, FAM2, FAM3, FAM_ALL};
        std::string names[FAM_ALL] {"Fam 1", "Fam 2", "Fam 3"};
    }
public:
    MainStruct mainEntries;
    SubStruct subEntries;
    FamStruct famEntries;
};

// inside the main window, there was a QComboBox doing this:
// header
QComboBox *cbMain, *cbSub, *cbFam;
// source
Class cls {Class()};
cbMain = new QComboBox; // same for cbSub, cbFam
// retranslateUi() (manually edited, not automatic), called each time the menu was clicked
void MainApp::retranslateUi()
{
    cbMain.clear();
    for(unsigned char i=0; i<cls.mainEntries.MAIN_ALL; ++i)
    {
        cbMain->insertItem(i, tr(cls.mainEntries.names[i].c_str()) );
        cbMain->setItemData(i, tr(cls.mainEntries.tooltips[i].c_str()), Qt::ToolTipRole);
    }
    // same for cbSub, cbFam
}
// a function that did what was supposed to do when a combo box entry was clicked
void MainApp::func() { /* execution with variables changed by the combo */ }
/* There were additional functions taking care of the rest.
 * Whenever an entry from the combo was clicked, somehow it "knew" that the entry was
 * changed and func(), when executed, it ran with the variables changed by combo.
 */
void MainApp::updateSub()
{
    switch(cbMain->currentIndex())
    {
    case cls.mainEntries.MAIN1: /*subentry stuff*/ break;
    case cls.mainEntries.MAIN2: /*subentry stuff*/ break;
    case cls.mainEntries.MAIN3: /*subentry stuff*/ break;
    }
}
void MainApp::updateFam()
{
    switch(cbSub->currentIndex())
    {
    case cls.subEntries.SUB1: /*fam entry stuff*/ break;
    case cls.subEntries.SUB2: /*fam entry stuff*/ break;
    case cls.subEntries.SUB3: /*fam entry stuff*/ break;
    }
}
/* The connect() were attached to specific combos. When an entry in cbMain was
 * clicked, cbSub and cbFam were properly updated and func() was run with
 * the proper values, when cbSub was clicked, cbFam was updated and func()
 * was run accordingly. Similar to an instant refresh.
 */
connect(cbMain, SIGNAL(activated(int)), this, SLOT(retranslateUi()));
connect(cbMain, SIGNAL(activated(int)), this, SLOT(updateSub()));
connect(cbMain, SIGNAL(activated(int)), this, SLOT(func()));
connect(cbSub, SIGNAL(activated(int)), this, SLOT(updateFam()));
connect(cbSub, SIGNAL(activated(int)), this, SLOT(func()));
connect(cbFam, SIGNAL(activated(int)), this, SLOT(func()));

每当我不得不为子菜单添加新的条目系列,甚至包含子条目的主菜单时,事情会变得复杂,所以我认为这样的菜单:

Menu:   Submenu:                AnotherSubmenu:
menu 1  submenu 1               altsub 1
menu 2  submenu 2 > family 1    altsub 2
menu 3  submenu 3   family 2    ...
                    family 3

会更好,更整洁。 Menu中的条目会调用适当的Submenu,其中有Family,无论身在何处,Submenu都可以调用另一级AnotherSubmenu等。

1 个答案:

答案 0 :(得分:1)

您可以将所有QAction的触发信号连接到同一个插槽,并且可以将对象与sender()连接,并使用{{将其转换为QAction对象类型1}}。

此外,如果您想使用索引,我们可以使用qobject_cast<>setObjectName()

objectName()