Qt個性化系統托盤的實現(電腦管家)

Qt實現個性化系統托盤


  今天剛好有點時間,就想着模仿下電腦管家的系統化托盤,看着挺好看的。下邊是電腦管家自帶的系統托盤:

  這裏寫圖片描述

  可以看出主要是由三部分組成的,分爲上部,中間和底部,上邊是
2個label,加2個圖標label;中間是6個按鈕,文字顯示在底部,只能是QToolButton了,QPushButton辦不到;底部也是2個圖標label加2個開關按鈕(開關按鈕需要自己實現狀態切換),最底部用3個QPushButton就可以了。

  
好了,有了上面的分析就可以開始進行設計了。

  1. 新建一個工程,名字就叫ComputerManager吧,類名和其他不變,Widget吧。本次將系統托盤的實現獨立出來,封裝成一個類。
  2. 新建一個C++類,作爲系統托盤的具體實現,叫ComputerManager,繼承自QSystemTrayIcon,源碼中有詳細介紹。

    private:

    void initTopAction(); //初始化頂部菜單

    void initMidAction(); //初始化中間菜單

    void initBottomAction(); //初始化底部菜單

    void initAction(); //初始化Action

    void addActions(); //將Action添加到Qmenu上

    void initConnect(); //初始化信號和槽的連接

  3. 初始化頂部菜單和Action

void ComputerManager::initTopAction()
{
    //頂層菜單項初始化
    m_topWidget = new QWidget();
    m_topWidgetAction = new QWidgetAction(m_trayMenu);
    m_topLabelIcon = new QLabel();
    m_topLabelIcon->setPixmap(QPixmap(":/image/topicon.png"));
    m_topLabelIcon->setFixedSize(30,30);

    m_topLabel = new QLabel(tr("Comuter Manager now protected you!"));
    m_topLabel->setObjectName(QString("WhiteLabel"));
    m_topLabel->setFont(QFont("Times", 11));
    QHBoxLayout* m_topLayout = new QHBoxLayout();
    m_topLayout->addWidget(m_topLabelIcon);
    m_topLayout->addWidget(m_topLabel,1,Qt::AlignHCenter | Qt::AlignVCenter);
    m_topLayout->setContentsMargins(0,0,0,0);
    m_topWidget->setLayout(m_topLayout);
    m_topWidget->setMinimumHeight(70);
    m_topWidget->installEventFilter(this);
    m_topWidgetAction->setDefaultWidget(m_topWidget);


    m_topQQWidget = new QWidget();
    m_topQQWidgetAction = new QWidgetAction(m_trayMenu);
    m_QQInfoBtn = new QLabel(tr("qq infor safety\n")+
                             tr("reinforce more secure"));
    m_QQInfoBtn->setCursor(Qt::PointingHandCursor);
    m_labelIcon = new QLabel();
    m_labelIcon->setPixmap(QPixmap(":/image/icon.png"));
    m_labelIcon->setFixedSize(50,50);

    QHBoxLayout* m_topQQLayout = new QHBoxLayout();
    m_topQQLayout->addWidget(m_labelIcon);
    m_topQQLayout->addWidget(m_QQInfoBtn);
    m_topQQLayout->setContentsMargins(0,0,0,0);
    m_topQQWidget->setLayout(m_topQQLayout);
    m_topQQWidget->setMinimumHeight(70);
    m_topQQWidget->installEventFilter(this);
    m_topQQWidgetAction->setDefaultWidget(m_topQQWidget);


    m_topTbnWidget = new QWidget();
    m_topTbnWidgetAction = new QWidgetAction(m_trayMenu);

    m_tbnOpenSteward = new QToolButton();
    m_tbnOpenSteward->setIcon(QIcon(":/image/1.png"));
    m_tbnOpenSteward->setIconSize(QSize(50,50));
    m_tbnOpenSteward->setAutoRaise(true);
    m_tbnOpenSteward->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);

    m_tbnHome = new QToolButton();
    m_tbnHome->setIcon(QIcon(":/image/2.png"));
    m_tbnHome->setIconSize(QSize(50,50));
    m_tbnHome->setAutoRaise(true);
    m_tbnHome->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);

    m_tbnAntiVirus = new QToolButton();
    m_tbnAntiVirus->setIcon(QIcon(":/image/3.png"));
    m_tbnAntiVirus->setIconSize(QSize(50,50));
    m_tbnAntiVirus->setAutoRaise(true);
    m_tbnAntiVirus->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);


    m_tbnCleanRubbish = new QToolButton();
    m_tbnCleanRubbish->setIcon(QIcon(":/image/4.png"));
    m_tbnCleanRubbish->setIconSize(QSize(50,50));
    m_tbnCleanRubbish->setAutoRaise(true);
    m_tbnCleanRubbish->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);

    m_tbnComputerSpeed = new QToolButton();
    m_tbnComputerSpeed->setIcon(QIcon(":/image/5.png"));
    m_tbnComputerSpeed->setIconSize(QSize(50,50));
    m_tbnComputerSpeed->setAutoRaise(true);
    m_tbnComputerSpeed->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);

    m_tbnComputerClinic = new QToolButton();
    m_tbnComputerClinic->setIcon(QIcon(":/image/6.png"));
    m_tbnComputerClinic->setIconSize(QSize(50,50));
    m_tbnComputerClinic->setAutoRaise(true);
    m_tbnComputerClinic->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);

    QHBoxLayout *layout1 = new QHBoxLayout();
    layout1->addWidget(m_tbnOpenSteward);
    layout1->addWidget(m_tbnHome);
    layout1->setContentsMargins(0,0,0,0);
    QHBoxLayout *layout2 = new QHBoxLayout();
    layout2->addWidget(m_tbnAntiVirus);
    layout2->addWidget(m_tbnCleanRubbish);
    layout2->setContentsMargins(0,0,0,0);
    QHBoxLayout *layout3 = new QHBoxLayout();
    layout3->addWidget(m_tbnComputerSpeed);
    layout3->addWidget(m_tbnComputerClinic);
    layout3->setContentsMargins(0,0,0,0);
    QVBoxLayout *mainLout = new QVBoxLayout();
    mainLout->addLayout(layout1);
    mainLout->addLayout(layout2);
    mainLout->addLayout(layout3);
    mainLout->setContentsMargins(0,0,0,0);
    m_topTbnWidget->setLayout(mainLout);
    m_topTbnWidgetAction->setDefaultWidget(m_topTbnWidget);
}

