1.0
MainWindow.h
#ifndef MAINWINDOW_H #define MAINWINDOW_H #include<QMainWindow> #include<QAction> #include<QMenu> #include<QTextBrowser> class MainWindow : public QMainWindow{ Q_OBJECT public: MainWindow(); private slots: void fNew(); void fClose(); private: void SetupMenus(); void SetupEditor(); QTextBrowser *edit; QAction *newAction; QAction *closeAction; QMenu *file; }; #endif // MAINWINDOW_H
MainWindow.CPP
#include<QtGui> #include"MainWindow.h" MainWindow::MainWindow(){ QWidget *widget=new QWidget; setCentralWidget(widget); /*QWidget *topfiller=new QWidget; topfiller->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding); QWidget *bottomFiller=new QWidget; bottomFiller->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);*/ SetupEditor(); QVBoxLayout *layout=new QVBoxLayout; /*layout->addWidget(topFiller); layout->addWidget(bottomFiller);*/ layout->addWidget(edit); widget->setLayout(layout); widget->setLayout(layout); SetupMenus(); setWindowTitle(tr("Main Window")); setMinimumSize(256,256); resize(512,480); } void MainWindow::SetupMenus(){ newAction=new QAction(tr("&New"),this); newAction->setShortcut(QKeySequence::New); connect(newAction,SIGNAL(triggered()),this,SLOT(fNew())); closeAction=new QAction(tr("E&xit"),this); closeAction->setShortcut(QKeySequence::Close); connect(closeAction,SIGNAL(triggered()),this,SLOT(fClose())); file=menuBar()->addMenu(tr("&File")); file->addAction(newAction); file->addAction(closeAction); } void MainWindow::fNew(){ edit->clear(); } void MainWindow::fClose(){ this->close(); } void MainWindow::SetupEditor(){ edit=new QTextBrowser; edit->setAcceptRichText(true); edit->setAutoFormatting(QTextBrowser::AutoNone); edit->setCursorWidth(1); edit->setDocumentTitle(tr("new Document")); edit->setCursorWidth(16); edit->setReadOnly(false); edit->setEnabled(true); }
Main.CPP
#include<QApplication> #include"MainWindow.h" int main(int argc,char* argv[]){ QApplication app(argc,argv); MainWindow wnd; wnd.show(); app.exec(); }
2.0
MainWindow.h
#ifndef MAINWINDOW_H #define MAINWINDOW_H #include<QMainWindow> #include<QAction> #include<QMenu> #include<QTextBrowser> #include<QPushButton> class QHBoxLayout; class MainWindow : public QMainWindow{ Q_OBJECT public: MainWindow(); private slots: void fNew(); void fClose(); private: void SetupMenus(); void SetupEditor(); QTextBrowser *edit; QPushButton *newBtn; QPushButton *exit; QAction *newAction; QAction *closeAction; QMenu *file; QHBoxLayout *btnBox; }; #endif // MAINWINDOW_H
MainWindow.CPP
#include<QtGui> #include"MainWindow.h" MainWindow::MainWindow(){ QWidget *widget=new QWidget; setCentralWidget(widget); SetupEditor(); QVBoxLayout *layout=new QVBoxLayout; layout->addLayout(btnBox); layout->addWidget(edit); widget->setLayout(layout); widget->setLayout(layout); SetupMenus(); setWindowTitle(tr("Main Window")); setMinimumSize(256,256); resize(512,480); } void MainWindow::SetupMenus(){ newAction=new QAction(tr("&New"),this); newAction->setShortcut(QKeySequence::New); connect(newAction,SIGNAL(triggered()),this,SLOT(fNew())); closeAction=new QAction(tr("E&xit"),this); closeAction->setShortcut(QKeySequence::Close); connect(closeAction,SIGNAL(triggered()),this,SLOT(fClose())); file=menuBar()->addMenu(tr("&File")); file->addAction(newAction); file->addAction(closeAction); } void MainWindow::fNew(){ edit->clear(); } void MainWindow::fClose(){ this->close(); } void MainWindow::SetupEditor(){ newBtn=new QPushButton(tr("new"),this); newBtn->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Fixed); connect(newBtn,SIGNAL(clicked()),this,SLOT(fNew())); exit=new QPushButton(tr("exit"),this); exit->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Fixed); connect(exit,SIGNAL(clicked()),this,SLOT(fClose())); edit=new QTextBrowser; edit->setAcceptRichText(true); edit->setAutoFormatting(QTextBrowser::AutoNone); edit->setCursorWidth(1); edit->setDocumentTitle(tr("new Document")); edit->setCursorWidth(16); edit->setReadOnly(false); edit->setEnabled(true); btnBox=new QHBoxLayout; btnBox->addWidget(newBtn); btnBox->addWidget(exit); }
Main.cpp
#include<QApplication> #include"MainWindow.h" int main(int argc,char* argv[]){ QApplication app(argc,argv); MainWindow wnd; wnd.show(); app.exec(); }