Chinaunix首页 | 论坛 | 博客
  • 博客访问: 45226
  • 博文数量: 12
  • 博客积分: 325
  • 博客等级: 一等列兵
  • 技术积分: 170
  • 用 户 组: 普通用户
  • 注册时间: 2012-08-02 12:08
文章分类

全部博文(12)

文章存档

2012年(12)

我的朋友

分类: LINUX

2012-09-19 15:44:45

mainview.h

点击(此处)折叠或打开

  1. #ifndef MAINVIEW_H
  2. #define MAINVIEW_H
  3. #include <QMainWindow>
  4. class QTextEdit;
  5. class QMenuBar;
  6. class QLabel;
  7. class QAction;
  8. class QToolBar;
  9. class QMenuBar;
  10. class QStatusBar;
  11. class QDockWidget;
  12. class QPushButton;
  13. class MainView:public QMainWindow
  14. {
  15.     Q_OBJECT
  16.  public:
  17.     MainView(QWidget* parent = 0);

  18. public slots:
  19.     void slot_toolbar_cmd1();
  20.     void slot_toolbar_cmd3();
  21.     void slot_toolbar_cmd2();
  22.     void slot_press_ok_btn();

  23. private:
  24.     QMenuBar *pmenuBar;
  25.     QTextEdit *textEdit;
  26.     QMenu *fileMenu;
  27.     QMenu *editMenu;
  28.     QMenu *aboutMenu;
  29.     QToolBar *toolbar;
  30.     QAction *fileAction;
  31.     QStatusBar *pStatusBar;
  32.     QDockWidget *dockWidget;
  33.     QPushButton *btnDockWidget;
  34. };

  35. #endif // MAINVIEW_H

mainview.cpp

点击(此处)折叠或打开

  1. #include <QTextEdit>
  2. #include <QMenu>
  3. #include <QToolBar>
  4. #include <QStatusBar>
  5. #include <QDockWidget>
  6. #include <QString>
  7. #include <QAction>
  8. #include <QMenuBar>
  9. #include <QPushButton>
  10. #include "mainview.h"

  11. MainView::MainView(QWidget *parent):QMainWindow(parent)
  12. {
  13.     //文字编辑区
  14.     textEdit = new QTextEdit;
  15.     textEdit->setFocus();
  16.     setCentralWidget(textEdit);

  17.     //menu
  18.     fileMenu = new QMenu("&File");
  19.     fileAction = new QAction("Open..", fileMenu);

  20.     //add a shortcut
  21.     fileAction->setShortcut(Qt::CTRL + Qt::Key_O);
  22.     fileMenu->addAction(fileAction);
  23.     fileMenu->addAction("Save");
  24.     fileMenu->addAction("Save as..");

  25.     //space line
  26.     fileMenu->addSeparator();

  27.     //Menu edit
  28.     editMenu = new QMenu("&Edit");
  29.     editMenu->addAction("Cut");
  30.     editMenu->addAction("Copy");
  31.     editMenu->addAction("Paste");

  32.     //Menu About
  33.     aboutMenu = new QMenu("&About");
  34.     aboutMenu->addAction("About");

  35.     //add Menu
  36.     pmenuBar = menuBar();
  37.     pmenuBar->addMenu(fileMenu);
  38.     pmenuBar->addMenu(editMenu);
  39.     pmenuBar->addMenu(aboutMenu);

  40.     //toolbar
  41.     toolbar = new QToolBar("QtoolBar");
  42.     QAction *actionCmd1 = new QAction("cmd1",this);
  43.     QAction *actionCmd2 = new QAction("cmd2",this);
  44.     QAction *actionCmd3 = new QAction("cmd3", this);

  45.     toolbar->addAction(actionCmd1);
  46.     toolbar->addAction(actionCmd2);
  47.     toolbar->addAction(actionCmd3);
  48.     this->addToolBar(toolbar);

  49.     connect(actionCmd1,SIGNAL(triggered()),this, SLOT(slot_toolbar_cmd1()));
  50.     connect(actionCmd2,SIGNAL(triggered()),this, SLOT(slot_toolbar_cmd2()));
  51.     connect(actionCmd3,SIGNAL(triggered()),this, SLOT(slot_toolbar_cmd3()));

  52.     //statusbar
  53.     pStatusBar = statusBar();
  54.     pStatusBar->showMessage("see me statusBar");

  55.     //button
  56.     btnDockWidget = new QPushButton("ok");

  57.     //DockWidget
  58.     dockWidget = new QDockWidget("QDockWidget");
  59.     dockWidget->setWidget(btnDockWidget);
  60.     connect(btnDockWidget,SIGNAL(clicked()),this, SLOT(slot_press_ok_btn()));
  61.     addDockWidget(Qt::RightDockWidgetArea,dockWidget);
  62. }
  63. void MainView::slot_press_ok_btn()
  64. { static int i = 0;
  65.     QString str;
  66.     str.sprintf("you press ok btn %d times", i++);
  67.     textEdit->setText(str);
  68.     pStatusBar->showMessage(str);
  69. }

  70. void MainView::slot_toolbar_cmd1()
  71. {
  72.     textEdit->setText("you press toolbar cmd1");
  73.     pStatusBar->showMessage("cmd1");
  74. }

  75. void MainView::slot_toolbar_cmd2()
  76. {
  77.     textEdit->setText("you press toolbar cmd2");
  78.     pStatusBar->showMessage("cmd2");
  79. }

  80. void MainView::slot_toolbar_cmd3()
  81. {
  82.     textEdit->setText("you press toolbar cmd3");
  83.     pStatusBar->showMessage("cmd3");
  84. }

 

main.cpp

点击(此处)折叠或打开

  1. #include <QApplication>
  2. #include "mainview.h"

  3. int main(int argc, char* argv[])
  4. {
  5.     QApplication app(argc, argv);
  6.     MainView *mainview = new MainView;
  7.     mainview->setWindowTitle("MainWindow test");
  8.     mainview->resize(500,300);
  9.     mainview->show();
  10.     return app.exec();
  11. }
运行效果图

image

image

 

image

 

image

阅读(1612) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~