跳过正文
  1. Posts/

qt 5.x 初探(1)

·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"
 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

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

代码

相关文章

c++11 学习笔记

·1 分钟
昨天终于搞定了ycm对c++11的支持…. 嘛,17都快出来了,我竟然连11都不会用。

c语言中static的作用

·1 分钟
一般有两个 1static int a; 2int b; 3void func(void) 4{ 5 static int c=0; 6 int d; 7} 在这里,a与b都是全局变量,二者的区别是,b可以被别的文件使用,a只能在本文件中使用,这是static对全局变量的作用。 ** c和d的区别是,d是一个自动变量,func函数执行完后,d会自动被释放。但c却不会被释放,下一次调用func函数时,c的值会保留上次的值继续使用(而不是初始值0,初始化只会在函数第一次被调用的时候执行)**