Check Wireless State Change in qt
-
I want to connect some signal to my handler slot for notify me when wireless connection disconnect or when connect ( be carful I do not care for internet Access only check for connection to modem)
I use "onlineStateChanged" in "QNetworkConfigurationManager" but it does not work Properly
how I should do this? -
@fireghostea said in Check Wireless State Change in qt:
I use "onlineStateChanged" in "QNetworkConfigurationManager" but it does not work Properly
elaborate
-
@raven-worx said in Check Wireless State Change in qt:
elaborate
this is my simple code
QNetworkConfigurationManager *ncm = new QNetworkConfigurationManager(); ChangeEvent *myhandler = new ChangeEvent (); QObject::connect(ncm, SIGNAL(onlineStateChanged(bool)), myhandler, SLOT(myslot(bool)));
my slot in ChangeEvent class is
void myslot(bool isonline);
i expect from this code when i disconnect my wireless "myslot" function be call but does not work
i ask this question in this link too linkand some one provide me some code that works for his but does not work for me
i use windows 10 -
@fireghostea said in Check Wireless State Change in qt:
ChangeEvent *myhandler = new ChangeEvent ();
Is myhandler a local variable, i.e. when does myhandler get's destroyed?
-
Hi
One note. Can it be you have a ncm in the class ?
As you say
QNetworkConfigurationManager *ncm = new QNetworkConfigurationManager();
but use
QObject::connect(&ncm, << the &if ncm is a pointer, using & wont compile.
-
@fireghostea
the online state istrue
when at least one connection/configuration isActive
! Are you sure your WLAN connection is the only connection? (Beside Bluetooth, LAN, etc.)You should rather listen to the QNetworkManager's configuration related signals and check explicitly for WLAN configurations (bearer type is
BearerWLAN
) -
@fireghostea
So you do have 2 of them :)QNetworkConfigurationManager *ncm = new QNetworkConfigurationManager(); // no 2 then
-
@fireghostea
Ok, it was just to be sure.Update: was bored so tested.
This works for me on win 10MainWindow::MainWindow(QWidget* parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); QNetworkConfigurationManager *ncm = new QNetworkConfigurationManager(this); ncm->updateConfigurations(); // NOTE. was needed connect(ncm, &QNetworkConfigurationManager::onlineStateChanged, this,MainWindow::configurationStateChanged); } void MainWindow::configurationStateChanged(bool ) { qDebug()<< "online State Changed"; }
Disabling the net, i get this
Update: Just saw raven-worx. Sounds much better as i have only 1 interface and hence it works.
-
@raven-worx with probability of 99% my problem is that you say
because when i disconnect All connections and connect again my handler function call properly
can you tell me how only focus on wireless ? -
I think he thinks of these signals
void configurationAdded(const QNetworkConfiguration &config);
void configurationRemoved(const QNetworkConfiguration &config);
void configurationChanged(const QNetworkConfiguration &config);There you get an QNetworkConfiguration and it has a
BearerType bearerType() const;so if that returns BearerWLAN, its the one you want.
I have no wifi to test with but i assume that configurationChanged will trigger and if its right type
then you should have the state.Hopefully that was what mr @raven-worx ment :)
-
@fireghostea said in Check Wireless State Change in qt:
can you tell me how only focus on wireless ?
@mrjj said in Check Wireless State Change in qt:
Hopefully that was what mr @raven-worx ment :)
exactly.