Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. How to use QSetting or QApplication to access set/get global variables?
Forum Updated to NodeBB v4.3 + New Features

How to use QSetting or QApplication to access set/get global variables?

Scheduled Pinned Locked Moved Solved Mobile and Embedded
5 Posts 2 Posters 1.7k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    NTCYP
    wrote on last edited by NTCYP
    #1

    Hi,

    I have QT ui_mainwindow and I also have ui_DialogWifi GUI window.

    Now form ui_DialogWifi I connect to WiFi hotspot. Once its connected I need to set some global variable to true such as;

    wifiConnected = true;
    

    My ping operation also in ui_DialogWifi. Once the ping esatblish means user knows that wifi is connected so user close the ui_DialogWifi and return to ui_mainwindow.

    In ui_mainwindow I have a function that checks if the wifi is on or off. My problem is how to I use QSetting or QApplication to crate a global variable such as bool wifiConnected; so I can access from ui_mainwindow and ui_DialogWifi?

    Any help please? Kind Regards.

    Here is my code:

    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
    
       // SHOW WIFI LOGO 
       WifiLogoTimer = new QTimer(this);
       connect(WifiLogoTimer, SIGNAL(timeout()),this, SLOT(showWifiLogo()));
       WifiLogoTimer->start(300);
    }
    
    void MainWindow::showWifiLogo()
    {
        // WIFI LOGO
        if(wifiConnected == true)
        {
             ui->labelWifiLogo->setPixmap(QPixmap("/home/res/wifiOn.png"));
        }
        else if(wifiConnected == false)
        {
             ui->labelWifiLogo->setPixmap(QPixmap("/home/res/wifiOff.png"));
        }
    }
    
    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Global variables like that one are usually sign of bad design. A better solution would be using signals and slots. Have your DialogWifi emit a signal when it finds the WiFi is connected or when It loses it.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      N 1 Reply Last reply
      0
      • SGaistS SGaist

        Hi,

        Global variables like that one are usually sign of bad design. A better solution would be using signals and slots. Have your DialogWifi emit a signal when it finds the WiFi is connected or when It loses it.

        N Offline
        N Offline
        NTCYP
        wrote on last edited by
        #3

        @SGaist Thank you again. I followed your insight and I find that in my application there is a api that my ui_DialogWifi used to check the internet with a using SIGNAL. So I use;

        wifiResponse = _Wifi.check Internet();
        

        in my main window ui and yes it did the job nicely. I thought that if I use QSetting or QApplication to set a global variable I can acces from main window. But after I read your post I realized that I already have an api for my wifi connection. Thank you for your insight.

        Here is my code:

        // SHOW WIFI LOGO 
         WifiLogoTimer = new QTimer(this);
         connect(WifiLogoTimer, SIGNAL(timeout()),this, SLOT(showWifiLogo()));
         WifiLogoTimer->start(300);
        
        
        void MainWindow::showWifiLogo()
        {
          _acrResponse = _acrWifi.checkInternet();
        
          if(_acrResponse.contains("Not connected"))
          {
              ui->labelWifiLogo->setPixmap(QPixmap("/home/res/button/wifiOff.png"));
          }
          else
          {
              ui->labelWifiLogo->setPixmap(QPixmap("/home/res/button/wifi.png"));
          }
        }
        
        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Doing it every 300ms might be a bit much, no ?

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          N 1 Reply Last reply
          0
          • SGaistS SGaist

            Doing it every 300ms might be a bit much, no ?

            N Offline
            N Offline
            NTCYP
            wrote on last edited by
            #5

            @SGaist Thanks,

            Because of the wifi hotspot is not stable I want to test every 5 min. (5 * 60s = 300 sec) :) forget to convert from second to millisecond. Now I set it to WifiLogoTimer->start(30000); 30000 milisecond. :)

            Kind Regards

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved