今天来实现一下记事本中的剪切、拷贝、粘贴等功能
一、添加菜单栏
QMenu *editMenu = menuBar()->addMenu("Edit"); QToolBar *editToolBar = addToolBar("Edit"); QAction *cutAct = new QAction(QIcon(":/images/cut.png"), "Cut", this); cutAct->setShortcuts(QKeySequence::Cut); cutAct->setStatusTip(tr("Cut the current selection's contents to the " "clipboard")); connect(cutAct, &QAction::triggered, textEdit, &QPlainTextEdit::cut); editMenu->addAction(cutAct); editToolBar->addAction(cutAct); QAction *copyAct = new QAction(QIcon(":/images/copy.png"), "Copy", this); copyAct->setShortcuts(QKeySequence::Copy); copyAct->setStatusTip("Copy the current selection's contents to the " "clipboard"); connect(copyAct, &QAction::triggered, textEdit, &QPlainTextEdit::copy); editMenu->addAction(copyAct); editToolBar->addAction(copyAct); QAction *pasteAct = new QAction(QIcon(":/images/paste.png"), "Paste", this); pasteAct->setShortcuts(QKeySequence::Paste); pasteAct->setStatusTip("Paste the clipboard's contents into the current " "selection"); connect(pasteAct, &QAction::triggered, textEdit, &QPlainTextEdit::paste); editMenu->addAction(pasteAct); editToolBar->addAction(pasteAct); menuBar()->addSeparator(); QMenu *helpMenu = menuBar()->addMenu("Help"); QAction *aboutAct = helpMenu->addAction("&About", this, &MainWindow::about); aboutAct->setStatusTip("Show the application's About box"); QAction *aboutQtAct = helpMenu->addAction("About &Qt", qApp, &QApplication::aboutQt); aboutQtAct->setStatusTip("Show the Qt library's About box"); cutAct->setEnabled(false); copyAct->setEnabled(false); connect(textEdit, &QPlainTextEdit::copyAvailable, cutAct, &QAction::setEnabled); connect(textEdit, &QPlainTextEdit::copyAvailable, copyAct, &QAction::setEnabled); //创建状态栏 statusBar()->showMessage(tr("Ready"));
标签:addAction,Qt,pasteAct,QAction,菜单栏,cutAct,connect,copyAct,粘贴 From: https://www.cnblogs.com/QingYiShouJiuRen/p/16849584.html