Qt5 学习之路及嵌入式开发教程14:Qt5主窗口---界面设置
这次任务要完成Qt5主窗口的界面设计
Qt5主窗口布局及基本元素
基本元素
QMainWindow是一个为用户提供主窗口程序的类,包含一个菜单栏(menu bar)、多个工具栏(tool bars)、多个锚接部件(dock widgets)及一个中心部件(central widget),是许多应用程序的基础,如文本编辑器、图片编辑器等。
基本元素
菜单栏
菜单是一系列命令的列表。为了实现菜单、工具栏按钮、键盘快捷方式等命令的一致性,Qt使用动作(Action)来表示这些命令。Qt的菜单就是由一系列的QAction动作对象构成的列表,而菜单栏则是包容菜单的面板,它位于主窗口顶部、主窗口标题栏的下面。一个主窗口最多只有一个菜单栏。
状态栏
状态栏通常显示GUI应用程序的一些状态信息,它位于主窗口的底部。用户可以在状态栏上添加、使用Qt窗口部件。一个主窗口最多只能有一个状态栏。
工具栏
工具栏是由一系列的类似于按钮的动作排列而成的面板,它通常由一些经常使用的命令(动作)组成。工具栏位于菜单栏的下面、状态栏的上面,可以停靠在主窗口的上、下、左、右四个方向上。一个主窗口可以包含多个工具栏。
锚接部件
锚接部件作为一个容器使用,以包容其他窗口部件来实现某些功能。例如:Qt设计器的属性编辑器、对象监视器等都是由锚接部件包容其他的Qt窗口部件来实现的。它位于工具栏区的内部,可以作为一个窗口自由浮动在主窗口上面,也可以像工具栏一样停靠在主窗口的上、下、左、右四个方向上。一个主窗口可以包含多个锚接部件。
中心部件
中心部件处于锚接部件区的内部、主窗口的中心。一个主窗口只有一个中心部件。
注:主窗口QMainWindow具有自己的布局管理器,因此在QMainWindow窗口上设置布局管理器或者创建一个父窗口部件作为QMainWindow的布局管理器都是不允许的。但可以在主窗口的中心部件上设置管理器。
一、主窗口的界面设计
1、点击新建文件,一直下一步,到界面设置
下一步后,完成
2、设置“文件”主菜单,子菜单:新建、打开、保存、打印、退出
各部分属性:
属性设置:
新建
打开:
保存:
打印:
退出:
其中工具栏设置:
3、各部分功能实现
在头文件中加入:
在mainwindow.cpp中添加代码
#include <QFileDialog>
#include <QTextStream>
#include <QFile>
#include <QtPrintSupport/QPrinter>
#include <QtPrintSupport/QPrintDialog>
#include <QMessageBox>
connect(ui->NewFileAction, SIGNAL(triggered(bool)),this,SLOT(ShowNewFile()));
connect(ui->OpenFileAction, SIGNAL(triggered(bool)),this,SLOT(ShowOpenFile()));
connect(ui->SaveFileAction, SIGNAL(triggered(bool)),this,SLOT(ShowSaveFile()));
connect(ui->PrintAction, SIGNAL(triggered(bool)),this,SLOT(ShowPrintFile()));
connect(ui->exitAction, SIGNAL(triggered(bool)),this,SLOT(ShowExitFile()));
接着实现功能
新建
void MainWindow::ShowNewFile()
{
MainWindow *newMainWindow = new MainWindow;
newMainWindow->show();
}
打开:
void MainWindow::loadFile(QString filename)
{
QByteArray cpath = filename.toLocal8Bit();
printf("file name:%s\n",cpath.data());
QFile file(filename);
if ( file.open(QIODevice::ReadOnly | QIODevice::Text ))
{
QTextStream textStream(&file);
while ( !textStream.atEnd() )
{
ui->textEdit->append(textStream.readLine());
printf("read line\n");
}
printf("end\n");
}
}
void MainWindow::ShowOpenFile()
{
fileName = QFileDialog::getOpenFileName(this);
if ( !fileName.isEmpty() )
{
if ( ui->textEdit->document()->isEmpty() )
{
loadFile(fileName);
}
else
{
MainWindow *newmainWindow = new MainWindow;
newmainWindow->show();
newmainWindow->loadFile(fileName);
}
}
}
保存功能:
void MainWindow::ShowSaveFile()
{
QString path = QFileDialog::getSaveFileName(this,
tr("打开文件"),
".",
tr("文本文件(*.txt)"));
if(!path.isEmpty()){
QFile file(path);
if(!file.open(QIODevice::WriteOnly | QIODevice::Text)){
QMessageBox::warning(this, tr("保存文件"),
tr("不能打开文件:\n%1").arg(path));
return;
}
QTextStream out(&file);
out << ui->textEdit->toPlainText();
file.close();
} else {
QMessageBox::warning(this, tr("Path"),
tr("您没有选择任何文件."));
}
}
打印功能:
pro 文件里面 一定写入 QT+= printsupport (特别重要)
void MainWindow::ShowPrintFile()
{
QPrinter printer(QPrinter::HighResolution);
QPrintDialog printDialog(&printer,this);
if ( printDialog.exec() )
{
QTextDocument *doc = ui->textEdit->document();
doc->print(&printer);
}
}
退出功能
void MainWindow::ShowExitFile()
{
this->close();
}
运行效果图