手动写QT界面实例 不依赖ui文件

例子是个小技术本
例子*********************************************************mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
 
#include <QMainWindow>
#include <QMenu>
#include <QMenuBar>
#include <QTextEdit>
 
/*
namespace Ui {
class MainWindow;
}
*/
 
class MainWindow : public QMainWindow
{
    Q_OBJECT
 
public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();
    void setupUi();
 
private:
    QString m_filePath;
    QMenuBar *m_menuBar;
    QMenu *m_fileMenu;
    QTextEdit *textEdit;
    //Ui::MainWindow *ui;
public slots:
    void openFileSlot();
    void saveFileSlot();
    void newFileSlot();
    bool saveAsSlot();
};
 
#endif // MAINWINDOW_H



****************************************************************mainwindow.cpp
#include "mainwindow.h"
#include <QMenuBar>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QFileDialog>
#include <QDebug>
#include "GLDTypes.h"
#include <QMessageBox>
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent)
{
    setupUi();
}
void MainWindow::setupUi()
{
    this->resize(600,600);
    m_fileMenu = new QMenu("&File(F)",this);
    //向menu中加入Action
    QAction *newFile = new QAction("&New File",this);
    newFile->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_N));//给action 设置快捷键
    m_fileMenu->addAction(newFile);
    QAction *openFile = new QAction("&Open File",this);
    openFile->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_O));//给action 设置快捷键
    m_fileMenu->addAction(openFile);
    QAction *saveFile = new QAction("&Save File",this);
    saveFile->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_S));//给action 设置快捷键
    m_fileMenu->addAction(saveFile);
    QAction *saveFileAs = new QAction("&Save File A",this);
    saveFileAs->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_S));//给action 设置快捷键
    m_fileMenu->addAction(saveFileAs);
    //向menuBar中加入Menu
    m_menuBar = menuBar();
    m_menuBar->addMenu(m_fileMenu);
    //向menuBar中直接加入Action
    m_menuBar->addAction(new QAction("Close File",this));
    connect(newFile,&QAction::triggered,this,&MainWindow::newFileSlot);
    connect(openFile,&QAction::triggered,this,&MainWindow::openFileSlot);
    connect(saveFile,&QAction::triggered,this,&MainWindow::saveFileSlot);
    connect(saveFileAs,&QAction::triggered,this,&MainWindow::saveAsSlot);
    //new一个水平布局,在水平居中加入一个textedit
    QHBoxLayout *mainHbox = new QHBoxLayout(this);
    textEdit = new QTextEdit(this);
    mainHbox->addWidget(textEdit);
    //新建一个widget
    QWidget *widget = new QWidget(this);
    //将水平布局加入到widget中
    widget->setLayout(mainHbox);
    //设置中心窗体为widget
    setCentralWidget(widget);
}
void MainWindow::openFileSlot()
{
    QString filePath = QFileDialog::getOpenFileName(
                this, tr("Open File"), "", tr("File (*.txt)"));
    //ui->filePathLineEdit->setText(filePath);
    m_filePath=filePath;
    QFile file(filePath);
    file.open(QIODevice::ReadOnly | QIODevice::Text);//以只读方式打开文件
    QTextStream in(&file);
    //in.setCodec("UTF-8");
    QString content = in.readAll();
    textEdit->setPlainText(content);
    //设置标题
    QFileInfo fi(filePath);
    QString name=fi.fileName();
    this->setWindowTitle(tr("%1[*]").arg(name));
    file.close();
}
void MainWindow::saveFileSlot()
{
    QString content;
    content = textEdit->toPlainText();
    QFile file(m_filePath);
    file.open(QIODevice::WriteOnly);
    file.write(content.toLocal8Bit());
    //QTextStream writefiletext(&file);
    //content = QString::fromLocal8Bit(content);
    //writefiletext<<content.toLocal8Bit();
    file.close();
    qDebug()<<content;
}
void MainWindow::newFileSlot()
{
    QTextDocument *document=textEdit->document();
    int ret=QMessageBox::question(this, "Save?",
                                  "disSave, Save",
                                  QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel);
    switch(ret)
    {
    case QMessageBox::Cancel:
        break;
    case QMessageBox::Discard:
        textEdit->setText("");
        this->setWindowTitle(tr("%1[*]").arg(""));
        break;
    case QMessageBox::Save:
        textEdit->setText("");
        this->setWindowTitle(tr("%1[*]").arg(""));
        break;
    }
}
bool MainWindow::saveAsSlot()
{
    QString fileName = QFileDialog::getSaveFileName(this,"as file");
    if(fileName.isEmpty())
       {
        return false;
    }
    QString content;
    content = textEdit->toPlainText();
    QFile file(fileName);
    file.open(QIODevice::WriteOnly);
    file.write(content.toLocal8Bit());
    file.close();
    return true;
}
MainWindow::~MainWindow()
{
    //delete ui;
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值