Qt中的菜单类使用

Qt中的菜单类使用
2008-10-15 11:26:38
标签: QMenu  c++  休闲  Qt  职场
mainwindow.h:
 
#ifndef __MAINWINDOW_H__ 
#define __MAINWINDOW_H__ 
 
#include <QMainWindow> 
 
class QMenu; 
class QAction; 
class QLabel; 
 
class MainWindow: public QMainWindow 

  Q_OBJECT 
public
  MainWindow(); 
 
private slots: 
   void newFile(); 
   void openFile(); 
   void saveFile(); 
   void printFile(); 
   void about(); 
   void bold(); 
   void italic(); 
private
  QMenu* fileMenu; //文件菜单 
  QMenu* editMenu; //编辑菜单 
  QMenu* helpMenu; //帮助菜单 
  QMenu* formatMenu; 
    
  QAction* newAct; //新建 
  QAction* openAct; //打开 
  QAction* saveAct; //保存 
  QAction* printAct; //打印 
  QAction* exitAct; //退出 
  QAction* boldAct; 
  QAction* italicAct; 
    
  QAction* aboutAct; //关于 
  QLabel* infoLabel; 
    
   void createActions(); 
   void createMenus(); 
}; 
 
#endif  // __MAINWINDOW_H__ 
 
 
mainwindow.cpp:
 
#include <QMenu> 
#include <QAction> 
#include <QMenuBar> 
#include <QLabel> 
#include <QWidget> 
#include <QVBoxLayout> 
#include <QStatusBar> 
#include <QKeySequence> 
#include <QMessageBox> 
#include  "mainwindow.h" 
 
MainWindow::MainWindow() 

  QWidget *widget= new QWidget; 
  setCentralWidget(widget); //设置中央Widget 
 
  infoLabel= new QLabel( "Choose a menu option,or right-click to invoke action"); 
  infoLabel->setAlignment(Qt::AlignCenter); //居中对齐 
  QVBoxLayout *layout= new QVBoxLayout; 
  layout->addWidget(infoLabel); 
    
  widget->setLayout(layout); 
    
  createActions(); 
  createMenus(); 
    
  QString msg= "popup a menu by right-clicking."
  statusBar()->showMessage(msg); 
    
  setWindowTitle(tr( "Menus")); 
  setMinimumSize(160,160); //设置最小尺寸 
  resize(480,320); //设置尺寸 

 
//创建Actions 
void MainWindow::createActions() 

  newAct= new QAction( "&New", this); 
  newAct->setShortcut(QKeySequence( "Ctrl+N")); //设置快捷键 
  newAct->setStatusTip( "New a file"); //设置状态栏提示 
  connect(newAct,SIGNAL(triggered()), this,SLOT(newFile())); 
    
         openAct =  new QAction( "&Open..."this); 
         openAct->setShortcut(tr( "Ctrl+O")); 
         openAct->setStatusTip( "Open an existing file"); 
         connect(openAct, SIGNAL(triggered()),  this, SLOT(openFile()));    
            
         saveAct =  new QAction( "&Save"this); 
         saveAct->setShortcut(tr( "Ctrl+S")); 
         saveAct->setStatusTip( "save the    file"); 
         connect(saveAct, SIGNAL(triggered()),  this, SLOT(saveFile()));             
            
         printAct =  new QAction( "&Print..."this); 
         printAct->setShortcut(tr( "Ctrl+P")); 
         printAct->setStatusTip( "Print the    file"); 
         connect(printAct, SIGNAL(triggered()),  this, SLOT(printFile()));                     
            
         exitAct =  new QAction( "E&xit"this); 
         exitAct->setShortcut(tr( "Ctrl+Q")); 
         exitAct->setStatusTip( "Exit the Application."); 
         connect(exitAct, SIGNAL(triggered()),  this, SLOT(close()));            
            
         boldAct =  new QAction(tr( "&Bold"),  this); 
         boldAct->setCheckable( true); //可选择 
         boldAct->setShortcut(tr( "Ctrl+B")); 
         boldAct->setStatusTip(tr( "Make the text bold")); 
         connect(boldAct, SIGNAL(triggered()),  this, SLOT(bold()));            
         QFont boldFont = boldAct->font(); 
         boldFont.setBold( true); //字体加粗 
         boldAct->setFont(boldFont); //设置字体 
            
         italicAct =  new QAction(tr( "&Italic"),  this); 
         italicAct->setCheckable( true); 
         italicAct->setShortcut(tr( "Ctrl+I")); 
         italicAct->setStatusTip(tr( "Make the text italic")); 
         connect(italicAct, SIGNAL(triggered()),  this, SLOT(italic())); 
         QFont italicFont = italicAct->font(); 
         italicFont.setItalic( true); //设置倾斜 
         italicAct->setFont(italicFont); 
            
            
         aboutAct= new QAction( "&About", this); 
         aboutAct->setStatusTip( "About the App"); 
         connect(aboutAct,SIGNAL(triggered()), this,SLOT(about())); 
                

 
//创建菜单 
void MainWindow::createMenus() 

   //文件菜单 
  fileMenu=menuBar()->addMenu( "&File"); 
  fileMenu->addAction(newAct); 
  fileMenu->addAction(openAct); 
  fileMenu->addAction(saveAct); 
  fileMenu->addAction(printAct); 
  fileMenu->addSeparator(); 
  fileMenu->addAction(exitAct); 
    
   //编辑菜单 
  editMenu=menuBar()->addMenu( "&Edit"); 
  formatMenu=editMenu->addMenu( "&Format"); 
  formatMenu->addAction(boldAct); 
  formatMenu->addAction(italicAct); 
    
   //帮助菜单 
  helpMenu=menuBar()->addMenu( "&Help"); 
  helpMenu->addAction(aboutAct); 
    

 
void MainWindow::newFile() 

  infoLabel->setText( "Invoke<b>File|New</b>"); 

void MainWindow::openFile() 

  infoLabel->setText( "Invoke<b>File|Open</b>"); 

void MainWindow::saveFile() 

  infoLabel->setText( "Invoke<b>File|Save</b>"); 

 
void MainWindow::printFile() 

  infoLabel->setText( "Invoke<b>File|Print</b>"); 

 
//关于对话框 
void MainWindow::about() 

  infoLabel->setText( "Invoke<b>Help|About</b>"); 
  QMessageBox::about( this, "About", "How to createMenus"); 

 
void MainWindow::bold() 

  infoLabel->setText( "Invoke<b>Edit|Format|bold</b>"); 

 
void MainWindow::italic() 

  infoLabel->setText( "Invoke<b>Edit|Format|italic</b>"); 

 
 
main.cpp:
 
#include <QApplication> 
#include  "mainwindow.h" 
 
int main( int argc, char* argv[]) 

  QApplication app(argc,argv); 
  MainWindow win; 
  win.show(); 
   return app.exec(); 
}
 
 
截图
Qt中的菜单类使用_第1张图片

你可能感兴趣的:(File,layout,action,qt,menu,Signal)