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



  • 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"));
        }
    }
    

  • Lifetime Qt Champion

    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.



  • @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"));
      }
    }
    

  • Lifetime Qt Champion

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



  • @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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.