目录
前言
做嵌入式总是要和Qt扯上关系,而且Qt也挺有意思的,毕竟有个界面可以玩!而且断断续续学过一段时间,感觉一点效果没有,写下来就不需要重新学了!有时候提供效率最有效的方法就是总结!
相关Qt代码介绍
资源文件的添加
- 右键项目,如图所示为
1_test
,点击添加新文件,如下图:
- 在打开的界面选择
Qt
,然后选择Qt Resource File
,如下图:
choose
,添入资源的名称,比如images
,然后下一步,最后点击完成如下图:
- 资源文件添加后,会出现资源文件夹,在资源文件中会包含
images.qrc
,如下图:
- 点击
images.qrc
,在右侧界面点击添加,选择添加前缀, - 前缀可以随意设置,比如我设置为
\
,然后再次点击添加,选择添加文件,然后在工程目录下的资源文件夹,比如我在工程目录下创建了images
这个文件,这里面放置了我需要的图片文件,比如save.png
,这时候就添加这个文件就可以了。添加成功后,如下图:
简单的界面实现
界面分为菜单栏、工具栏、中央窗口、停靠栏和状态栏(至少目前学到是这样!)。而我想要做的就是实现一个有简单的菜单栏和工具栏、中央窗口和状态栏的界面。因为是学习,所以就不使用界面文件做窗口先!
- main.cpp
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
- mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
class QTextEdit;
class QAction;
class QMenu;
class QToolBar;
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
void createActions();
void createMenus();
void createToolBars();
void createStatusBar();
public slots:
void slotNewFile();
void slotOpenFile();
void slotSaveFile();
void slotExitFile();
void slotCopyFile();
void slotCutFile();
void slotPasteFile();
void slotAboutFile();
private:
QTextEdit* text;
QMenu* fileMenu;
QMenu* editMeau;
QMenu* aboutMeau;
QToolBar* fileTool;
QToolBar* editTool;
QAction* fileOpenAction;
QAction* fileNewAction;
QAction* fileSaveAction;
QAction* exitAction;
QAction* copyAction;
QAction* cutAction;
QAction* pasteAction;
QAction* aboutAction;
};
#endif // MAINWINDOW_H
- mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QtWidgets>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent)
{
setWindowTitle(tr("My Main Window"));
text = new QTextEdit(this);
setCentralWidget(text);
// sets the given widget to be the main window's central widget
createActions(); // have to create Actions before Menus
createMenus();
createToolBars();
createStatusBar();
}
void MainWindow::createMenus()
{
fileMenu = menuBar()->addMenu(tr("File"));
fileMenu->addAction(fileOpenAction);
}
void MainWindow::createToolBars()
{
fileTool = addToolBar(tr("File"));
editTool = addToolBar(tr("Edit"));
fileTool->addAction(fileOpenAction);
}
void MainWindow::createStatusBar()
{
QLabel* tipLabel = new QLabel(tr("ready"));
statusBar()->addWidget(tipLabel);
}
void MainWindow::createActions()
{
// : represent current projetc directory
fileOpenAction = new QAction(QIcon(":/images/Save.png"), tr("Open"), this);
fileOpenAction->setShortcut(tr("Ctrl+O"));
connect(fileOpenAction, SIGNAL(triggered()), this, SLOT(slotOpenFile()));
}
void MainWindow::slotOpenFile()
{
QMessageBox msgBox;
msgBox.setText(tr("File Open"));
msgBox.exec();
}
void MainWindow::slotNewFile()
{
}
void MainWindow::slotSaveFile()
{
}
void MainWindow::slotExitFile()
{
}
void MainWindow::slotCopyFile()
{
}
void MainWindow::slotCutFile()
{
}
void MainWindow::slotPasteFile()
{
}
void MainWindow::slotAboutFile()
{
}
MainWindow::~MainWindow()
{
}