4 初始化中間菜單和Action

void ComputerManager::initMidAction()
{
    m_midWidget = new QWidget();
    m_midWidgetAction = new QWidgetAction(m_trayMenu);

    m_speedLabel = new QLabel(tr("Auto backup"));
    m_SpeedBtn = new mySwitchButton();
    m_SpeedBtn->setFixedSize(60,25);
    m_SpeedBtn->setObjectName(QString("TrayButtonAuto"));
    m_SpeedBtn->SetCheck(true);

    QHBoxLayout *m_midUpLayout = new QHBoxLayout();
    m_midUpLayout->addWidget(m_speedLabel);
    m_midUpLayout->addWidget(m_SpeedBtn);


    m_troubleLabel = new QLabel(tr("Auto Start"));
    m_troubleLabel->setToolTip(tr("computer now "));
    m_TroubleBtn = new mySwitchButton();
    m_TroubleBtn->setFixedSize(60,25);
    m_TroubleBtn->setObjectName(QString("TrayButtonAuto"));
    m_TroubleBtn->SetCheck(true);

    QHBoxLayout *m_midBottomLayout = new QHBoxLayout();
    m_midBottomLayout->addWidget(m_troubleLabel);
    m_midBottomLayout->addWidget(m_TroubleBtn);


    QVBoxLayout *m_midMainLayout = new QVBoxLayout();
    m_midMainLayout->addLayout(m_midUpLayout);
    m_midMainLayout->addLayout(m_midBottomLayout);

    m_midWidget->setLayout(m_midMainLayout);
    m_midWidgetAction->setDefaultWidget(m_midWidget);
}

5 初始化底部菜單和Action

void ComputerManager::initBottomAction()
{
m_bottomWidget = new QWidget();
m_bottomWidgetAction = new QWidgetAction(m_trayMenu);

m_ReportBtn = new QPushButton(QIcon(":/image/mainMenu/update.png"), tr("Update"));
m_ReportBtn->setObjectName(QString("TrayButton"));
m_ReportBtn->setFixedSize(60, 25);

m_InformBtn = new QPushButton(QIcon(":/image/mainMenu/about.png"), tr("About"));
m_InformBtn->setObjectName(QString("TrayButton"));
m_InformBtn->setFixedSize(60, 25);

m_exitBtn = new QPushButton(QIcon(":/image/mainMenu/quit.png"), tr("Exit"));
m_exitBtn->setObjectName(QString("TrayButton"));
m_exitBtn->setFixedSize(60, 25);

QHBoxLayout* m_bottomLayout = new QHBoxLayout();
m_bottomLayout->addWidget(m_ReportBtn, 0, Qt::AlignCenter);
m_bottomLayout->addWidget(m_InformBtn, 0, Qt::AlignCenter);
m_bottomLayout->addWidget(m_exitBtn, 0, Qt::AlignCenter);

m_bottomLayout->setSpacing(5);
m_bottomLayout->setContentsMargins(5,5,5,5);

m_bottomWidget->setLayout(m_bottomLayout);
m_bottomWidgetAction->setDefaultWidget(m_bottomWidget);

}


6 **將所創建的Action添加到QMenu中**


    void ComputerManager::initAction()
{
    this->setToolTip(tr("Computer Manager"));
    this->setIcon(QIcon(":/image/system_tray.png"));
    m_trayMenu = new QMenu();

    this->setContextMenu(m_trayMenu);
}


7 **外部調用**
m_systemTray = new ComputerManager(this);

“`

m_systemTray->show();

Qt利用語言家實現多語言翻譯

軟件開發中可以使用Qt強大的語言家進行翻譯,保證不會出現大家所謂的亂碼問題。
這裏寫圖片描述

完成後的樣子如下圖所示:
這裏寫圖片描述

在來一張

這裏寫圖片描述

源碼下載地址:

http://download.csdn.net/detail/u013704336/9229231

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章