Qt菜单栏第二章

mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include"qmenu.h"
#include"QAction"
#include"QLCDNumber"
#include<QLabel>
#include<QSystemTrayIcon>
namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();
    QLCDNumber *mytoolBarTime;
    void timerEvent(QTimerEvent *event);
    /*
    使用QTimer定义的对象来计时,应该使用timeout信号来连接到适当的槽来实
    现想要的功能,而timerEvent事件则用来响应QObject::startTimer()开启的定时器。
    */
public:
    void mouseMoveEvent(QMouseEvent *event);
    QLabel *MouseX;
    QLabel *MouseY;
    QSystemTrayIcon * myTrayIcon;
private slots:
    void on_action_O_triggered();

    void on_action_S_triggered();

    void on_action_E_triggered();

    void about();

    void mousePressEvent(QMouseEvent *event);
public slots:
    void activated(QSystemTrayIcon::ActivationReason reason);
private:
    Ui::MainWindow *ui;
    QMenu *aboutMenu;
    QAction *aboutAct;

};

#endif // MAINWINDOW_H

main.cpp

#include "mainwindow.h"
#include <QApplication>
#include <QtWidgets/QApplication>
#include<QAction>
#include<QToolBar>
#include<QTextCodec>
#include<QString>
#include<QIcon>
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    //设置中文字符
    QTextCodec::setCodecForLocale(QTextCodec::codecForName("GB2312"));
    //创建打开、保存、退出、关于等按钮的动作
    QAction *aboutAct;
    aboutAct=new QAction(QIcon(":/new/prefix1/img/5.jpg"),"&A关于",&a);
    QAction *OpenAct;
    OpenAct=new QAction(QIcon(":/new/prefix1/img/2.jpg"),"&O打开",&a);
    QAction *SaveAct;
    SaveAct=new QAction(QIcon(":/new/prefix1/img/3.jpg"),"&S保存",&a);
    QAction *ExitAct;
    ExitAct=new QAction(QIcon(":/new/prefix1/img/4.jpg"),"&E退出",&a);
    //创建工具条,将动作添加到工具条中
    QToolBar *mytoolBar;
    mytoolBar =new QToolBar;
    mytoolBar->addAction(OpenAct);
    mytoolBar->addAction(SaveAct);
    mytoolBar->addSeparator();
    mytoolBar->addAction(ExitAct);
    mytoolBar->addAction(aboutAct);
    //设置窗口形式为无框模式
    mytoolBar->setWindowFlags(Qt::FramelessWindowHint);
    mytoolBar->setMovable(true);
    mytoolBar->setFloatable(true);
    mytoolBar->show();
    //建立信号与槽的链接
    QObject::connect(aboutAct,SIGNAL(triggered(bool)),&w,SLOT(about()));
    QObject::connect(ExitAct,SIGNAL(triggered(bool)),mytoolBar,SLOT(close()));
    w.show();

    return a.exec();
}


mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include"QMessageBox"
#include"QMouseEvent"
#include"QLCDNumber"
#include<QTime>
#include<QSystemTrayIcon>
#include<QIcon>
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    aboutAct=new QAction(QIcon(":/new/prefix1/img/5.jpg"),tr("&A关于"),this);
    //点击事件源
    connect(aboutAct,SIGNAL(triggered(bool)),this,SLOT(about()));
    aboutMenu=menuBar()->addMenu(tr("关于"));
    //aboutMenu是菜单项
    aboutMenu->addAction(aboutAct);

    ui->mainToolBar->addSeparator();//分隔符
    ui->mainToolBar->addAction(aboutAct);
    //LCD
    QTime Current_Time=QTime::currentTime();
    mytoolBarTime=new QLCDNumber(ui->mainToolBar);
    mytoolBarTime->setDigitCount(15);//设置大小
    mytoolBarTime->display(Current_Time.toString("HH:mm:ss.zzz"));
    ui->mainToolBar->addWidget(mytoolBarTime);//设置位置
    this->startTimer(1000);//刷新时间
    //状态条添加部件
    MouseX=new QLabel(ui->statusBar);
    MouseX->setFixedWidth(50);
    MouseX->setIndent(3);
    MouseY=new QLabel(ui->statusBar);
    MouseY->setFixedWidth(50);
    MouseY->setIndent(3);
    ui->statusBar->addWidget(MouseX);
    ui->statusBar->addWidget(MouseY);
    setMouseTracking(true);
    /*这个属性保存的是窗口部件跟踪鼠标是否生效。
如果鼠标跟踪失效(默认),当鼠标被移动的时候只有在至少一个鼠标按键被按下时,
这个窗口部件才会接收鼠标移动事件。如果鼠标跟踪生效,如果没有按键被按下,
这个窗口部件也会接收鼠标移动事件。*/
    QIcon systemIcon(":/new/prefix1/img/5.jpg");

    myTrayIcon=new QSystemTrayIcon(systemIcon,this);
    myTrayIcon->show();
    myTrayIcon->showMessage("提示","单击隐藏,双击显示主窗口");
    myTrayIcon->setContextMenu(ui->menu1);//右击弹出菜单。
    connect(myTrayIcon,SIGNAL(activated(QSystemTrayIcon::ActivationReason)),this,SLOT(activated(QSystemTrayIcon::ActivationReason)));

}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_action_O_triggered()
{
    //QMessageBox::information(this,"消息","选择了打开按钮");
    statusBar()->showMessage(tr("选择打开"));
}

void MainWindow::on_action_S_triggered()
{
    QMessageBox::information(this,"消息","选择了保存按钮");
}

void MainWindow::on_action_E_triggered()
{
    QMessageBox::information(this,"消息","选择了退出按钮");
}
void MainWindow::about(){
    QMessageBox::about(this,"消息","关于");
}
void MainWindow::mousePressEvent(QMouseEvent *event){
    //右击弹出自己编写的代码
    if(event->button()==Qt::RightButton){
        aboutMenu->popup(event->globalPos());
    }
    //左击有QDesigner生成的菜单
    /*这段代码会和下面的void MainWindow::mouseMoveEvent(QMouseEvent *event){
    MouseX->setNum(event->x());
    MouseY->setNum(event->y());
    }
        的代码起冲突。
*/
  /*  if(event->button()==Qt::LeftButton){
        ui->menu1->popup(event->globalPos());
    }*/

}
/*刷新之前显示上面的格式,刷新后显示下面的格式*/
void MainWindow::timerEvent(QTimerEvent *event){
    QTime Current_Time=QTime::currentTime();
    mytoolBarTime->display(Current_Time.toString("HH:mm:ss"));
}
void MainWindow::mouseMoveEvent(QMouseEvent *event){
    MouseX->setNum(event->x());
    MouseY->setNum(event->y());
}
void MainWindow::activated(QSystemTrayIcon::ActivationReason reason){
    switch(reason){
    case QSystemTrayIcon::Trigger:this->hide();
        break;
    case QSystemTrayIcon::DoubleClick:this->showNormal();
        break;
    default:break;
    }
}



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值