Results 1 to 1 of 1

Thread: Cannot access QSystemTrayIcon object of MainWindow class from other classes

  1. #1
    Join Date
    Apr 2020
    Location
    Australia Victoria
    Posts
    3
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Question Cannot access QSystemTrayIcon object of MainWindow class from other classes

    Hi,

    The purpose of my program is to run all the time and update a real time sqlite db with half hour aggregates of the raw 2 second data.

    The program has the following elements

    1. simple gui which can be minimized to the windows system tray which has a context menu attached to it.


    1. database update class that provides for all of the db functions which are controlled by a worker object with the main
      process being an infinite loop with a wait function that provides sets the loop repeat time.


    There is no requirement for multiple objects of any class as only one instance of any class is required at a time.

    The program works fine for the core functionality with all of the database functions ok. The system tray and the minimize to tray and restore functions are all sweet.

    The problem:

    I use the system tray object message system to bring up notifications of the programs state such as database update running/stopped.
    When I try and reference this function from the MainWindow class I get a compile error:


    Qt Code:
    1. void MainWindow::dbErrorNotifyTray(QString TitleSuffix,
    2. QString MessageSuffix,
    3. int errNumber)
    4. { ...
    5.  
    6. if(mSystemTrayIcon->isVisible()) error: invalid use of member in static member function
    7. mSystemTrayIcon->showMessage(nRec.MainHeading, nRec.Message, error: invalid use of member in static member function
    8. QIcon(nRec.IconURLPath),
    9. 2000);
    10. ...
    11. }
    To copy to clipboard, switch view to plain text mode 

    My header file for the MainWindow class mainwindow.h

    is shown below. The function definition for the dbErrorNotifyTray method is found on line 50.

    mainwindow.h
    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include <QMainWindow>
    5. #include <QObject>
    6. ...
    7.  
    8. #include <QSystemTrayIcon>
    9. #include <QMenu>
    10. #include <QAction>
    11. #include <QMessageBox>
    12. #include <QCloseEvent>
    13.  
    14.  
    15. QT_BEGIN_NAMESPACE
    16. namespace Ui
    17. {
    18. class MainWindow;
    19. }
    20. QT_END_NAMESPACE
    21.  
    22. class MainWindow : public QMainWindow
    23. {
    24. Q_OBJECT
    25.  
    26. public:
    27. MainWindow(QWidget *parent = nullptr);
    28. ~MainWindow();
    29.  
    30. protected:
    31. void closeEvent(QCloseEvent *event) override;
    32.  
    33. signals:
    34. void startWork_mwSig();
    35. void stopWork_mwSig();
    36.  
    37. private slots:
    38. void on_pbStartThread_clicked();
    39. void on_pbStopThread_clicked();
    40. void dbErrorFcn_Slot(QString errstr);
    41.  
    42. void on_actionClose_triggered();
    43.  
    44. void on_actionAbout_Energy_Server_triggered();
    45.  
    46. void createTrayIconAndMenu();
    47. void createTrayActions();
    48.  
    49. public:
    50. static void dbErrorNotifyTray(QString TitleSuffix,
    51. QString MessageSuffix,
    52. int errNumber);
    53. public:
    54. Ui::MainWindow *ui;
    55. QSystemTrayIcon *mSystemTrayIcon;
    56.  
    57. private:
    58. QMenu *trayIconMenu;
    59. QAction *minimizeAction; ...
    60.  
    61.  
    62. worker *myWorker;
    63. QThread *threadSQL;
    64. bool closing;
    65.  
    66. ...
    67. };
    68.  
    69. #endif // MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 


    The MainWindow implementation file which contains the problem code is shown below with the code producing the compile error on lines 68 and 69
    mainwindow.cpp
    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3. #include "dbupdate.h"
    4.  
    5. class Sleeper : public QThread
    6. {
    7. public:
    8. ...
    9. };
    10.  
    11.  
    12. MainWindow::MainWindow(QWidget *parent)
    13. : QMainWindow(parent)
    14. , ui(new Ui::MainWindow)
    15. {
    16. ui->setupUi(this);
    17.  
    18. createTrayActions();
    19. createTrayIconAndMenu();
    20.  
    21. ui->pbStopThread->setEnabled(false);
    22. ui->pbStartThread->setEnabled(true);
    23.  
    24. myWorker = new worker();
    25. threadSQL = new QThread;
    26.  
    27. myWorker->moveToThread(threadSQL);
    28. threadSQL->start();
    29. threadSQL->setPriority(QThread::NormalPriority);
    30.  
    31. connect(this, SIGNAL(stopWork_mwSig()), myWorker, SLOT(StopWorkFcn()));
    32. connect(this, SIGNAL(startWork_mwSig()), myWorker, SLOT(StartWorkFcn()));
    33. connect(myWorker, SIGNAL(error_dbTaskSig(QString)), this, SLOT(dbErrorFcn_Slot(QString)));
    34. }
    35.  
    36. MainWindow::~MainWindow()
    37. {
    38. delete ui;
    39. }
    40.  
    41. void MainWindow::on_pbStartThread_clicked()
    42. {
    43. ...
    44.  
    45.  
    46. void MainWindow::closeEvent(QCloseEvent *event)
    47. {
    48. if(mSystemTrayIcon->isVisible())
    49. mSystemTrayIcon->showMessage("Power Monitor Energy Server",
    50. "Minimized to system tray.",
    51. QIcon(":/images/bluewren9mmhrBlackOnGreyNotRunning.png"),
    52. 2000);
    53. if(closing)
    54. {
    55. ...
    56. }
    57.  
    58. void MainWindow::dbErrorNotifyTray(QString TitleSuffix,
    59. QString MessageSuffix,
    60. int errNumber)
    61. {
    62. typedef struct
    63. {
    64. ...
    65. } noteRecord_t;
    66. ...
    67.  
    68. if(mSystemTrayIcon->isVisible())
    69. mSystemTrayIcon->showMessage(nRec.MainHeading, nRec.Message,
    70. QIcon(nRec.IconURLPath),
    71. 2000);
    72. NOTIFY_ERROR:
    73. db.close();
    74. }
    75.  
    76. void MainWindow::createTrayIconAndMenu()
    77. {
    78. mSystemTrayIcon = new QSystemTrayIcon(this);
    79. mSystemTrayIcon->setIcon(QIcon(":/images/bluewren9mmhrBlackOnGreyNotRunning.png"));
    80. mSystemTrayIcon->setVisible(true);
    81. mSystemTrayIcon->setToolTip("Power Monitor Energy Server");
    82. trayIconMenu = new QMenu(this); ...
    83.  
    84. ...
    85. mSystemTrayIcon->setContextMenu(trayIconMenu);
    86. }
    87.  
    88. void MainWindow::createTrayActions()
    89. {
    90. ...
    91. }
    92. ...
    To copy to clipboard, switch view to plain text mode 

    The compiler is complaining about using the mSystemTrayIcon object in a static function, however if I make the function non-static:
    Qt Code:
    1. void dbErrorNotifyTray(QString TitleSuffix,
    2. QString MessageSuffix,
    3. int errNumber);
    To copy to clipboard, switch view to plain text mode 

    then i get a compile error in my dbupdate class:
    Qt Code:
    1. MainWindow::dbErrorNotifyTray("", "", errNumber);
    2.  
    3.  
    4. error: cannot call member function 'void MainWindow::dbErrorNotifyTray(QString, QString, int)' without object
    5. MainWindow::dbErrorNotifyTray("", "", errNumber);
    To copy to clipboard, switch view to plain text mode 

    The header file for the dbupdate class and the first part of the implementation are shown below:
    dbupdate.h
    Qt Code:
    1. #ifndef DBUPDATE_H
    2. #define DBUPDATE_H
    3.  
    4.  
    5. #include <QObject>
    6. ...
    7. #include "mainwindow.h"
    8.  
    9.  
    10. typedef struct
    11. {...
    12. } energyRecord_t;
    13.  
    14. typedef struct
    15. {...
    16. } energyRecInput_t;
    17.  
    18. ...
    19.  
    20. class dbupdate : public QObject
    21. {
    22. Q_OBJECT
    23. public:
    24. explicit dbupdate(QObject *parent = nullptr);
    25.  
    26. signals:
    27. void sendMsgToNotify(QString TitleSuffix,
    28. QString MessageSuffix,
    29. int errNumber);
    30.  
    31. private slots:
    32.  
    33. static bool agregateData();
    34. ...
    35.  
    36. static QString dbErrorHandler(QString errStr,
    37. QSqlError eQuery,
    38. QSqlError eEnergyDb,
    39. QSqlError ePMonDb,
    40. int errNumber,
    41. bool returnErrString);
    42. ...
    43.  
    44. public slots:
    45. static double getNextHalfHourTimeFromClock();
    46. ...
    47.  
    48. public:
    49. static QSqlDatabase dbPMon;
    50. static QSqlDatabase dbEnergy;
    51. double nextHalfHourClock;
    52. double nextHalfHourDB;
    53. };
    54.  
    55. #endif // DBUPDATE_H
    To copy to clipboard, switch view to plain text mode 

    The implementation of the dbupdate (just the first part) is shown below
    dbupdate.cpp
    Qt Code:
    1. #include "dbupdate.h"
    2. #include "juliandate.h"
    3. #include "mainwindow.h"
    4. #include <QSystemTrayIcon>
    5.  
    6. #ifndef STR_EQUAL
    7. #define STR_EQUAL 0
    8. #endif
    9.  
    10. #ifndef PRAGMA_OK
    11. #define PRAGMA_OK "ok"
    12. #endif
    13.  
    14. QSqlDatabase dbupdate::dbPMon;
    15. QSqlDatabase dbupdate::dbEnergy;
    16.  
    17.  
    18. /*------------------------------------------------------------------------------------------------------*/
    19. dbupdate::dbupdate(QObject *parent) : QObject(parent)
    20. {
    21.  
    22. }
    23. ...
    To copy to clipboard, switch view to plain text mode 

    So I am between a rock and a hard place. I have spent five days trying to get this to work, I've also tried using signals and slots, but I always end up with the same issue of one class.

    I was almost tempted to use global variables and a timer running as an object of the Main Window, but I thought this to be something that even an embedded c programmer like myself wouldn't do.

    I think my problem is caused by not constructing an instance of one of the classes, but although I have tried this I haven't been able to get this to work.

    Most of the examples given about C++ Qt C++ only provide trivial examples which don't seem to bear much relationship with more complex code.

    Can anyone suggest how I might tackle this problem as I am at a complete standstill.

    I have attached a zip of the project to assist. Some files such as images and the databases are deleted as they make the file too large.
    Attached Files Attached Files

    Best regards
    Rob

Similar Threads

  1. access to another ui object from mainwindow
    By rapid84 in forum Qt Programming
    Replies: 2
    Last Post: 23rd January 2016, 12:57
  2. Replies: 3
    Last Post: 8th December 2015, 22:20
  3. show object (scene) in widget in class class mainwindow
    By rimie23 in forum Qt Programming
    Replies: 8
    Last Post: 1st May 2012, 16:15
  4. Replies: 3
    Last Post: 12th April 2011, 10:58
  5. Replies: 4
    Last Post: 17th January 2011, 14:05

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.