qt 5.x 初探(1)

嘛。。为了系统安全课来学一波qt…

现在算是写出了一个可以打开文件,保存文件的记事本。。。

接下来要搞定的事情是。。。如何写一个自定义的事件。。。比如计算个开方之类的。。。

放一波代码好了。。。

 1#include <QAction>
 2#include <QMenuBar>
 3#include <QMessageBox>
 4#include <QStatusBar>
 5#include <QToolBar>
 6#include <QDebug>
 7#include <QTextEdit>
 8#include <QFileDialog>
 9
10#include "mainwindow.h"
1MainWindow::MainWindow(QWidget *parent) :
2    QMainWindow(parent)
3{
  1    openAction = new QAction(QIcon(":/images/file-open"), tr("&Open..."), this);
  2    openAction->setShortcuts(QKeySequence::Open);
  3    openAction->setStatusTip(tr("Open an existing file"));
  4
  5    saveAction = new QAction(QIcon(":/images/file-open"), tr("&Save..."), this);
  6    saveAction->setShortcuts(QKeySequence::Save);
  7    saveAction->setStatusTip(tr("Save a new file"));
  8
  9    QMenu *file = menuBar()->addMenu(tr("&File"));
 10    file->addAction(openAction);
 11    file->addAction(saveAction);
 12
 13    QToolBar *toolBar = addToolBar(tr("&File"));
 14    toolBar->addAction(openAction);
 15    toolBar->addAction(saveAction);
 16
 17    textEdit = new QTextEdit(this);
 18    setCentralWidget(textEdit);
 19
 20    connect(openAction, &QAction::triggered, this, &MainWindow::openFile);
 21    connect(saveAction, &QAction::triggered, this, &MainWindow::saveFile);
 22
 23
 24}
 25
 26MainWindow::~MainWindow()
 27{
 28}
 29
 30
 31void MainWindow::openFile()
 32{
 33    QString path = QFileDialog::getOpenFileName(this,
 34                                                tr("Open File"),
 35                                                ".",
 36                                                tr("Text Files(*.txt)"));
 37    if(!path.isEmpty()) {
 38        QFile file(path);
 39        if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
 40            QMessageBox::warning(this, tr("Read File"),
 41                                 tr("Cannot open file:\n%1").arg(path));
 42            return;
 43        }
 44        QTextStream in(&file);
 45        textEdit->setText(in.readAll());
 46        file.close();
 47    } else {
 48        QMessageBox::warning(this, tr("Path"),
 49                             tr("You did not select any file."));
 50    }
 51}
 52
 53void MainWindow::saveFile()
 54{
 55    QString path = QFileDialog::getSaveFileName(this,
 56                                                tr("Open File"),
 57                                                ".",
 58                                                tr("Text Files(*.txt)"));
 59    if(!path.isEmpty()) {
 60        QFile file(path);
 61        if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
 62            QMessageBox::warning(this, tr("Write File"),
 63                                       tr("Cannot open file:\n%1").arg(path));
 64            return;
 65        }
 66        QTextStream out(&file);
 67        out << textEdit->toPlainText();
 68        file.close();
 69    } else {
 70        QMessageBox::warning(this, tr("Path"),
 71                             tr("You did not select any file."));
 72    }
 73}
 74
 75
 76
 77
 78
 79
 80
 81#include "mainwindow.h"
 82#include <QApplication>
 83#include <QSpinBox>
 84#include <QSlider>
 85#include <QHBoxLayout>
 86
 87// !!! Qt 5
 88
 89int main(int argc, char *argv[])
 90{
 91    QApplication app(argc, argv);
 92
 93    MainWindow win;
 94    win.show();
 95
 96    return app.exec();
 97}
 98
 99
100
101
102
103#ifndef MAINWINDOW_H
104#define MAINWINDOW_H
105
106#include <QMainWindow>
107#include <QTextEdit>
108
109namespace Ui {
110class MainWindow;
111}
1class MainWindow : public QMainWindow
2{
3    Q_OBJECT
4
5public:
6     MainWindow(QWidget *parent = 0);
7    ~MainWindow();
 1private:
 2    Ui::MainWindow *ui;
 3    void openFile();
 4    void saveFile();
 5    QAction *openAction;
 6    QAction *saveAction;
 7    QTextEdit *textEdit;
 8};
 9
10#endif // MAINWINDOW_H

大概看了一下午。。。这东西看起来虽然说完全没难度。。。但是还是很麻烦啊。。。

代码