1. 重点内容
- 新建一个QTabWidget多页风格主窗口。
- 在主窗口中查看系统日志。
- 在主窗口中打开一个MainWindow窗口。
2. 运行展示
3. 程序示例
3.1 创建一个Qt peanutlib项目
创建一个项目,请参考“00u-00-project-建立项目基本开发环境(一)-建立一个peanutlib的项目”,“00u-01-project-建立项目基本开发环境(二)-创建启动及销毁一个peanutlib项目”,注意项目命名规则,除了main.cpp文件外,其它文件与类要加前缀名ps。
3.2 新建一个输出Hello World字符串的MainWindow窗口
使用Qt Creator创建一个Main Window风格的窗口,按照命名规则,设定类名PsHelloworldMainWindow,程序名称ps_helloworld_mainwindow.h,ps_helloworld_mainwindow.cppps_helloworld_mainwindow.ui
下面是设计器创建的UI
- 头文件ps_helloworld_mainwindow.h
/***************************************************************************
* This file is part of the Peanut Library project *
* Copyright (C) 2022 by Wang Ren Qian *
* author:积木虎 154318869@qq.com *
* Peanut Software Studio *
****************************************************************************/
#ifndef PS_HELLOWORLD_MAINWINDOW_H
#define PS_HELLOWORLD_MAINWINDOW_H
#include <QMainWindow>
namespace peanut {
namespace Ui {
class PsHelloworldMainWindow;
}
class PsHelloworldMainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit PsHelloworldMainWindow(QWidget *parent = nullptr);
~PsHelloworldMainWindow();
private:
Ui::PsHelloworldMainWindow *ui;
};
}
#endif // PS_HELLOWORLD_MAINWINDOW_H
- 程序文件ps_helloworld_mainwindow.cpp
/***************************************************************************
* This file is part of the Peanut Library project *
* Copyright (C) 2022 by Wang Ren Qian *
* author:积木虎 154318869@qq.com *
* Peanut Software Studio *
****************************************************************************/
#include "ps_helloworld_mainwindow.h"
#include "ui_ps_helloworld_mainwindow.h"
namespace peanut {
PsHelloworldMainWindow::PsHelloworldMainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::PsHelloworldMainWindow)
{
ui->setupUi(this);
}
PsHelloworldMainWindow::~PsHelloworldMainWindow()
{
delete ui;
}
}
- 设计UI文件ps_helloworld_mainwindow.ui
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>peanut::PsHelloworldMainWindow</class>
<widget class="QMainWindow" name="peanut::PsHelloworldMainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>800</width>
<height>600</height>
</rect>
</property>
<property name="windowTitle">
<string>HelloWorld窗口</string>
</property>
<widget class="QWidget" name="centralwidget">
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QLabel" name="label">
<property name="font">
<font>
<family>宋体</family>
<pointsize>24</pointsize>
</font>
</property>
<property name="text">
<string>Hello World!</string>
</property>
<property name="alignment">
<set>Qt::AlignCenter</set>
</property>
</widget>
</item>
</layout>
</widget>
<widget class="QMenuBar" name="menubar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>800</width>
<height>23</height>
</rect>
</property>
</widget>
<widget class="QStatusBar" name="statusbar"/>
</widget>
<resources/>
<connections/>
</ui>
3.3 新建QTabWidget多页风格主窗口,并查看日志、打开Hello World窗口
- 设计UI文件ps_mainwindow.ui
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>peanut::PsMainWindow</class>
<widget class="QMainWindow" name="peanut::PsMainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>800</width>
<height>600</height>
</rect>
</property>
<property name="windowTitle">
<string>psample00u0 - 积木虎 154318869@qq.com </string>
</property>
<widget class="QWidget" name="centralwidget">
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QTabWidget" name="tabWidget">
<property name="currentIndex">
<number>-1</number>
</property>
</widget>
</item>
</layout>
</widget>
<widget class="QMenuBar" name="menubar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>800</width>
<height>23</height>
</rect>
</property>
<widget class="QMenu" name="menu">
<property name="title">
<string>示例</string>
</property>
<addaction name="act_PlLogForm"/>
<addaction name="act_PsHelloworldMainWindow"/>
</widget>
<addaction name="menu"/>
</widget>
<widget class="QStatusBar" name="statusbar"/>
<action name="act_PlLogForm">
<property name="text">
<string>系统运行日志</string>
</property>
</action>
<action name="act_PsHelloworldMainWindow">
<property name="text">
<string>Hello World窗口</string>
</property>
</action>
</widget>
<resources/>
<connections/>
</ui>
- 头文件ps_mainwindow.h
/***************************************************************************
* This file is part of the Peanut Library project *
* Copyright (C) 2022 by Wang Ren Qian *
* author:积木虎 154318869@qq.com *
* Peanut Software Studio *
****************************************************************************/
#ifndef PSMAINWINDOW_H
#define PSMAINWINDOW_H
#include <QMainWindow>
#include "pl_lib_global.h"
#include "log/pl_log_form.h"
namespace peanut {
QT_BEGIN_NAMESPACE
namespace Ui { class PsMainWindow; }
QT_END_NAMESPACE
//多页QTabWidget风格的主窗口类
class PsMainWindow : public QMainWindow
{
Q_OBJECT
public:
PsMainWindow(QWidget *parent = nullptr);
~PsMainWindow();
public Q_SLOTS:
bool close(); //关闭
protected:
virtual void closeEvent(QCloseEvent *event); //关闭事件触发
private slots:
void on_trigerMenu(QAction *action); //菜单单击信号发出后,对应的槽方法
void on_tabWidget_tabCloseRequested(int index); //QTabWidget的tab页关闭信号发出后,对应的槽方法
void on_tabWidget_tabBarDoubleClicked(int index); //QTabWidget的tab页栏双击信号发出后,对应的槽方法
private:
Ui::PsMainWindow *ui;
//声明系统运行日志输出窗口指针
PlLogForm *m_logForm = nullptr;
};
}
#endif // PSMAINWINDOW_H
- 程序文件ps_mainwindow.cpp
/***************************************************************************
* This file is part of the Peanut Library project *
* Copyright (C) 2022 by Wang Ren Qian *
* author:积木虎 154318869@qq.com *
* Peanut Software Studio *
****************************************************************************/
#include "ps_mainwindow.h"
#include "ui_ps_mainwindow.h"
#include "pl_form.h"
#include "ps_application.h"
#include "ps_helloworld_mainwindow.h"
namespace peanut {
PsMainWindow::PsMainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::PsMainWindow)
{
ui->setupUi(this);
//取Application对象
PsApplication *app = PsApplication::getPsApplication();
Q_ASSERT(app!=nullptr);
this->setWindowIcon(app->getMainIcon());
ui->tabWidget->setVisible(false);
ui->tabWidget->clear();
ui->tabWidget->tabsClosable();
ui->tabWidget->setTabsClosable(true);
if(this->m_logForm==nullptr) {
//取系统日志对象
m_logForm = app->getGlobalLogForm();
//设置该对象的父对象是本对象。注:本对象析构时会自动析构m_logForm
m_logForm->setParent(this);
}
//绑定ui->menubar的单击信号triggered方法与槽方法on_trigerMenu的关系
connect(ui->menubar,SIGNAL(triggered(QAction*)),this,SLOT(on_trigerMenu(QAction*)));
}
PsMainWindow::~PsMainWindow()
{
delete ui;
}
bool PsMainWindow::close()
{
int ret = PlMessageBox::question(this, "消息", tr("您是否确定退出系统\n"),
QMessageBox::Ok,QMessageBox::Cancel);
if(QMessageBox::Ok == ret) {
qApp->quit(); //退出整个应用程序
}
else {
return false;
}
return true;
}
void PsMainWindow::closeEvent(QCloseEvent *event)
{
Q_UNUSED(event);
if(!this->close()) {
event->ignore();
}
}
void PsMainWindow::on_trigerMenu(QAction *action)
{
PsApplication *app = PsApplication::getPsApplication();
if(action != nullptr) {
QIcon icon;
int cur;
//得到菜单ui->menubar当前响应的单击的action对象,并得到它的对象名称
QString actName = action->objectName();
if(actName=="act_PlLogForm") { //识别是act_PlLogForm对象
if(this->m_logForm==nullptr) {
m_logForm = app->getGlobalLogForm();
}
icon = m_logForm->windowIcon();
if(icon.isNull()) { //如果m_logForm对象的图标为空
cur = ui->tabWidget->addTab(m_logForm, m_logForm->windowTitle());
}
else {
//增加m_logForm对象到Tab页中
cur = ui->tabWidget->addTab(m_logForm, icon, m_logForm->windowTitle());
}
m_logForm->show();
ui->tabWidget->setCurrentIndex(cur); //设置TabWidget的当前页
ui->tabWidget->setVisible(true);
}
else if(actName=="act_PsHelloworldMainWindow") { //识别是act_PsHelloworldMainWindow对象
PsHelloworldMainWindow *widget = new PsHelloworldMainWindow(this);
icon = widget->windowIcon();
widget->setAttribute(Qt::WA_DeleteOnClose); //关闭时自动删除
if(icon.isNull()) {
cur = ui->tabWidget->addTab(widget, widget->windowTitle());
}
else {
//增加PsHelloworldMainWindow对象到Tab页中
cur = ui->tabWidget->addTab(widget, icon, widget->windowTitle());
}
ui->tabWidget->setCurrentIndex(cur);
ui->tabWidget->setVisible(true);
}
}
}
void PsMainWindow::on_tabWidget_tabCloseRequested(int index)
{
if (index<0)
return;
//从tabWidget的当前tab页中,得到当前嵌入对象
QWidget* aForm = ui->tabWidget->widget(index);
if(PlBaseApplication::getGlobalLogger()->isDebug()) {
LOG_DEBUG(PlBaseApplication::getGlobalLogger(), QObject::tr("示例模块-关闭QWidget类[%1]").arg(aForm->metaObject()->className()));
}
//如果当前对象的名称是PlLogForm
if(PlReflector::isClassName(aForm, "PlLogForm")) {
aForm->hide();
ui->tabWidget->removeTab(index);
return;
}
//关闭当前页
aForm->close();
}
void PsMainWindow::on_tabWidget_tabBarDoubleClicked(int index)
{
on_tabWidget_tabCloseRequested(index);
}
}
4. 代码下载
代码下载链接:百度网盘 请输入提取码
Peanutlib项目演示程序下载:peanut_pwdis: pwdis是QT应用系统开发框架(C++),采用分层模块化设计,底层peanutlib按模块封装方便易用的类库及API(xml,db,appconfig,log,grid,json,bean等十几个),应用层提供部门人员权限及报表等,还提供了开发中常用的组件使用。
联系人:积木虎
QQ:154318869
Email:154318869.qq.com