创建项目时,选择qmainwindow
菜单栏
#include <QMenuBar> //菜单栏管理类
#include <QMenu> //菜单管理类
#include <QAction> //菜单项管理类
相关信号:
triggered 点击菜单管理项触发
connect(newAct, &QAction::triggered, this, &MainWindow::newDock);
获取默认的菜单栏首地址:
menuBar()
添加菜单:
addMenu()
QMenu *file = menuBar()->addMenu("文件");
在菜单中添加子菜单
QMenu * cmenu=file->addMenu(QIcon(),"最近打开的"); cmenu指针接收子菜单首地址,方便子菜单添加菜单项
添加菜单项:
addAction()
QAction *newAct = file->addAction("新建");
设置快捷方式:
setShortcut()
newAct->setShortcut(QKeySequence("ctrl+n"));
openAct->setShortcut(QKeySequence(QKeySequence::Open))
openAct->setShortcut(Qt::CTRL + Qt::Key_O);
工具栏
有ui界面的工具栏
ui->toolBar() 可以直接使用
添加新的工具栏:
addToolBar()
QToolBar *tool = this->addToolBar(".....");
设置允许的区域
setAllowedAreas()
tool->setAllowedAreas(Qt::TopToolBarArea);
工具栏添加分割符
addSeparator()
tool->addSeparator();
工具栏添加菜单项:
addAction()
tool->addAction(new QAction("关闭"));
工具栏添加控件:
addWidget()
tool->addWidget(lab);
状态栏
悬停在其他栏的菜单项或控件上可以在状态栏显示状态
setStatusTip()
newAct->setStatusTip("新建....");
获取默认的状态栏
statusBar()
QStatusBar *status = statusBar();
状态栏能显示临时消息,设置持续时长
showMessage()
statusBar()->showMessage("......", 5000);
工作区
#include <QDockWidget>
#include <QTextEdit>
相关信号:
visibilityChanged 点击工作区项目进行触发
connect(dock[dock.size()-1], &QDockWidget::visibilityChanged, this, &MainWindow::floatingDock);
------------------------------------------------------------------------------------------------------------------------
相关函数:
tabifyDockWidget()
void QMainWindow::tabifyDockWidget(QDockWidget *first, QDockWidget *second)
函数用于将两个QDockWidget对象以Tab页的形式分组。
这意味着当调用该函数时,第一个QDockWidget会成为Tab页的主要内容,
而第二个QDockWidget会作为一个新的Tab页添加到主要的QDockWidget上。
tabifyDockWidget(dock[dock.size()-1 - 1], dock[dock.size() - 1]);
------------------------------------------------------------------------------------------------------------------------
在工作区域创建文档编辑器:
QList<QDockWidget *> dock;
QList<QTextEdit *> tdt;
1.创建新的dock,dock是用链表的方式,不停新建就不停追加
dock.append(new QDockWidget(QString("新文件 %1").arg(dock.size() + 1)));
dock[dock.size()-1]->setMinimumWidth(width());
2.创建新的textEdit,也用链表的方式,不停新建就不停追加
tdt.append(new QTextEdit);
tdt[tdt.size()-1]->setMinimumWidth(width());
3.将textEdit加入到dock中
dock[dock.size()-1]->setWidget(tdt[tdt.size()-1]);
4.将工作区显示在界面上
addDockWidget(Qt::LeftDockWidgetArea, dock[dock.size()-1]);
工作区域 添加的控件
------------------------------------------------------------------------------------------------------------------------
设置工作区的堆叠:
调换两个tab页,第二个在第一个之上:
if(dock.size() > 1)
{
tabifyDockWidget(dock[dock.size()-1 - 1], dock[dock.size() - 1]);
}
------------------------------------------------------------------------------------------------------------------------
获取当前tab页的索引:
信号绑定:绑定在dock的编辑的函数里,而不是绑定在构造函数
connect(dock[dock.size()-1], &QDockWidget::visibilityChanged, this, &MainWindow::floatingDock);
槽函数:
void MainWindow::floatingDock(bool vis)
{
if(vis)
{
QDockWidget *doc = static_cast<QDockWidget *>(sender()); //得到触发信号的工作区
for(int i = 0; i < dock.size(); i++)
{
if( doc == dock[i])
{
currentIndex = i; //获取当前tab页的索引 可以在*.h中的成员中int currentIndex;定义成全局变量
qDebug() << "currentIndex: " << currentIndex;
}
}
}
}
QFile
#include <QFile>
#include <QFileDialog>
打开文件:
QFile file(fileName);
file.open(QFile::ReadWrite | QFile::Text) //读写的方式打开,并且要使用text
file.open(QFile::WriteOnly | QFile::Text | QFile::Truncate)
//只写的方式打开,要对text进行操作,有内容则清空
读取文件所有内容:
file.readAll();
读取文件一行内容:
file.readLine();
写入内容:
file.write();
关闭文件:
file.close();
---------------------------------------------------------------------------------------------------------
成员区:
private:
QList<QString > fileName;
private slots:
void openFile();
void saveFile();
绑定槽函数:
connect(openAct, &QAction::triggered, this, &MainWindow::openFile);
connect(saveAct, &QAction::triggered, this, &MainWindow::saveFile);
--------------------------------------------------------------------------------------------------------
字符串的分割: (可以来分割带有路径名的文件名)
split();
QStringList list = fileName.split("/")
判断是否包含某个字符串
contains():
dock[currentIndex]->windowTitle().contains("新文件"); //判断文件标题是否包含“新文件”
获取文本框的内容:
toPlainText();
将 QString转换为 QByteArray类型
toLatin1();
file.write(tdt[currentIndex]->toPlainText().toLatin1());
//获取一个文本编辑框的内容,并将内容转换为QByteArray类型,最后写入文件中
---------------------------------------------------------------------------------------------------------
void MainWindow::openFile()
{
if(dock.size() == 0){
newDock();
currentIndex = 0;
}
QString fileName = QFileDialog::getOpenFileName();
if(fileName.isEmpty()){
return;
}
// QFile file(fileName);
QFile file;
file.setFileName(fileName);
if( !file.open(QFile::ReadWrite | QFile::Text)){
QMessageBox::warning(this, "警告", "文件打开失败");
return;
}
tdt[currentIndex]->setText( file.readAll() );
file.close();
// split(): 字符串的分割
QStringList list = fileName.split("/");
qDebug() << "fileName: " << fileName << list[list.size()-1];
dock[currentIndex]->setWindowTitle(list[list.size()-1]);
this->fileName[currentIndex] = fileName;
}
---------------------------------------------------------------------------------------------------------
void MainWindow::saveFile()
{
// contains(): 判断是否包含某个字符串
if( dock[currentIndex]->windowTitle().contains("新文件")){
QString fileName = QFileDialog::getSaveFileName();
QFile file(fileName);
if(!file.open(QFile::WriteOnly | QFile::Text | QFile::Truncate)){
QMessageBox::warning(this, "警告", "文件保存失败");
return;
}
// toPlainText():获取文本框的内容。 toLatin1():将 QString转换为 QByteArray类型
file.write(tdt[currentIndex]->toPlainText().toLatin1());
file.close();
return;
}
QFile file(fileName[currentIndex]);
if(!file.open(QFile::WriteOnly | QFile::Text | QFile::Truncate)){
QMessageBox::warning(this, "警告", "文件保存失败");
return;
}
// toPlainText():获取文本框的内容。 toLatin1():将 QString转换为 QByteArray类型
file.write(tdt[currentIndex]->toPlainText().toLatin1());
file.close();
}
eg:写一个简易的notepad,可以新建、保存、打开文档
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QMenuBar> //菜单栏管理类
#include <QMenu> //菜单管理类
#include <QAction> //菜单项管理类
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>
#include <QDockWidget>
#include <QTextEdit>
#include <QFile>
#include <QFileDialog>
#include <QMessageBox>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private slots:
void newDock();
void openFile();
void saveFile();
void floatingDock(bool vis);
private:
Ui::MainWindow *ui;
QList<QDockWidget *> dock;
QList<QTextEdit *> tdt;
QList<QString > fileName;
int currentIndex;
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
setMinimumSize(640, 480);
/**************************************菜单栏*******************************************/
// menuBar():获取默认的菜单栏首地址 addMenu(): 添加菜单
QMenu *file = menuBar()->addMenu("文件");
// addAction(): 添加菜单项
QAction *newAct = file->addAction("新建");
newAct->setIcon(QIcon(":/img/newfile.png"));
// setShortcut(): 设置快捷方式
newAct->setShortcut(QKeySequence("ctrl+n"));
newAct->setStatusTip("新建....");
QAction *openAct = file->addAction(QIcon(":/img/openfile.png"), "打开");
// openAct->setShortcut(QKeySequence(QKeySequence::Open));
openAct->setShortcut(Qt::CTRL + Qt::Key_O);
openAct->setStatusTip("打开....");
QAction *saveAct = file->addAction(QIcon(":/img/saveasfile.png"), "保存");
// openAct->setShortcut(QKeySequence(QKeySequence::Open));
openAct->setShortcut(Qt::CTRL + Qt::Key_S);
openAct->setStatusTip("保存....");
// addMenu(): 添加菜单
QMenu *cmenu = file->addMenu(QIcon(), "最近打开的");
cmenu->addAction("1.cpp");
/**************************************工具栏*******************************************/
ui->toolBar->addAction(newAct);
ui->toolBar->addAction(openAct);
ui->toolBar->addAction(saveAct);
// addToolBar(): 添加新的工具栏
QToolBar *tool = this->addToolBar(".....");
// setAllowedAreas(): 设置允许的区域
tool->setAllowedAreas(Qt::TopToolBarArea);
tool->addAction(new QAction("关闭"));
QLabel *lab = new QLabel("页码");
QLineEdit *edt = new QLineEdit;
QPushButton *btn = new QPushButton("跳转");
tool->addSeparator();
tool->addWidget(lab);
tool->addWidget(edt);
tool->addWidget(btn);
/**************************************状态栏*******************************************/
// statusBar():获取默认的状态栏
// showMessage(): 状态栏能显示临时消息,设置持续时长
// statusBar()->showMessage("......", 5000);
QStatusBar *status = statusBar();
QLabel *lab1 = new QLabel("C++ source file");
lab1->setMinimumWidth(width()/2);
status->addWidget(lab1);
QLabel *lab2 = new QLabel("Ln: 12 Col: 23 Pos: 46");
status->addWidget(lab2);
/**************************************工作区*******************************************/
connect(newAct, &QAction::triggered, this, &MainWindow::newDock);
connect(openAct, &QAction::triggered, this, &MainWindow::openFile);
connect(saveAct, &QAction::triggered, this, &MainWindow::saveFile);
}
MainWindow::~MainWindow()
{
delete ui;
}
#include <QDebug>
void MainWindow::newDock()
{
dock.append(new QDockWidget(QString("新文件 %1").arg(dock.size() + 1)));
dock[dock.size()-1]->setMinimumWidth(width());
tdt.append(new QTextEdit);
tdt[tdt.size()-1]->setMinimumWidth(width());
fileName.append("");
dock[dock.size()-1]->setWidget(tdt[tdt.size()-1]);
addDockWidget(Qt::LeftDockWidgetArea, dock[dock.size()-1]);
if(dock.size() > 1){
tabifyDockWidget(dock[dock.size()-1 - 1], dock[dock.size() - 1]);
}
connect(dock[dock.size()-1], &QDockWidget::visibilityChanged, this, &MainWindow::floatingDock);
connect(dock[dock.size()-1], &QDockWidget::destroyed, this, [&](){
qDebug() << "des";
});
}
void MainWindow::openFile()
{
if(dock.size() == 0){
newDock();
currentIndex = 0;
}
QString fileName = QFileDialog::getOpenFileName();
if(fileName.isEmpty()){
return;
}
// QFile file(fileName);
QFile file;
file.setFileName(fileName);
if( !file.open(QFile::ReadWrite | QFile::Text)){
QMessageBox::warning(this, "警告", "文件打开失败");
return;
}
tdt[currentIndex]->setText( file.readAll() );
file.close();
// split(): 字符串的分割
QStringList list = fileName.split("/");
qDebug() << "fileName: " << fileName << list[list.size()-1];
dock[currentIndex]->setWindowTitle(list[list.size()-1]);
this->fileName[currentIndex] = fileName;
}
void MainWindow::saveFile()
{
// contains(): 判断是否包含某个字符串
if( dock[currentIndex]->windowTitle().contains("新文件")){
QString fileName = QFileDialog::getSaveFileName();
QFile file(fileName);
if(!file.open(QFile::WriteOnly | QFile::Text | QFile::Truncate)){
QMessageBox::warning(this, "警告", "文件保存失败");
return;
}
// toPlainText():获取文本框的内容。 toLatin1():将 QString转换为 QByteArray类型
file.write(tdt[currentIndex]->toPlainText().toLatin1());
file.close();
return;
}
QFile file(fileName[currentIndex]);
if(!file.open(QFile::WriteOnly | QFile::Text | QFile::Truncate)){
QMessageBox::warning(this, "警告", "文件保存失败");
return;
}
// toPlainText():获取文本框的内容。 toLatin1():将 QString转换为 QByteArray类型
file.write(tdt[currentIndex]->toPlainText().toLatin1());
file.close();
}
void MainWindow::floatingDock(bool vis)
{
if(vis){
QDockWidget *doc = static_cast<QDockWidget *>(sender());
for(int i = 0; i < dock.size(); i++){
if( doc == dock[i]){
currentIndex = i;
qDebug() << "currentIndex: " << currentIndex;
}
}
}
}