转载

Qt中实现简单的菜单栏,工具栏和状态栏!

看了一下啊Qt的布局管理机制和Qt的控件,就综合到一起,写了一个简易的Qt代码

还有就是想问一下是否有入门的Qt程序比如实现计算器类型的可以练一下手,巩固一下学的知识!


main.cpp

<pre style="margin-top: 0px; margin-bottom: 0px;"><span style=" color:#000080;">#include</span><span style=" color:#c0c0c0;"> </span><span style=" color:#008000;">"mainwindow.h"</span>
#include <QApplication>
 
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();
 
    return a.exec();
}
 
 


mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H
 
#include <QMainWindow>
#include<QMenu>
#include<QMenuBar>
#include<QAction>
#include<QToolBar>
#include<QDialog>
#include<QPushButton>
#include<QVBoxLayout>
#include<QHBoxLayout>
#include<QGridLayout>
#include<QLineEdit>
#include<QLabel>
 
class MainWindow : public QMainWindow
{
    Q_OBJECT
 
public:
     MainWindow(QWidget *parent = 0);
    ~MainWindow();
 
protected:
     QMenu *File;
     QMenu *Edit;
     QMenu *Window;
     QMenu *Help;
     QMenuBar *total;
 
     QAction *open;
     QAction *save;
     QAction *cancel;
     QAction *chat;
 
     QToolBar *Tool1;
     QToolBar *Tool2;
 
public slots:
     void openclicked();//定义槽函数
 
};
 
#endif // MAINWINDOW_H
 

mainwindow.cpp

#include "mainwindow.h"
 
 
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent)
 
{
    this->setFixedSize(400,300);
 
    open=new QAction(QIcon(":/image/clean"),"open",this);
    open->setStatusTip("you will open a messagebox");
    statusBar();//它是类的函数所以可以直接调用,显示状态栏
 
    save=new QAction(QIcon(":/image/download"),"save",this);
    cancel=new QAction(QIcon(":/image/alert"),"cancel",this);
    chat=new QAction(QIcon(":/image/chat"),"chat",this);
 
    File=new QMenu("         File            ");//往菜单栏内添加动作
    File->addAction(open);
 
    Edit=new QMenu("         Edit           ");
    Edit->addAction(chat);
 
    Window=new QMenu("       Window      ");
    Window->addAction(cancel);
 
    Help=new QMenu("        Help        ");
    Help->addAction(save);
 
    menuBar()->addMenu(File); //往主窗口添加菜单项
    menuBar()->addMenu(Edit);
    menuBar()->addMenu(Window);
    menuBar()->addMenu(Help);
 
    Tool1=new QToolBar(this);//向工具栏内添加动作
    Tool1->addAction(open);
    Tool1->addAction(cancel);
 
    Tool2=new QToolBar(this);
    Tool2->addAction(save);
    Tool2->addAction(chat);
 
    addToolBar(Qt::TopToolBarArea,Tool1);//把这两个工具栏添加到窗口
    addToolBar(Qt::TopToolBarArea,Tool2);
   connect(open,SIGNAL(triggered()),this,SLOT(openclicked()));
 
}
 
class dialog:public QDialog
{
  public:
 
   QPushButton *button1;
   QPushButton *button2;
   QLineEdit   *lineidt;
   QLabel     *label;
   QGridLayout *gridlayout;
 
   dialog()//构造函数
   {
      this->setFixedSize(600,400);
      button1=new QPushButton("choose");
      button2=new QPushButton("quit");
 
      label=new QLabel("input data");
      lineidt=new QLineEdit("");
 
      gridlayout=new QGridLayout(this);
 
      gridlayout->addWidget(label,0,0);
      gridlayout->addWidget(lineidt,0,1);
      gridlayout->addWidget(button1,1,0);
      gridlayout->addWidget(button2,1,1);
 
      connect(button2,SIGNAL(clicked()),this,SLOT(close()));
      setLayout(gridlayout);
   }
 
   ~dialog()
   {
 
   }
 
};
 
void MainWindow::openclicked()
{
   dialog a;
   a.exec();
}
 
 
MainWindow::~MainWindow()
{
 
}
 



正文到此结束
本文目录