day4 QT主窗口

QMainWindow

  • QMainWindow是用于创建应用程序的主窗口的类,通常用于创建具有复杂布局和多个功能区域的应用程序窗口。
  • 与QWidget和QDialog不同,QMainWindow提供了一个主要的菜单栏、工具栏、状态栏和中央部件,使得创建复杂的多文档界面(MDI)应用程序更加方便。
  • 它通常用于创建大型的桌面应用程序。

示例

#include "mainwindow.h"
#include 
#include 
#include 
#include 

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    // 创建动作
    openfileact = new QAction(QIcon("open.png"), "Open"); // 打开文件动作
    openfileact->setShortcut(QKeySequence(tr("Ctrl+O"))); // 设置快捷键 Ctrl+O

    copyfileact = new QAction(QIcon("copy.png"), "Copy"); // 复制动作
    pastfileact = new QAction(QIcon("paste.png"), "Paste"); // 粘贴动作

    setcoloract = new QAction(QIcon("color.png"), "Set Color"); // 设置颜色动作

    // 创建菜单
    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"); // 创建文本编辑器,并设置默认文本为 "hello"
    te->setMinimumSize(640, 480); // 设置文本编辑器的最小尺寸为 640x480
    setCentralWidget(te); // 将文本编辑器设置为主窗口的中央部件

    // 状态栏部件
    slb = new QLabel("1.txt*"); // 创建状态栏标签,并显示默认文本为 "1.txt*"
    QStatusBar *stb = statusBar(); // 获取主窗口的状态栏
    stb->addWidget(slb); // 将状态栏标签添加到状态栏中

    // 连接信号和槽函数
    connect(openfileact, SIGNAL(triggered(bool)), this, SLOT(openf())); // 打开文件动作与槽函数 openf() 连接
    connect(copyfileact, SIGNAL(triggered(bool)), this, SLOT(copyf())); // 复制动作与槽函数 copyf() 连接
    connect(pastfileact, SIGNAL(triggered(bool)), this, SLOT(pastf())); // 粘贴动作与槽函数 pastf() 连接
    connect(setcoloract, SIGNAL(triggered(bool)), this, SLOT(setcf())); // 设置颜色动作与槽函数 setcf() 连接
}

MainWindow::~MainWindow()
{

}
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include 
#include 
#include 
#include 
#include 
#include 

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;
    QAction *copyfileact, *pastfileact;
    QAction *setcoloract;

    // 中央部件
    QTextEdit *te;

    // 状态栏部件
    QLabel *slb;
};

#endif // MAINWINDOW_H

你可能感兴趣的:(LV.20,QT嵌入式图形开发,我的小白学习笔记,qt,开发语言,c语言,c++,linux)