QT代码编写菜单栏、子菜单栏、菜单栏显示汉字、信号连接、添加快捷键和图标

QT纯代码编写menubar、添加菜单栏

QMenu *optionMenu = menuBar()->addMenu( tr( "选项" ) );  //新建菜单栏 “选项”
optionMenu->addAction( newGame );  //菜单栏“选项”下添加newgame选项
optionMenu->addAction( exitGame );  
newGame = new QAction( QIcon( ":/res/Icon.png"), tr( "New Game" ), this );

效果如图1:

在这里插入图1图片描述

菜单栏显示汉语、添加快捷键、添加图标

 newGame = new QAction( QIcon( ":/res/Icon.png"), tr( "New Game" ), this );
newGame->setText("新游戏");
newGame->setShortcut(tr("Ctrl+P"));
exitGame->setText("退出");
新效果如下:

在这里插入图片描述

信号连接

  connect( startGame, SIGNAL( triggered() ), this, SLOT( on_startGame() ) );

整体代码

  newGame = new QAction( QIcon( ":/res/Icon.png"), tr( "New Game" ), this );
  QMenu *optionMenu = menuBar()->addMenu( tr( "选项" ) );
  optionMenu->addAction( newGame);
  newGame->setText("新游戏");
  newGame->setShortcut(tr("Ctrl+P"));

  optionMenu->addAction( exitGame );
  exitGame->setText("退出");
  connect( startGame, SIGNAL( triggered() ), this, SLOT( on_startGame() ) );

你可能感兴趣的:(QT,qt)