qt 5.15.2 主窗体菜单工具栏树控件功能

qt 5.15.2 主窗体菜单工具栏树控件功能

显示主窗体效果:
在这里插入图片描述
mainwindow.h文件内容:

#ifndef MAINWINDOW_H
#define MAINWINDOW_H#include <QMainWindow>
#include <QFileDialog>
#include <QString>
#include <QMessageBox>#include <QTreeView>
#include <QFileSystemModel>#include <QDockWidget>
#include <QLabel>
#include <QPushButton>
#include <QTextEdit>
#include <QToolBar>
#include <QAction>
#include <QFile>
#include <QStandardItemModel>
#include <QResizeEvent>
#include <QDebug>
//#include "scene.h"using namespace Qt;QT_BEGIN_NAMESPACE
namespace Ui {
class MainWindow;
}
QT_END_NAMESPACEclass MainWindow : public QMainWindow
{Q_OBJECTpublic:MainWindow(QWidget *parent = nullptr);~MainWindow();//bool eventFilter(QObject *obj, QEvent *event);//void resizeEvent(QResizeEvent *event);private slots:void openImageFile();     //定义卡槽函数void TreeDoubleClicked(const QModelIndex &index);void on_actionOpen_File_triggered();void init_3d();private:Ui::MainWindow *ui;    //QString currentFile;QTreeView* treeView;QDockWidget *dockWidget;//QStandardItem* currentNode;QStandardItemModel* model;//Scene* scene;Qt3DExtras::Qt3DWindow* view;QWidget *sceneWidget;
};
#endif // MAINWINDOW_H

mainwindow.cpp文件内容:

