当前位置:网站首页>QT realization tray

QT realization tray

2022-06-12 07:22:00 Big 1234 grass

Preface

I was looking for a job , It took me a few days to get to know Qt. at that time , The training document is here :QT A little experiment - Turn gold coin games .

Some time ago , Modified the company's pallet code , The original code structure design is very technical .

lately , I also saw the tray code written by another colleague , He separates the interface from the function , The overall quality of the code is pretty good .

My hands itch , So in my spare time, I wrote a Qt Tray demo.

See warehouse for complete code :Qt/tray. This code is written with reference to :Qt Official tray code example feiyangqingyun-QWidgetDemo-trayicon

The results are shown in the following figure :

 Insert picture description here


overall design

  1. The tray has a right-click menu .
  2. The tray has an icon .
  3. The tray is associated with an interface .

Detailed design

There are sample codes to refer to , The rest is to call API, Difficulty is not great . Key codes are shown below .

Writing pallets may be the following three key points :

  1. Setting of the tray itself . Including icon switching , Tips, etc .
  2. Right-click menu .
  3. Code structure relationship between interface and tray .
// tray.h

class tray : public QDialog
{
    Q_OBJECT

private:
    std::shared_ptr<QSystemTrayIcon> trayIcon; //  Menu icons 
    std::shared_ptr<QMenu> trayMenu; //  Menu context 
    std::shared_ptr<QAction> showAction; //  Display options 
    std::shared_ptr<QAction> versionAction; //  Version options in the menu 
    std::shared_ptr<QAction> quitAction; //  Exit options in the menu 

    std::shared_ptr<mainWindow> windowInterface; //  Main window interface 
    bool isShowVersion = false; //  Currently, the version interface is being displayed 

private slots:
    void show_version(void);
    void exit(void);

public:
    tray(QWidget *parent = nullptr);
    void createAction(void);
    void createIcon(void);
    ~tray();
};
// tray.cpp
#include "tray.h"

tray::tray(QWidget *parent)
    : QDialog(parent)
{
    QApplication::setQuitOnLastWindowClosed(false); //  Don't exit the program implicitly because there is no window , Click the exit option in the tray to exit 

    //  avoid connect Cannot connect to this object when , First, create all the objects in the first layer 
    windowInterface.reset(new mainWindow(this));

    createAction();
    createIcon();

    setVisible(false); //  By inheritance QDialog, If there is no such line , A dialog box will pop up when running 

    trayIcon->show(); //  Display tray icon 
    windowInterface->show();
}


void tray::createAction()
{
    showAction.reset(new QAction(QString(" display "), this));
    connect(showAction.get(), &QAction::triggered, windowInterface.get(), &mainWindow::show);

    versionAction.reset(new QAction(QString(" edition "), this));
    connect(versionAction.get(), &QAction::triggered, this, &tray::show_version);

    quitAction.reset(new QAction(QString(" sign out "),this));
    connect(quitAction.get(), &QAction::triggered, this, &tray::exit);
}


void tray::createIcon()
{
    trayMenu.reset(new QMenu(this));
    trayMenu->addAction(showAction.get());
    trayMenu->addAction(versionAction.get());
    trayMenu->addAction(quitAction.get());
    trayIcon.reset(new QSystemTrayIcon(this));

    trayIcon.reset(new QSystemTrayIcon(this));
    trayIcon->setIcon(QIcon(":/image/flow.ico"));
    trayIcon->setContextMenu(trayMenu.get());
}


void tray::show_version()
{
    if(isShowVersion) {
        return;
    }

    isShowVersion = true;
    QMessageBox versionInterface = QMessageBox(this);
    versionInterface.setText("Tray's version is 0.0.0");

    //  If there is only one button , Shut down and escap key This button will also be used by default . So let's add another one that doesn't show no Button 
    versionInterface.setStandardButtons(QMessageBox::Ok);
    versionInterface.button(QMessageBox::Ok)->setText("Qt edition ");
    versionInterface.addButton(QMessageBox::No)->hide();

    int ret = versionInterface.exec(); //  Use the modal dialog 
    if(ret == QMessageBox::Ok) {
        QApplication::aboutQt();
    }
    isShowVersion = false;
}


void tray::exit()
{
    trayIcon->hide(); // QSystemTrayIcon No, close function , call hide
    QApplication::quit(); //  Exit procedure 
}


tray::~tray()
{
}

Qt Some small points of

At present, there is no established system for Qt Face recognition of , Check it when you use it .

At present, there is no system to see Qt Books , It doesn't seem necessary . because , I don't care about the details of a dialog box or icon , Just check these contents when you use them . What I need is a strategic view of Qt Introduction to its structure .( It doesn't seem necessary , If only the tool could be used .)

Recommended information :feiyangqingyun/QWidgetDemo

below , Let me introduce the code details above .

  1. I used a lot of smart pointers in the above code . stay C++ in , Objects created on the heap , Manage with smart pointers , It's a good habit . But in Qt in , It doesn't seem to have to be exactly so . We can use more Object tree .
  2. To write tray Class time , We let it inherit QDialog class . Turn around and think about , If we don't inherit QDialog Class ? What we write is the tray code , There seems to be no need for dialog functions . We really don't need the dialog function , But we need to QObject The function of . You can refer to QObject Detailed explanation
  3. Q_OBJECT Macros must appear in declaring their own signals and slots or using Qt In the private part of the class definition of other services provided by the meta object system . If you use cmake As a building system , We'll see set(CMAKE_AUTOMOC ON). We went to install Qt Can also be found in the directory of moc.exe Executable program . You can refer to Qt Meta object system
  4. QMessageBox in , I use the exec Method , Instead of using show Method . The difference between the two is blocking and non blocking . You can refer to : modal dialog. And about the QMessageBox Use , I don't like reading English , You can refer to Qt QMessageBox Usage details
  5. For programs with trays , When the last page closes , The program does not exit , Need to set up setQuitOnLastWindowClosed by false.
  6. When using signals and slot functions , We need to make sure that the object of the slot function has been created , Otherwise, it defaults to null pointer . Introduction to signals and slot functions , You can refer to Signals & Slots
  7. Set up QMessageBox Button time , without QMessageBox::No Button time , It is recommended to add a hidden button . This is also a helpless way . because , When there is no setting QMessageBox::escapeButton Function , Press down esc key , If only one button is set at this time , This button function will be used by default . close button , Tested under , So it is with .
原网站

版权声明
本文为[Big 1234 grass]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/163/202206120717139817.html