Qt実現基本QMainWindowメインウィンドウプログラム

19842 ワード

1.Qt creatorの新しいMainWindowプロジェクトを開きます.
2、右クリック工事名に新しいファイル、mainファイルを追加し、接尾辞を自動的に補完してmainとする.cpp.
3、プロジェクトを選択し、再度右クリックし、テンプレートを選択するときにC++クラスを選択し、QMainWindowを継承するMainWindowクラスを追加します.
4、mainwindowを追加します.qrcリソースファイル
コードセクション:
main.cpp
#include 
#include "mainwindow.h"
 
  
 
  
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();
 
  
 
  
    return a.exec();
}
 
   
   
  

注:

1、在qmake工程中,默认情况下已经包含了QtCore和QtGui模块,因此无须配置就可以使用这两个模块中的类。若不想使用QtGui模块,而仅仅使用QtCore,就可以在qmake工程文件中通过使用"QT-=gui"来取消QtGui模块的包含。 

2、一般应用程序中通过#include来包含整个QtGui模块的所有类的头文件,其中第一个QtGui是模块名,第二个QtGui是QtGui模块(文件名)下的预定义头文件;

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H
 
  
#include 
 
  
 
  
class MainWindow : public QMainWindow
{
    Q_OBJECT
 
  
public:
    MainWindow(QWidget *parent = 0, Qt::WFlags flags = 0);
    ~MainWindow();
 
  
    void createMenus();     //    
    void createActions();   //    
    void createToolBars();  //     
    void createStatusBar(); //     
public slots:
    void sl_NewFile();
    void sl_OpenFile();
    void sl_SaveFile();
    void sl_Copy();
    void sl_Cut();
    void sl_Paste();
    void sl_About();
protected:
    void loadFile(QString);
 
  
//                 
 
  
private:
    QMenu *fileMenu;
    QMenu *editMenu;
    QMenu *aboutMenu;
 
  
    QToolBar *fileTool;
    QToolBar *editTool;
 
  
    QAction *fileOpenAction;
    QAction *fileNewAction;
    QAction *fileSaveAction;
    QAction *exitAction;
 
  
    QAction *copyAction;
    QAction *cutAction;
    QAction *pasteAction;
 
  
    QAction *aboutAction;
 
  
    QLabel *tipLabel;
 
  
    QTextEdit *edtText;
};
 
  
#endif // MAINWINDOW_H
 
  
 
   
  

注:

 
   
  
1、#ifndef MAINWINDOW_H
#define MAINWINDOW_H
         ,  #endif // MAINWINDOW_H        
 
   
  

2、Q_OBJECT宏对于所有使用了信号/槽机制的类而言是必须要的,同时它要求放置在类声明的开始处。

 
  
3、             ,          ,      void。
public slots:
    void sl_NewFile();
    void sl_OpenFile();
    void sl_SaveFile();
    void sl_About();
は、スロットを通常の関数として呼び出すことができることに注意しなければならない.この場合、その戻り値は、通常のC++関数を呼び出すことによって生じる戻り値と変わらない.一方、スロットが信号の応答関数として実行されると、その戻り値はプログラムによって無視される.つまり、
信号を使用しないで、スロット関数を正常に呼び出していくつかのことを完了することもできます.
以下に、宣言スロットと宣言スロットの使用の違いを示します.
 
  
//"  "  
    fileOpenAction = new QAction(QIcon("E:/Qt/MainWindow/open.png"),tr("Open"),this);
    fileOpenAction->setShortcut(tr("Ctrl+0"));
    fileOpenAction->setStatusTip(tr("open a file"));
    connect(fileOpenAction,SIGNAL(triggered()),this,SLOT(sl_OpenFile()));
 
  
//"  "  
    cutAction = new QAction(QIcon("E:/Qt/MainWindow/cut.gif"),tr("cut"),this);
    cutAction->setShortcut(tr("Ctrl+X"));
    cutAction->setStatusTip("cut to clipboard");
    connect(cutAction,SIGNAL(triggered()),this,SLOT(cut()));

mainwindow.cpp
 
  
#include "mainwindow.h"
#include 
#include 

MainWindow::MainWindow(QWidget *parent, Qt::WFlags flags)
    : QMainWindow(parent, flags)
{
    //       
    setWindowTitle(tr("QMainWindow"));
    //this            MainWindow
    edtText = new QTextEdit(this);
    //             edtText
    setCentralWidget(edtText);

    createActions();
    createMenus();
    createToolBars();
    createStatusBar();
}

MainWindow::~MainWindow()
{

}

