.h文件内容:
#pragma once
#include <QtWidgets/QMainWindow>
#include "ui_QtWidgetsApplication2.h"
#include <QLabel>
class QtWidgetsApplication2 : public QMainWindow
{
Q_OBJECT
public:
QtWidgetsApplication2(QWidget *parent = nullptr);
~QtWidgetsApplication2();
QStatusBar *statusbar;
QLabel* label;
public slots:
void OnActionTest();
private:
Ui::QtWidgetsApplication2Class ui;
};
cpp内容:新建文件连接了打开图片得函数用于测试
#include "QtWidgetsApplication2.h"
#include <QMenuBar>
#include <QToolBar>
#include <QStatusBar>
#include <QLabel>
#include <QDockWidget>
#include <QTextEdit>
#include <QFileDialog>
#include <QDebug>
#pragma execution_character_set("utf-8")
QtWidgetsApplication2::QtWidgetsApplication2(QWidget *parent)
: QMainWindow(parent)
{
ui.setupUi(this);
resize(800, 600);//设定窗口大小
QMenuBar *menubar = new QMenuBar(this);
setMenuBar(menubar);
QMenu * filename = new QMenu("文件", this);
QMenu *editmenu = new QMenu("编辑", this);
QMenu *buildname = new QMenu("构建", this);
//QMenu *filename = menubar->addMenu("文件");
//QMenu *editmenu = menubar->addMenu("编辑");
//QMenu *buildname = menubar->addMenu("构建");
QAction *pActionTest = new QAction("打开图片", this);
QAction *pActionTest1 = new QAction("打开文件", this);
QAction *pActionTest2 = new QAction("关闭文件", this);
filename->addAction(pActionTest);
filename->addAction(pActionTest1);
filename->addSeparator();
filename->addAction(pActionTest2);
QAction *pActionTest3 = new QAction("恢复", this);
editmenu->addAction(pActionTest3);
QAction *pActionTest4 = new QAction("构建所有项目", this);
buildname->addAction(pActionTest4);
menubar->addMenu(filename);
menubar->addMenu(editmenu);
menubar->addMenu(buildname);
// ToolBar
QToolBar *toolbar = new QToolBar(this);
addToolBar(Qt::TopToolBarArea, toolbar);
toolbar->addAction("新建");
toolbar->addSeparator();
toolbar->addAction("打开");
toolbar->addSeparator();
toolbar->addAction("关闭");
// statusbar
statusbar = new QStatusBar(this);
setStatusBar(statusbar);
QLabel *label1 = new QLabel(this);
label1->setText("状态栏");
statusbar->addWidget(label1);
// DockWidgets 浮动窗口 停靠窗口 中心部件的停靠位置
// 验证:浮动窗口是否在中心部件的左侧
//注意:这里还有问题,小窗口会可能导致菜单栏点击出现BUG
QDockWidget *dockWidget = new QDockWidget("小窗口", this);
addDockWidget(Qt::LeftDockWidgetArea, dockWidget);
// Center Widget 文本编辑器或浏览器部分
QTextEdit *edit = new QTextEdit(this);
setCentralWidget(edit);
label = new QLabel(this);
label->move(200, 200); //将标签移动到(0,100)位置
label->resize(200, 160); //设定标签大小
//label->setStyleSheet("border:1px solid black"); //设定边框
connect(pActionTest, &QAction::triggered, this, &QtWidgetsApplication2::OnActionTest);
}
void QtWidgetsApplication2::OnActionTest() {
QString filename = QFileDialog::getOpenFileName(this, tr("Open Image"), QDir::homePath(), tr("(*.jpg)\n(*.bmp)\n(*.png)")); //打开图片文件,选择图片
if (filename.isEmpty()) {
return;
}
QImage image = QImage(filename); //图片初始化
if (!image.isNull())
{
label->setPixmap(QPixmap::fromImage(image));
label->setScaledContents(true);//图片自适应大小
//label->show();
statusbar->showMessage(tr("Open Image Success!"), 3000); //打开成功时显示的内容
}
else
{
statusbar->showMessage(tr("Open Image Failed!"), 3000);
return;
}
}
QtWidgetsApplication2::~QtWidgetsApplication2()
{}
效果图展示:
此界面可能会出现一个bug,因为添加了小窗口和文本编辑器,会导致菜单栏点击时有点儿小问题,不用小窗口可以去掉。