有类似增加菜单的程序,看上去有很多重复类似的代码,
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
QMenu *editMenu = menuBar()->addMenu(tr("Edit"));
QMenu *newObjectMenu = editMenu->addMenu(tr("New Object"));
QAction *newRectangleAction = new QAction(tr("Rectangle"), this);
connect(newRectangleAction, SIGNAL(triggered(bool)), this, SLOT(newRectangle()));
newObjectMenu->addAction(newRectangleAction);
QAction *newLineAction = new QAction(tr("Line"), this);
connect(newLineAction, SIGNAL(triggered(bool)), this, SLOT(newLine()));
newObjectMenu->addAction(newLineAction);
QAction *newEllipseAction = new QAction(tr("Ellipse"), this);
connect(newEllipseAction, SIGNAL(triggered(bool)), this, SLOT(newEllipse()));
newObjectMenu->addAction(newEllipseAction
QAction *newTextAction = new QAction(tr("Text"), this);
connect(newTextAction, SIGNAL(triggered(bool)), this, SLOT(newText()));
newObjectMenu->addAction(newTextAction);*/
...........................
}
下面定义宏来实现上面的功能,更有利于维护
#define ADD_MENU_ACTION(AAA, menuObj, actObj, menuName, menuAct) \
QAction *newAct##AAA = new QAction(menuName, actObj); \
connect(newAct##AAA , SIGNAL(triggered(bool)), actObj, SLOT(menuAct)); \
menuObj->addAction(newAct##AAA);
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
QMenu *editMenu = menuBar()->addMenu(tr("Edit"));
QMenu *newObjectMenu = editMenu->addMenu(tr("New Object"));
ADD_MENU_ACTION(1, newObjectMenu, this, tr("Rectangle"), newRectangle());
ADD_MENU_ACTION(2, newObjectMenu, this, tr("Line"), newLine());
ADD_MENU_ACTION(3, newObjectMenu, this, tr("Ellipse"), newEllipse());
ADD_MENU_ACTION(4, newObjectMenu, this, tr("Text"), newText());
...........................
}
阅读(2128) | 评论(2) | 转发(0) |