void MainWindow::createActions()
{
    //"  "  
    fileOpenAction = new QAction(QIcon(":/images/open.png"),tr("Open"),this);
    fileOpenAction->setShortcut(tr("Ctrl+o"));
    fileOpenAction->setStatusTip(tr("open a file"));
    connect(fileOpenAction,SIGNAL(triggered()),this,SLOT(sl_OpenFile()));

    //"  "  
    fileNewAction = new QAction(QIcon(":/images/new.gif"),tr("New"),this);
    fileNewAction->setShortcut(tr("Ctrl+N"));
    fileNewAction->setStatusTip(tr("new file"));
    connect(fileNewAction,SIGNAL(triggered()),this,SLOT(sl_NewFile()));

    //"  "  
    fileSaveAction = new QAction(QIcon(":/images/save.png"),tr("Save"),this);
    fileSaveAction->setShortcut(tr("Ctrl+S"));
    fileSaveAction->setStatusTip(tr("save file"));
    connect(fileSaveAction,SIGNAL(activated()),this,SLOT(sl_SaveFile()));

    //"  "  
    exitAction = new QAction(tr("Exit"),this);
    exitAction->setShortcut(tr("Ctrl+Q"));
    exitAction->setStatusTip(tr("exit"));
    connect(exitAction,SIGNAL(triggered()),this,SLOT(close()));

    //"  "  
    cutAction = new QAction(QIcon(":/images/cut.gif"),tr("cut"),this);
    cutAction->setShortcut(tr("Ctrl+X"));
    cutAction->setStatusTip("cut to clipboard");
    connect(cutAction,SIGNAL(triggered()),this,SLOT(cut()));

    //"  "  
    copyAction = new QAction(QIcon(":/images/copy.png"),tr("copy"),this);
    copyAction->setShortcut(tr("Ctrl+C"));
    copyAction->setStatusTip("copy to clipboard");
    connect(copyAction,SIGNAL(triggered()),this,SLOT(copy()));

    //"  "  
    pasteAction = new QAction(QIcon(":/images/paste.gif"),tr("paste"),this);
    pasteAction->setShortcut(tr("Ctrl+V"));
    pasteAction->setStatusTip("paste clipboard to selection");
    connect(pasteAction,SIGNAL(triggered()),this,SLOT(paste()));

    //"  "  
    aboutAction = new QAction(tr("About"),this);
    connect(aboutAction,SIGNAL(triggered()),this,SLOT(sl_About()));
}

void MainWindow::createMenus()
{
    //    
    fileMenu = menuBar()->addMenu(tr("File"));
    fileMenu->addAction(fileNewAction);
    fileMenu->addAction(fileOpenAction);
    fileMenu->addAction(fileSaveAction);
    fileMenu->addSeparator();
    fileMenu->addAction(exitAction);

    //    
    editMenu = menuBar()->addMenu(tr("Edit"));
    editMenu->addAction(copyAction);
    editMenu->addAction(cutAction);
    editMenu->addAction(pasteAction);

    //    
    aboutMenu = menuBar()->addMenu(tr("Help"));
    aboutMenu->addAction(aboutAction);
}

void MainWindow::createToolBars()
{
    //     
    fileTool = addToolBar("File");
    fileTool->addAction(fileNewAction);
    fileTool->addAction(fileOpenAction);
    fileTool->addAction(fileSaveAction);
    fileTool->setAllowedAreas(Qt::TopToolBarArea | Qt::LeftToolBarArea);
    fileTool->setMovable(false);

    //     
    editTool = addToolBar("Edit");
    //addToolBar(Qt::RightToolBarArea,editTool);
    //editTool->setMovable(true);
    //editTool->setAllowedAreas(Qt::RightToolBarArea);
    //editTool->setFloatable(true);
    //QSize size(16,15);
    //editTool->setIconSize(size);
    editTool->addAction(copyAction);
    editTool->addAction(cutAction);
    editTool->addAction(pasteAction);
}

void MainWindow::createStatusBar()
{
    tipLabel=new QLabel(tr("ready"));
    tipLabel->setAlignment(Qt::AlignHCenter);
    tipLabel->setMinimumSize(tipLabel->sizeHint());
    statusBar()->addWidget(tipLabel);
}

void MainWindow::sl_NewFile()
{
    MainWindow *newWin = new MainWindow;
    newWin->show();
}

void MainWindow::sl_OpenFile()
{
    QString fileName = QFileDialog::getOpenFileName(this);
    if(!fileName.isEmpty())
    {
        if(edtText->document()->isEmpty())
        {
            loadFile(fileName);
        }
        else
        {
            MainWindow *newWin = new MainWindow;
            newWin->show();
            newWin->loadFile(fileName);
        }
    }
}

void MainWindow::sl_SaveFile()
{

}

void MainWindow::sl_About()
{

}

void MainWindow::loadFile(QString fileName)
{
    QFile file(fileName);
    if(file.open(QIODevice::ReadOnly | QIODevice::Text))
    {
        QTextStream textStream(&file);
        while (!textStream.atEnd())
        {
            edtText->append(textStream.readLine());
        }
    }
}


注意:
1、setWindowTitleはQWidgetから継承する方法であり、その原型はvoid setWindowTitle(const QString&)である.QMainWindowはQWidgetから継承されているので、QWidgetの公有方法はQMainWindowにもあります.
2、プログラムで文字列操作が必要な場合はtr()関数をできるだけ使用し、プログラムを複数の言語に翻訳することができる.