本文主要是介绍基于QAction和事件过滤器的软件主界面设计,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
先看一下效果图。
功能说明:1.因为使用了布局管理器,故下方白色区域(tabwidget)可随窗体大小自动充满。
2.依次点击五个图标(主页、查询、设置、用户、退出),可切换白色区域显示内容(其实是切换到tabwidget不同的索引页),每个索引页的UI可在Form中进行可视化设计。
代码如下:
一、KS.pro(默认配置,无须编写),我的工程取名为KS
#-------------------------------------------------
#
# Project created by QtCreator 2019-11-03T00:00:46
#
#-------------------------------------------------QT += core guigreaterThan(QT_MAJOR_VERSION, 4): QT += widgetsTARGET = KS
TEMPLATE = app# The following define makes your compiler emit warnings if you use
# any feature of Qt which as been marked as deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
SOURCES += \main.cpp \mainwindow.cpp
HEADERS += \mainwindow.h \includes.h
FORMS += \mainwindow.ui
二、main.cpp(也是默认配置)
#include "mainwindow.h"
#include <QApplication>int main(int argc, char *argv[])
{QApplication a(argc, argv);MainWindow w;w.show();return a.exec();
}
三、includes.h
#ifndef INCLUDES_H
#define INCLUDES_H#include <QMainWindow>
#include <QDebug>
#include <QDialog>
#include <QAction>
#include <QMessageBox>
#include <QCoreApplication>
#include <QEvent>
#include <QImage>
#include <QPixmap>
#include <QStatusBar>
#include <QMouseEvent>
#include <QGridLayout>
#include <QVBoxLayout>#endif // INCLUDES_H
四、 mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H#include <QMainWindow>
#include "includes.h"namespace Ui {
class MainWindow;
}class MainWindow : public QMainWindow
{Q_OBJECTQAction *Action_MainForm;QAction *Action_Inquire;QAction *Action_Settings;QAction *Action_Users;QAction *Action_Quit;void InstallEventFilter();void UI_Init();public:explicit MainWindow(QWidget *parent = 0);~MainWindow();private:Ui::MainWindow *ui;protected:bool eventFilter(QObject *obj, QEvent *event);public slots:};#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);UI_Init();InstallEventFilter();
}MainWindow::~MainWindow()
{delete ui;
}void MainWindow::UI_Init()
{this->resize(1100,700);this->setWindowTitle("监控软件");setWindowFlags(Qt::WindowCloseButtonHint | Qt::WindowMinMaxButtonsHint);QVBoxLayout* layout = new QVBoxLayout(); //创建一个layout对象ui->tabWidget->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding); //设置tabWidget的尺寸策略ui->tabWidget->findChildren<QTabBar*>().at(0)->hide(); //隐藏tabwidget表头layout->addWidget(ui->tabWidget); //将tabwidget添加到布局layoutQWidget* widget = new QWidget(this); //新建一个widgetwidget->setLayout(layout); //将layout添加到widget区域中this->setCentralWidget(widget);//将widget设置为中心窗口int maintoolbar_size=54;ui->mainToolBar->setFixedHeight(maintoolbar_size);ui->mainToolBar->setIconSize(QSize(maintoolbar_size,maintoolbar_size));QPixmap *pixmap_logo_long=new QPixmap("./resources/logo_long.jpg");QPixmap p_logo_long=pixmap_logo_long->scaled(270,50,Qt::KeepAspectRatio);QPixmap *pixmap_home=new QPixmap("./resources/home.jpg");QPixmap p_home=pixmap_home->scaled(maintoolbar_size,maintoolbar_size,Qt::KeepAspectRatio);QPixmap *pixmap_inquire=new QPixmap("./resources/inquire.jpg");QPixmap p_inquire=pixmap_inquire->scaled(maintoolbar_size,maintoolbar_size,Qt::KeepAspectRatio);QPixmap *pixmap_settings=new QPixmap("./resources/settings.jpg");QPixmap p_settings=pixmap_settings->scaled(maintoolbar_size,maintoolbar_size,Qt::KeepAspectRatio); QPixmap *pixmap_human=new QPixmap("./resources/human.jpg");QPixmap p_human=pixmap_human->scaled(maintoolbar_size,maintoolbar_size,Qt::KeepAspectRatio);QPixmap *pixmap_quit=new QPixmap("./resources/quit.jpg");QPixmap p_quit=pixmap_quit->scaled(maintoolbar_size,maintoolbar_size,Qt::KeepAspectRatio);//创建ActionQWidget* blankwidget; int spacesize=30;QWidget* logowidget = new QWidget(this); logowidget->setFixedSize(270,50);QLabel* label_longlogo=new QLabel(logowidget);label_longlogo->setFixedSize(270,50);label_longlogo->setPixmap(p_logo_long);ui->mainToolBar->addWidget(logowidget);QWidget* spacer = new QWidget();spacer->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);ui->mainToolBar->addWidget(spacer);Action_MainForm = new QAction(QIcon(p_home), tr("&主界面"), this);Action_MainForm->setShortcut(tr("Ctrl+1"));Action_MainForm->setStatusTip(tr("Main interface"));ui->mainToolBar->addAction(Action_MainForm);blankwidget = new QWidget(this); //新建一个widgetblankwidget->setFixedSize(spacesize,maintoolbar_size);ui->mainToolBar->addWidget(blankwidget);Action_Inquire = new QAction(QIcon(p_inquire), tr("&查询"), this);Action_Inquire->setShortcut(tr("Ctrl+2"));Action_Inquire->setStatusTip(tr("Inquire"));ui->mainToolBar->addAction(Action_Inquire);blankwidget = new QWidget(this); //新建一个widgetblankwidget->setFixedSize(spacesize,maintoolbar_size);ui->mainToolBar->addWidget(blankwidget);Action_Settings = new QAction(QIcon(p_settings), tr("&设置"), this);Action_Settings->setShortcut(tr("Ctrl+3"));Action_Settings->setStatusTip(tr("Settings"));ui->mainToolBar->addAction(Action_Settings);blankwidget = new QWidget(this); //新建一个widgetblankwidget->setFixedSize(spacesize,maintoolbar_size);ui->mainToolBar->addWidget(blankwidget);Action_Users = new QAction(QIcon(p_human), tr("&用户"), this);Action_Users ->setShortcut(tr("Ctrl+4"));Action_Users ->setStatusTip(tr("User Management"));ui->mainToolBar->addAction(Action_Users);blankwidget = new QWidget(this); //新建一个widgetblankwidget->setFixedSize(spacesize,maintoolbar_size);ui->mainToolBar->addWidget(blankwidget);Action_Quit = new QAction(QIcon(p_quit), tr("&退出"), this);Action_Quit->setShortcut(tr("Ctrl+5"));Action_Quit->setStatusTip(tr("Quit"));ui->mainToolBar->addAction(Action_Quit);blankwidget = new QWidget(this); //新建一个widgetblankwidget->setFixedSize(spacesize,maintoolbar_size);ui->mainToolBar->addWidget(blankwidget);for(int i=0;i<ui->tabWidget->tabBar()->count();i++){ui->tabWidget->setTabEnabled(i,false);}ui->tabWidget->setTabEnabled(0,true);ui->tabWidget->setCurrentIndex(0);}//注册事件过滤器
void MainWindow::InstallEventFilter()
{Action_MainForm->setCheckable(true);Action_Inquire->setCheckable(true);Action_Settings->setCheckable(true);Action_Users->setCheckable(true);Action_Quit->setCheckable(true);Action_MainForm->installEventFilter(this);Action_Inquire->installEventFilter(this);Action_Settings->installEventFilter(this);Action_Users->installEventFilter(this);Action_Quit->installEventFilter(this);
}//事件过滤器
bool MainWindow::eventFilter(QObject *obj, QEvent *event)
{if(event->type()==QEvent::ActionChanged){if(obj==Action_MainForm&&Action_MainForm->isChecked()==true){qDebug()<<"1 Action_MainForm";if(Action_Inquire->isChecked())Action_Inquire->setChecked(false);if(Action_Settings->isChecked())Action_Settings->setChecked(false);if(Action_Users->isChecked())Action_Users->setChecked(false);if(Action_Quit->isChecked())Action_Quit->setChecked(false); ui->tabWidget->setCurrentIndex(0);}else if(obj==Action_Inquire&&Action_Inquire->isChecked()==true){qDebug()<<"2 Action_Inquire";if(Action_MainForm->isChecked())Action_MainForm->setChecked(false);if(Action_Settings->isChecked())Action_Settings->setChecked(false);if(Action_Users->isChecked())Action_Users->setChecked(false);if(Action_Quit->isChecked())Action_Quit->setChecked(false);ui->tabWidget->setCurrentIndex(1);}else if(obj==Action_Settings&&Action_Settings->isChecked()==true){qDebug()<<"3 Action_Settings";if(Action_MainForm->isChecked())Action_MainForm->setChecked(false);if(Action_Inquire->isChecked())Action_Inquire->setChecked(false);if(Action_Users->isChecked())Action_Users->setChecked(false);if(Action_Quit->isChecked())Action_Quit->setChecked(false);ui->tabWidget->setCurrentIndex(2);}else if(obj==Action_Users&&Action_Users->isChecked()==true){qDebug()<<"4 Action_Users";if(Action_MainForm->isChecked())Action_MainForm->setChecked(false);if(Action_Inquire->isChecked())Action_Inquire->setChecked(false);if(Action_Settings->isChecked())Action_Settings->setChecked(false);if(Action_Quit->isChecked())Action_Quit->setChecked(false);ui->tabWidget->setCurrentIndex(3);}else if(obj==Action_Quit&&Action_Quit->isChecked()==true){qDebug()<<"5 Action_Quit";if(Action_MainForm->isChecked())Action_MainForm->setChecked(false);if(Action_Inquire->isChecked())Action_Inquire->setChecked(false);if(Action_Settings->isChecked())Action_Settings->setChecked(false);if(Action_Users->isChecked())Action_Users->setChecked(false);ui->tabWidget->setCurrentIndex(4);}else{;}//如果想让五个图标点击后不自动弹起,请注释掉接下来的五行代码Action_MainForm->setChecked(false);Action_Inquire->setChecked(false);Action_Settings->setChecked(false);Action_Users->setChecked(false);Action_Quit->setChecked(false);}//eventFilter实现的最后必须调用watcher基类的eventFilter函数以传递事件,否则obj的所有事件都将丢失return QObject::eventFilter(obj,event);
}
后记:
1.使用事件过滤器无法判别QAction的按下或抬起,所以造成代码量有些复杂。如果改用信号槽机制,将每个Action的triggered信号连接到相应的槽进行处理,也是一种思路,代码量也相差无几。
2.本次示例没有建立菜单栏中的菜单项,也没有将Action添加到菜单中,因为对于简单的项目,会显得有些冗余,读者也可自己编写,将这五个Action添加到菜单中。
期待一起进步,也祝我接下来几天玩的愉快哈哈~
这篇关于基于QAction和事件过滤器的软件主界面设计的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!