mainwindow.h#ifndef MAINWINDOW_H#define MAINWINDOW_H#include <QtGui/QMainWindow>#include <QDialog.h>#include <QListView>#include <QStringListModel>#include <QHBoxLayout>#include <QGroupBox>#include <QDateTimeEdit>#include <qlabel>#include <QFile>#include <QTextStream>#include <QSystemTrayIcon.h>#include <qevent.h>#include <iostream.h>class QStringList;class QComboBox;class QListView;class QLineEdit;class QHBoxLayout;namespace Ui{ class MainWindowClass;}class MainWindow : public QMainWindow{ Q_OBJECTpublic: MainWindow(QWidget *parent = 0); ~MainWindow(); void setEnabled(bool enabled);private: QWidget *win; QGroupBox *groupBox_1; QGroupBox *groupBox_2; Ui::MainWindowClass *ui; QLineEdit *transkRip; QPushButton *addButton; QPushButton *delButton; QPushButton *closeButton; QPushButton *insertButton; QComboBox *slovoBox; QListView *pereVod; QDateTimeEdit *timeEdit; QStringListModel *model; QStringList *list; QVBoxLayout *layout_left; QVBoxLayout *layout_right; QHBoxLayout *addLayout_left; QHBoxLayout *mainLayout; QLabel *slovoLabel; QSystemTrayIcon *tray; void insert(); public slots:void slotPlaceToTray();int input();signals:void signalPlaceToTray();protected:void changeEvent(QEvent*);void closeEvent(QCloseEvent*);};#endif // MAINWINDOW_H
mainwindow.cpp#include "mainwindow.h"#include "ui_mainwindow.h"#include <QtGui>MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindowClass){ ui->setupUi(this);win=new QWidget; slovoBox=new QComboBox(win); slovoBox->setEditable(true); transkRip=new QLineEdit(win); timeEdit= new QTimeEdit(win); pereVod= new QListView(win); slovoLabel=new QLabel(tr("&Slovo")); slovoLabel->setBuddy(slovoBox); list= new QStringList; *list<<"a"<<"b"<<"c"; model=new QStringListModel (*list); pereVod->setModel(model); pereVod->setSelectionMode(QAbstractItemView::ExtendedSelection); tray= new QSystemTrayIcon(QIcon(":/icon/apply.png"),this); closeButton= new QPushButton ("Close",win); addButton=new QPushButton(tr("Add"),win); delButton=new QPushButton(tr("Del"),win); ....... win->resize(550,300); win->show(); connect(this,SIGNAL(signalPlaceToTray()),this,SLOT(slotPlaceToTray()),Qt::QueuedConnection); connect(closeButton,SIGNAL(clicked()),win,SLOT(close())); connect(addButton, SIGNAL(clicked()), this, SLOT(input()));}
minimizetray.h#include "mainwindow.h"void MainWindow::closeEvent(QCloseEvent *event){ if(tray->isVisible()) { hide(); event->ignore(); }}void MainWindow::changeEvent(QEvent *event){ if(event->type()==QEvent::WindowStateChange) { if(isMinimized()) { event->ignore(); emit signalPlaceToTray(); return; } } QMainWindow::changeEvent(event);}void MainWindow::slotPlaceToTray(){ tray->show(); hide();}void MainWindow::insert() { int row=pereVod->currentIndex().row(); model->insertRows(row,1); QModelIndex index=model->index(row); pereVod->setCurrentIndex(index); pereVod->edit(index); }int MainWindow::input(){ QFile file("sha.txt");if(!file.open(QIODevice::WriteOnly)){ cout<<"Cannot open"<<qPrintable(file.errorString())<<endl;return -1;}QTextStream out(&file);out<<slovoBox;}MainWindow::~MainWindow(){}
minimizetray.hvoid MainWindow::slotPlaceToTray(){ tray->show(); /*hide(); Так было*/ this->close(); // Так надо}
C++ (Qt)if(tray->isVisible())
#ifndef MAINWINDOW_H#define MAINWINDOW_H#include <QMainWindow>#include <QSystemTrayIcon>#include "ui_mainwindow.h"class MainWindow : public QMainWindow, public Ui::TestMainWindow{ Q_OBJECT public: MainWindow(QWidget *parent = 0); protected: void closeEvent(QCloseEvent *event); private slots: void iconActivated(QSystemTrayIcon::ActivationReason reason); private: QAction *minimizeAction; //QAction *maximizeAction; QAction *restoreAction; QAction *quitAction; QSystemTrayIcon *trayIcon; QMenu *trayIconMenu;};#endif // MAINWINDOW_H
#include <QtGui>#include "mainwindow.h"MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent){ setupUi(this); //******************Create TrayIcon Menu*************************************** minimizeAction = new QAction(tr("Mi&nimize"), this); connect(minimizeAction, SIGNAL(triggered()), this, SLOT(hide())); //maximizeAction = new QAction(tr("Ma&ximize"), this); //connect(maximizeAction, SIGNAL(triggered()), this, SLOT(showMaximized())); restoreAction = new QAction(tr("&Restore"), this); connect(restoreAction, SIGNAL(triggered()), this, SLOT(showNormal())); quitAction = new QAction(tr("&Quit"), this); connect(quitAction, SIGNAL(triggered()), qApp, SLOT(quit())); trayIconMenu = new QMenu(this); trayIconMenu->addAction(minimizeAction); //trayIconMenu->addAction(maximizeAction); trayIconMenu->addAction(restoreAction); trayIconMenu->addSeparator(); trayIconMenu->addAction(quitAction); trayIcon = new QSystemTrayIcon(this); trayIcon->setContextMenu(trayIconMenu); //******************************************************************************** connect(trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)), this, SLOT(iconActivated(QSystemTrayIcon::ActivationReason))); trayIcon->setIcon(QIcon(":/icons/Download.png")); setWindowIcon(QIcon(":/icons/Download.png")); trayIcon->show(); }void MainWindow::closeEvent(QCloseEvent *event){ if (trayIcon->isVisible()) { hide(); event->ignore(); }}void MainWindow::iconActivated(QSystemTrayIcon::ActivationReason reason){ /*switch (reason) { case QSystemTrayIcon::Trigger: case QSystemTrayIcon::DoubleClick: iconComboBox->setCurrentIndex((iconComboBox->currentIndex() + 1) % iconComboBox->count()); break; case QSystemTrayIcon::MiddleClick: showMessage(); break; default: ; }*/}
#include <QApplication>#include "ui/mainwindow.h"int main(int argc, char *argv[]){ QApplication app(argc,argv); MainWindow *window = new MainWindow; window->hide(); return app.exec();}