@@ -187,24 +187,36 @@ void MainWindow::refresh()
187187
188188void MainWindow::refreshBatteryStatus ()
189189{
190- QDBusConnection bus = QDBusConnection::systemBus ();
191-
192- // 创建Dbus接口
193- QDBusInterface interfaceService (" org.freedesktop.UPower" , " /org/freedesktop/UPower" , " org.freedesktop.UPower" , bus);
194- if (interfaceService.isValid ()) {
195- QDBusReply<QString> interfacePath = interfaceService.call (" EnumerateDevices" );
196- if (interfacePath.value ().contains (" /org/freedesktop/UPower/devices/battery_BAT1" )) {
197- QDBusInterface interfaceBattery (" org.freedesktop.UPower" , " /org/freedesktop/UPower/devices/battery_BAT1" , " org.freedesktop.UPower.Device" , bus);
198- if (interfaceBattery.isValid ()) {
199- QDBusMessage reply = interfaceBattery.call (" Refresh" );
200-
201- if (reply.type () != QDBusMessage::ReplyMessage)
202- qWarning () << " call Refresh failure:" << reply.errorMessage ();
203- }
204- }
205- } else {
206- qDebug () << " interface UPower invalid" ;
207- }
190+ QDBusConnection bus = QDBusConnection::systemBus ();
191+
192+ // 创建Dbus接口
193+ QDBusInterface interfaceService (" org.freedesktop.UPower" , " /org/freedesktop/UPower" , " org.freedesktop.UPower" , bus);
194+ if (interfaceService.isValid ()) {
195+ QProcess process;
196+ QString command = " gdbus call --system --dest org.freedesktop.UPower --object-path /org/freedesktop/UPower --method org.freedesktop.UPower.EnumerateDevices" ;
197+ process.start (command);
198+ process.waitForFinished ();
199+
200+ QByteArray output = process.readAllStandardOutput ();
201+ QString outputStr = QString::fromLocal8Bit (output);
202+ QStringList outputList = outputStr.split (" ," );
203+
204+ foreach (const QString& str, outputList) {
205+ if (str.contains (" BAT" )) {
206+ QStringList pathStr = str.split (" '" );
207+ if (pathStr.size () >= 2 ) {
208+ QDBusInterface interfaceBattery (" org.freedesktop.UPower" , pathStr.at (1 ), " org.freedesktop.UPower.Device" , bus);
209+ if (interfaceBattery.isValid ()) {
210+ QDBusMessage reply = interfaceBattery.call (" Refresh" );
211+ if (reply.type () != QDBusMessage::ReplyMessage)
212+ qWarning () << " call Refresh failure:" << reply.errorMessage ();
213+ }
214+ }
215+ }
216+ }
217+ } else {
218+ qDebug () << " interface UPower invalid" ;
219+ }
208220}
209221
210222bool MainWindow::exportTo ()
0 commit comments