对QT学习之路12-14的源代码补充与修正

      QT学习之路12-14的源代码有些不完整,为了更好的让大家学习,本人做了一点修正与补充,谢谢。源代码如下:

      头文件:     

  
  
  
  
  1. #ifndef MAINWINDOW_H 
  2. #define MAINWINDOW_H 
  3.  
  4. #include <QtGui/QMainWindow> 
  5. #include <QLabel> 
  6. #include <QStatusBar> 
  7.  
  8. class QAction; 
  9. //public class QLabel; 
  10.  
  11. class MainWindow : public QMainWindow 
  12.         Q_OBJECT 
  13.  
  14. public: 
  15.         MainWindow(QWidget *parent = 0); 
  16.         ~MainWindow(); 
  17. private slots: 
  18.         void open(); 
  19.  
  20. private: 
  21.         QAction *openAction; 
  22.         QLabel  *msgLabel; 
  23. }; 
  24.  
  25. #endif // MAINWINDOW_H 

主源代码:mainwindow.cpp

  
  
  
  
  1. #include <QtGui/QAction> 
  2. #include <QtGui/QMenu> 
  3. #include <QtGui/QMenuBar> 
  4. #include <QtGui/QKeySequence> 
  5. #include <QtGui/QToolBar> 
  6. #include <QMessageBox> 
  7. //#include <QLabel> 
  8. #include "mainwindow.h" 
  9.  
  10. MainWindow::MainWindow(QWidget *parent) 
  11.         : QMainWindow(parent) 
  12.         openAction = new QAction(tr("&Open"), this); 
  13.         openAction->setShortcut(QKeySequence::Open); 
  14.         openAction->setStatusTip(tr("Open a file.")); 
  15.         openAction->setIcon(QIcon(":/2.png")); 
  16.         connect(openAction, SIGNAL(triggered()), this, SLOT(open())); 
  17.  
  18.         QMenu *file = menuBar()->addMenu(tr("&File")); 
  19.         file->addAction(openAction); 
  20.  
  21.         QToolBar *toolBar = addToolBar(tr("&File")); 
  22.         toolBar->addAction(openAction); 
  23.         msgLabel = new QLabel; 
  24.         msgLabel->setMinimumSize(msgLabel->sizeHint()); 
  25.         msgLabel->setAlignment(Qt::AlignHCenter); 
  26.         statusBar()->addWidget(msgLabel); 
  27. void MainWindow::open() 
  28.         QMessageBox::information(NULL, tr("Open"), tr("Open a file")); 
  29.  
  30. MainWindow::~MainWindow() 
  31.  

主函数main.cpp

 

  
  
  
  
  1. #include <QtGui/QApplication> 
  2. #include "mainwindow.h" 
  3.  
  4. int main(int argc, char *argv[]) 
  5.     QApplication a(argc, argv); 
  6.     MainWindow w; 
  7.     w.show(); 
  8.     return a.exec(); 

图标文件依据原文制作使用

你可能感兴趣的:(职场,休闲,QT入门教程源码修正)