承接上章,為 QAction 加入圖示並連接信號槽。
/:/Open.png(冒號開頭 + prefix + 檔名)

工具列預設 32×32,選單 16×16。圖示過小不放大,過大自動縮小。正式專案建議建立 resources 目錄管理。
openAction = new QAction(tr("&Open"), this);
openAction->setShortcut(QKeySequence::Open);
openAction->setStatusTip(tr("Open a file."));
openAction->setIcon(QIcon(":/Open.png")); // 新增

選單與工具列自動套用圖示,無需額外程式碼。
// mainwindow.h
private slots:
void open();
// mainwindow.cpp
connect(openAction, SIGNAL(triggered()), this, SLOT(open()));
void MainWindow::open() {
QMessageBox::information(NULL, tr("Open"), tr("Open a file"));
}

子選單可用 QMenu::addMenu() 建立。QAction 核心功能告一段落。