#include "mainwindow.h"
#include "ui_mainwindow.h"#include <QFirstPersonCameraController>
#include <QSplitter>
#include <QVBoxLayout>MainWindow::MainWindow(QWidget *parent): QMainWindow(parent), ui(new Ui::MainWindow)
{ui->setupUi(this);//设置主界面主题颜色风格(黑灰背景色+白字)//this->setStyleSheet ("background-color: rgb(60,60,60);color: rgb(255,255,255);border-color:rgb(230,230,230);");//重置窗口//resize(600, 400);//最大化窗体this->setWindowState(Qt::WindowMaximized);//创建菜单栏 (只有一个菜单栏)QMenuBar *bar = this->menuBar();//将菜单栏放入窗口中this->setMenuBar(bar);//创建菜单项QMenu *menuBegin = bar->addMenu("开始");QMenu *menuEdit = bar->addMenu("操作");QMenu *menuDisplay = bar->addMenu("显示");QMenu *menuView = bar->addMenu("视图");//创建菜单项QAction *build_act = menuBegin->addAction("新建");//给菜单项下面再添加项目//添加分隔符menuBegin->addSeparator();//程序中菜单栏最多有一个//QICon *ico=QIcon(":/images/File");//添加菜单和工具栏按钮功能并与功能函数绑定QString filePath=qApp->applicationDirPath()+"/images/File.ico";QAction *act_open_image =menuBegin->addAction(QIcon(filePath),"打开图片文件");connect(act_open_image,&QAction::triggered,this,&MainWindow::openImageFile);//添加菜单和工具栏按钮功能并与功能函数绑定QString openDirPath=qApp->applicationDirPath()+"/images/Open.ico";QAction *act_open = menuBegin->addAction(QIcon(openDirPath),"打开文件夹");connect(act_open,&QAction::triggered,this,&MainWindow::openImageFile);QAction *act_init_3d = menuBegin->addAction(QIcon(openDirPath),"初始化3D");connect(act_init_3d,&QAction::triggered,this,&MainWindow::init_3d);//工具栏,可以有多个QToolBar * toolBar = new QToolBar(this);this->addToolBar(toolBar);  // 将工具栏添加this->addToolBar(Qt::TopToolBarArea,toolBar);     //出现在top//this->addToolBar(Qt::LeftToolBarArea, toolBar);//令其默认出现在左边//this->addToolBar(Qt::RightToolBarArea, toolBar);//设置允许的停靠范围toolBar->setAllowedAreas(Qt::TopToolBarArea);//toolBar->setAllowedAreas(Qt::LeftToolBarArea | Qt::RightToolBarArea);//不会让其停靠在上边,但会浮动//设置浮动toolBar->setFloatable(false);//禁止浮动//设置移动toolBar->setMovable(false);//禁止移动//工具栏可设置内容toolBar->addAction(build_act);//将新建菜单项添加进来,传入的是QAction指针toolBar->addSeparator();//添加分割线toolBar->addAction(act_open);        //将打开项目添加进来toolBar->addAction(act_open_image);  //打开toolBar->addAction(act_init_3d);     //初始化3dtoolBar->addSeparator();toolBar->addAction("其他");//传入字符串//工具栏添加控件QPushButton *p = new QPushButton(this);p->setText("自定义的按钮");toolBar->addWidget(p);// 添加控件////状态栏:最多一个QStatusBar * stBar = this->statusBar();//设置到窗口this->setStatusBar(stBar);//QLabel *leftMsg=new QLabel(this->currentFile,this);stBar->showMessage(this->currentFile);// 放入标签控件QLabel *label = new QLabel("提示信息", this);stBar->addWidget(label);//放到左侧//状态栏设置标签控件位置stBar->addPermanentWidget(label);//放到右侧//铆接部件(浮动窗口),可以多个QDockWidget *dockWidget = new QDockWidget("功能树", this);//设置只能移动,不能关闭dockWidget->setFeatures(QDockWidget::NoDockWidgetFeatures | QDockWidget::DockWidgetMovable);this->addDockWidget(Qt::LeftDockWidgetArea, dockWidget);//将浮动窗口默认放到左边//设置后期停靠区域,只允许左右dockWidget->setAllowedAreas(Qt::LeftDockWidgetArea);dockWidget->setFixedWidth(300);//设置中心部件//QTextEdit *edit = new QTextEdit(this);//this->setCentralWidget(edit);//菜单栏:set只能一个, 工具栏add可以多个,状态栏set只能一个, 铆接部件add可以多个//树控件treeView=new QTreeView(dockWidget);//treeView->setFixedWidth(300);//treeView->setFixedHeight(600);// 隐藏标题头treeView->setHeaderHidden(true);// 禁用节点编辑treeView->setEditTriggers(QAbstractItemView::NoEditTriggers);//添加双击事件connect(treeView,&QTreeView::doubleClicked,this,&MainWindow::TreeDoubleClicked);dockWidget->setWidget(treeView);// 设置初始权重以控制初始大小分配//QSplitter splitter;//splitter.setStretchFactor(0, 1); // QDockWidget 子控件权重为 1//splitter.addWidget(dockWidget);//splitter.show();//QFileSystemModel* fileSystemModel=new QFileSystemModel;//fileSystemModel->setRootPath("/");//treeView->setModel(fileSystemModel);model = new QStandardItemModel(dockWidget);treeView->setModel(model);QStandardItem* item3d = new QStandardItem("三维模型");model->appendRow(item3d);//子节点QStandardItem* itemOpenShp = new QStandardItem("打开obj文件");item3d->appendRow(itemOpenShp);//QStandardItem* itemVec = new QStandardItem("矢量图层");model->appendRow(itemVec);//QStandardItem* itemImage = new QStandardItem("影像图层");model->appendRow(itemImage);//QStandardItem* itemMarker = new QStandardItem("标注信息");model->appendRow(itemMarker);////scene  3dview = new Qt3DExtras::Qt3DWindow();sceneWidget = QWidget::createWindowContainer(view);scene = new Scene(view);view-> installEventFilter(this);// 添加布局管理   treeView的高宽与QVBoxLayout布局自动变化QWidget *centralWidget = new QWidget();//QVBoxLayout *layout = new QVBoxLayout(centralWidget);QHBoxLayout *layout = new QHBoxLayout(centralWidget);layout->addWidget(dockWidget);   //树控件layout->addWidget(sceneWidget);  //3d地图控件this->setCentralWidget(centralWidget);//
}MainWindow::~MainWindow()
{delete ui;
}bool MainWindow::eventFilter(QObject *obj, QEvent *event)
{if (event->type() == QEvent::KeyPress){QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);scene->KeyControls(keyEvent);}return QObject::eventFilter(obj, event);
}/*
void MainWindow::resizeEvent(QResizeEvent *event)
{treeView->setFixedWidth(dockWidget->width());treeView->setFixedHeight(dockWidget->height());//qDebug()<<"resize: "<<event->size();this->update();
}*///打开文件 menu/toolbar
void MainWindow::openImageFile()
{QString filename= QFileDialog::getOpenFileName(this,"Open file");QFile file(filename);currentFile = filename;if (!file.open(QIODevice::ReadOnly)) {QMessageBox::warning(this,"Warning", "Cannot open "+file.errorString());}setWindowTitle(filename);//添加3d fileQUrl fUrl=QUrl("file://"+filename);Qt3DRender::QMesh *mesh = new Qt3DRender::QMesh();mesh->setSource(fUrl);Qt3DCore::QEntity *entity = new Qt3DCore::QEntity();entity->addComponent(mesh);Qt3DExtras::QFirstPersonCameraController *cam=new Qt3DExtras::QFirstPersonCameraController(entity);cam->setCamera(view->camera());view->setRootEntity(entity);//view->camera()->viewAll();}//打开文件 tree pad/open
void MainWindow::on_actionOpen_File_triggered()
{QString filename= QFileDialog::getOpenFileName(this,"Open file");QFile file(filename);currentFile = filename;if (!file.open(QIODevice::ReadOnly)) {QMessageBox::warning(this,"Warning", "Cannot open "+file.errorString());}setWindowTitle(filename);//Qt3DRender::QMesh *mesh = new Qt3DRender::QMesh();QUrl data =QUrl::fromLocalFile(filename);mesh->setSource(data);qDebug()<<"mesh status="<<mesh->status();//获取视图的大小//QSize screenSize = view->size();sceneWidget->setMinimumSize(QSize(10, 10));//最小sceneWidget->setMaximumSize(QSize(5000, 5000));//最大scene->NewScene(mesh);Qt3DExtras::QFirstPersonCameraController *cam=new Qt3DExtras::QFirstPersonCameraController(scene->rootEntity);cam->setCamera(view->camera());}void MainWindow::init_3d()
{scene->init3d();Qt3DExtras::QFirstPersonCameraController *cam=new Qt3DExtras::QFirstPersonCameraController(scene->rootEntity);cam->setCamera(view->camera());
}void MainWindow::TreeDoubleClicked(const QModelIndex &index)
{// 获取当前选中节点索引QModelIndex currentIndex = treeView->currentIndex();if (currentIndex.isValid()) {// 执行相关操作,例如获取节点信息或执行其他操作this->currentNode = model->itemFromIndex(currentIndex);if(this->currentNode->text()=="打开obj文件"){this->on_actionOpen_File_triggered();}else{//qDebug() << "Current selected item text:" << currentItem->text();QMessageBox::information(this,"提示信息", this->currentNode->text());}} else {//qDebug() << "No item selected";QMessageBox::information(this,"提示信息", "未选中节点");}
}

本blog地址:https://blog.csdn.net/hsg77

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/255256.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

SFX的妙用——如何在不安装软件的情况下打开自定义格式文件?

前段时间看到群友讨论压缩包能不能运行&#xff0c;想起了n年前用自解压文件SFX实现的一个“需求”&#xff1a;在没有安装任何应用软件的Windows&#xff08;当时还要支持XP&#xff09;上能双击打开自定义格式的文件。当时第一反应是这“需求”太奇葩了&#xff0c;简直是不可…

【JavaScript】JS——Map数据类型

【JavaScript】JS——Map数据类型 什么是Map?特性Map与Object的比较 map的创建map的属性map相关方法map的遍历 什么是Map? 存储键值对的对象。 能够记住键的原始插入顺序任何值&#xff08;对象或原始值&#xff09;都可以作为键或值。 特性 Map中的一个键只能出现一次&am…

PAT乙级—1002 写出这个数(C语言)

读入一个正整数 n&#xff0c;计算其各位数字之和&#xff0c;用汉语拼音写出和的每一位数字。 输入样例&#xff1a; 1234567890987654321123456789 输出样例&#xff1a; yi san wu #include <stdio.h>int main() {char number[100] { \0 };int i 0, sum 0, n[8] {…

应用现代化加速企业数字化转型

目录 一、数字化转型的必要性 二、应用现代化的推动力 数字化时代&#xff0c;企业正面临着前所未有的挑战和机遇。为了保持竞争力&#xff0c;许多企业正在寻求数字化转型&#xff0c;以提升运营效率、优化客户体验、创新商业模式。本文将探讨如何通过应用现代化加速企业数字化…

c++11计时器chrono库

去实现一下开始说的高精度计时器&#xff1a; #ifndef _TimerClock_hpp_ #define _TimerClock_hpp_#include <iostream> #include <chrono>using namespace std; using namespace std::chrono;class TimerClock { public:TimerClock(){update();}~TimerClock(){}v…

MySQL和MongoDB简介以及它们之间的区别

本文主要介绍MySQL和MongoDB的简介以及它们之间的区别。 目录 MySQL简介MySQL的优缺点MySQL的应用场景MongoDB简介MongoDB的优缺点MongoDB的应用场景MySQL和MongoDB的区别 MySQL简介 MySQL是一种开源的关系型数据库管理系统&#xff0c;是世界上最流行的数据库之一。它支持多用…

如何选择合适水下应用的集成电缆传感器?

来源&#xff1a;宏集科技 工业物联网丨宏集干货 | 如何选择合适水下应用的集成电缆传感器&#xff1f; 原文链接&#xff1a;https://mp.weixin.qq.com/s/wbN40niOgpUHy1iSH9Ad3Q 欢迎关注虹科&#xff0c;为您提供最新资讯&#xff01; 前言 许多工业过程都要求将传感器浸…

使用高防IP防护有哪些优势

高防IP是针对互联网服务器在遭受大流量的DDoS攻击后导致服务不可用的情况下&#xff0c;推出的付费增值服务&#xff0c;用户可以通过配置高防IP&#xff0c;将攻击流量引流到高防IP&#xff0c;确保源站的稳定可靠。高防IP相当于搭建完转发的服务器。 高防IP有两种接入方式&a…

项目管理:为什么项目计划必不可少

项目管理计划定义了如何执行、监督和控制项目。项目计划让我们准确地知道在项目的每个阶段应该做什么&#xff0c;在哪里分配资源和时间&#xff0c;以及在事情超出计划或超出预算时要注意什么。 为了项目中获得成功&#xff0c;管理者需要在前期创建一个项目计划&#xff0c…

高速风筒解决方案,基于高性价比的普冉单片机开发

高速风筒也就是高速吹风机&#xff0c;与传统的吹风机相比&#xff0c;高速吹风机具有更强大的风力和更快的干燥速度&#xff0c;可以更快地干燥头发或其他物体表面的水分。它通常由一个电动机驱动&#xff0c;并通过旋转的叶片来产生气流。高速风筒广泛应用于个人护理、美容、…

【华为数据之道学习笔记】3-2 基础数据治理

基础数据用于对其他数据进行分类&#xff0c;在业界也称作参考数据。基础数据通常是静态的&#xff08;如国家、币种&#xff09;&#xff0c;一般在业务事件发生之前就已经预先定义。它的可选值数量有限&#xff0c;可以用作业务或IT的开关和判断条件。当基础数据的取值发生变…

antdesign前端一直加载不出来

antdesign前端一直加载不出来 报错&#xff1a;Module “./querystring” does not exist in container. while loading “./querystring” from webpack/container/reference/mf at mf-va_remoteEntry.js:751:11 解决方案&#xff1a;Error: Module “xxx“ does not exist …