How to make a dynamic update of ToolTip'a on a track in Qt
-
How to make a dynamic change in data ToolTipon the hip in three?
After every change in the variable, the dynamics must be valued without reloading the program.
void Tray::showTrayIcon() { MainWindow w;
//Стопорим программу для обработки
QEventLoop loop;
QTimer::singleShot(1000, &loop, SLOT(quit()));
loop.exec();QIcon trayImage(":/images/images/tray.png");
trayIcon -> setIcon(trayImage);
trayIcon -> setContextMenu(trayIconMenu);balanceUser = w.searchBalance();
trayIcon->setToolTip(tr("Баланс: %1").arg(balanceUser));// Подключаем обработчик клика по иконке...
//connect(trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)), this, SLOT(trayIconActivated(QSystemTrayIcon::ActivationReason)));// Выводим значок...
trayIcon -> show();if(balanceUser < 0)
trayIcon->showMessage("Отрицательный баланс", "На балансе нету средств, "
"пополните счёт если хотите продолжать пользоваться услугами!");
}
-
There may be some likely solutions, I suggest two:
- Simple.
Keep the index to the facility.
QSystemTrayIcon
in your class objectMainWindow
(or somewhere where you're comfortable).In the timer (or by some external signal), you have an updated balance value and give the method:
systray_->setToolTip(QString("Баланс %1 у.е").arg(newBalance));
- More complicated.
Trace from class
QSystemTrayIcon
and determine the slot:public slots: void slotUpdateBalance(int newBalance) { setToolTip(QString("На балансе %1 у.е").arg(newBalance)); // Здесь можно вызвать showMessage(...), сохранить новое значение баланса или выполнить ещё какие-нибудь важные действия }
In your class.
MainWindow
Signalsignals: void balanceUpdated(int newBalance);
Monitor the event of a change in balance (e.g. timer) and release the signal:
emit balanceUpdated(newBalance);
We'connect the signal with the slot:
connect(mainwindow, SIGNAL(balanceUpdated(int)), systemtray, SLOT(slotUpdateBalance(int)));
Example code:
class MainWindow { MainWindow() : balance_(getBalance()), systemtray_(new YourSystemTray(this)) { // Особенности вашей реализации. // ... // Затем:
connect(this, SIGNAL(balanceUpdated(int)), systemtray_, SLOT(slotUpdateBalance(int))); QTimer* timer = new QTimer(this); connect(timer, SIGNAL(timeout()), this, SLOT(slotCheckBalance())); timer->start(1000);
}
signals:
void balanceUpdated(int newBalance);private slots:
void slotCheckBalance()
{
int newBalance = getBalance();
if (balance_ != newBalance) {
balance_ = newBalance;
emit balanceUpdated(balance_);
}
}private:
int getBalance(); // Предполагается, что здесь вы получаете текущее значение балансаint balance_; YourSystemTray* systemtray_;
};
class YourSystemTray : public QSystemTrayIcon
{
// Ваша реализацияpublic slots:
void slotUpdateBalance(int newBalance)
{
setToolTip(QString("На балансе %1 у.е.").arg(newBalance));
}
}