Skip to main content
  1. Posts/

qt 5.x 初探(1)

·1 min
Note: This article is available in Chinese only. 本文暂无英文版本。 View original

嘛。。为了系统安全课来学一波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"
 11
 12MainWindow::MainWindow(QWidget *parent) :
 13    QMainWindow(parent)
 14{
 15
 16
 17    openAction = new QAction(QIcon(":/images/file-open"), tr("&Open..."), this);
 18    openAction->setShortcuts(QKeySequence::Open);
 19    openAction->setStatusTip(tr("Open an existing file"));
 20
 21    saveAction = new QAction(QIcon(":/images/file-open"), tr("&Save..."), this);
 22    saveAction->setShortcuts(QKeySequence::Save);
 23    saveAction->setStatusTip(tr("Save a new file"));
 24
 25    QMenu *file = menuBar()->addMenu(tr("&File"));
 26    file->addAction(openAction);
 27    file->addAction(saveAction);
 28
 29    QToolBar *toolBar = addToolBar(tr("&File"));
 30    toolBar->addAction(openAction);
 31    toolBar->addAction(saveAction);
 32
 33    textEdit = new QTextEdit(this);
 34    setCentralWidget(textEdit);
 35
 36    connect(openAction, &QAction::triggered, this, &MainWindow::openFile);
 37    connect(saveAction, &QAction::triggered, this, &MainWindow::saveFile);
 38
 39
 40}
 41
 42MainWindow::~MainWindow()
 43{
 44}
 45
 46
 47void MainWindow::openFile()
 48{
 49    QString path = QFileDialog::getOpenFileName(this,
 50                                                tr("Open File"),
 51                                                ".",
 52                                                tr("Text Files(*.txt)"));
 53    if(!path.isEmpty()) {
 54        QFile file(path);
 55        if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
 56            QMessageBox::warning(this, tr("Read File"),
 57                                 tr("Cannot open file:\n%1").arg(path));
 58            return;
 59        }
 60        QTextStream in(&file);
 61        textEdit->setText(in.readAll());
 62        file.close();
 63    } else {
 64        QMessageBox::warning(this, tr("Path"),
 65                             tr("You did not select any file."));
 66    }
 67}
 68
 69void MainWindow::saveFile()
 70{
 71    QString path = QFileDialog::getSaveFileName(this,
 72                                                tr("Open File"),
 73                                                ".",
 74                                                tr("Text Files(*.txt)"));
 75    if(!path.isEmpty()) {
 76        QFile file(path);
 77        if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
 78            QMessageBox::warning(this, tr("Write File"),
 79                                       tr("Cannot open file:\n%1").arg(path));
 80            return;
 81        }
 82        QTextStream out(&file);
 83        out << textEdit->toPlainText();
 84        file.close();
 85    } else {
 86        QMessageBox::warning(this, tr("Path"),
 87                             tr("You did not select any file."));
 88    }
 89}
 90
 91
 92
 93
 94
 95
 96
 97#include "mainwindow.h"
 98#include <QApplication>
 99#include <QSpinBox>
100#include <QSlider>
101#include <QHBoxLayout>
102
103// !!! Qt 5
104
105int main(int argc, char *argv[])
106{
107    QApplication app(argc, argv);
108
109    MainWindow win;
110    win.show();
111
112    return app.exec();
113}
114
115
116
117
118
119#ifndef MAINWINDOW_H
120#define MAINWINDOW_H
121
122#include <QMainWindow>
123#include <QTextEdit>
124
125namespace Ui {
126class MainWindow;
127}
128
129class MainWindow : public QMainWindow
130{
131    Q_OBJECT
132
133public:
134     MainWindow(QWidget *parent = 0);
135    ~MainWindow();
136
137private:
138    Ui::MainWindow *ui;
139    void openFile();
140    void saveFile();
141    QAction *openAction;
142    QAction *saveAction;
143    QTextEdit *textEdit;
144};
145
146#endif // MAINWINDOW_H

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

代码

Related

20170504近况

·2 mins
啊。。在准备考试QAQ 明天约了鹅厂面试。。。然而从四月就开始一直考试考试考试….感觉药丸啊?

20140419

·1 min
最让你恶心痛苦的那些人,你必要从他们身上学到最重要和有用的东西。 所有的痛苦都不能白受,克我者必生我。