熟悉创建主窗口项目
1 QAction
2 主窗口
菜单栏:fileMenu = menuBar()->addMenu(tr("&File"));
工具栏:fileToolBar = addToolBar(tr("File"));
浮动窗:QDockWidget *dockWidget
= new QDockWidget(tr("Dock Widget"), this);
addDockWidget(Qt::LeftDockWidgetArea, dockWidget);
中央部件:setCentralWidget(te);
状态栏:statusBar()->addWidget(progress);
示例
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QAction>
#include <QTextEdit>
#include <QLabel>
#include <QFileDialog>
#include <QColorDialog>
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = 0);
~MainWindow();
public slots:
void openf()
{
QString filename = QFileDialog::getOpenFileName();
te->setText(filename);
}
void copyf()
{
te->copy();
}
void pastf()
{
te->paste();
}
void setcf()
{
QColor ccc = QColorDialog::getColor();
te->setTextColor(ccc);
}
private:
QAction *openfileact, *copyfileact, *pastfileact, *setcoloract;
//central
QTextEdit *te;
//status
QLabel *slb;
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include <QMenu>
#include <QMenuBar>
#include <QToolBar>
#include <QStatusBar>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
// 创建动作并设置图标、快捷键
openfileact = new QAction(QIcon("open.png"), "Open", this);
openfileact->setShortcut(QKeySequence(tr("Ctrl+O")));
copyfileact = new QAction(QIcon("copy.png"), "Copy", this);
pastfileact = new QAction(QIcon("past.png"), "Paste", this);
setcoloract = new QAction(QIcon("color.png"), "Set Color", this);
// 创建菜单并添加动作
QMenu *fileMenu = menuBar()->addMenu(tr("&File"));
fileMenu->addAction(openfileact);
QMenu *editMenu = menuBar()->addMenu(tr("&Edit"));
editMenu->addAction(copyfileact);
editMenu->addAction(pastfileact);
QMenu *setMenu = menuBar()->addMenu(tr("&Set"));
setMenu->addAction(setcoloract);
// 创建工具栏并添加动作
QToolBar *tfile = addToolBar("File");
tfile->addAction(openfileact);
QToolBar *tedit = addToolBar("Edit");
tedit->addAction(copyfileact);
tedit->addAction(pastfileact);
QToolBar *tset = addToolBar("Set");
tset->addAction(setcoloract);
// 设置中央部件为文本编辑框
te = new QTextEdit("hello", this);
te->setMinimumSize(640, 480);
setCentralWidget(te);
// 添加状态栏
slb = new QLabel("1.txt*", this);
QStatusBar *stb = statusBar();
stb->addWidget(slb);
// 连接信号和槽
connect(openfileact, &QAction::triggered, this, &MainWindow::openf);
connect(copyfileact, &QAction::triggered, this, &MainWindow::copyf);
connect(pastfileact, &QAction::triggered, this, &MainWindow::pastf);
connect(setcoloract, &QAction::triggered, this, &MainWindow::setcf);
}
MainWindow::~MainWindow()
{
}