根據第二個blog,我改動了部分**,使用dbus inte***ce呼叫方式,一步一步建立c/s結構**:
[socol@localhost testdbus]$ ll
client
hotelibs
service
1. 建立庫函式hotelibs
[socol@localhost testdbus]$ cd hotelibs
[socol@localhost hotelibs]$ ll
hotel.pro
hotel.cpp
hotel.h
[socol@localhost hotelibs]$cat hotel.h
#ifndef hotel_h[socol@localhost hotelibs]$cat hotel.cpp#define hotel_h
#include #include #define max_rooms 10
class hotel : public qobject
public slots:
// check in,引數為房間數,返回成功拿到的房間數
int checkin(int num_room);
// check out,引數為房間數,返回成功退回的房間數
int checkout(int num_room);
// query,用於查詢目前還剩下的房間數
int query();
private:
int m_rooms;
qreadwritelock m_lock;
};#endif // hotel_h
#include #include #include #include #include "hotel.h"int hotel::checkin(int num_room)
else
return num_room;
}int hotel::checkout(int num_room)
return num_room;
}int hotel::query()
com.test.hotel.xml
hotel.cpp
hotel.h
hotelibs.pro
hotelinte***ce.cpp
hotelinte***ce.h
[socol@localhost hotelibs]$cat com.test.hotel.xml
[socol@localhost hotelibs]$cat hotelibs.pro
qt += dbus[socol@localhost hotelibs]$qmake-qt4 & makeqt -= gui
target = hotelibs
config += console
template = lib
sources += hotelinte***ce.cpp hotel.cpp
headers += hotelinte***ce.h hotel.h
[socol@localhost hotelibs]$sudo cp libhotelibs.so* /usr/lib
2. 建立服務端service
[socol@localhost testdbus]$ cd service
[socol@localhost service]$ ll
hotelservice.pro
main.cpp
[socol@localhost service]$cat hotelservice.pro
qt += dbusqt -= gui
target = hotelservice
config += console
sources += main.cpp
libs += -l../hotelibs -lhotelibs
includepath += ../hotelibs
[socol@localhost service]$cat main.cpp
#include #include #include #include #include "hotel.h"[socol@localhost service]$qmake-qt4 & makeint main(int argc, char *argv)
hotel my_hotel;
#if 0
// adaport 方式,需要在hotelibs目錄中執行:
// qdbusxml2cpp com.test.hotel.xml -i hotel.h -a hoteladaport
new registryadaptor(my_hotel);
#else
// 註冊名為"/hotel/registry"的object。
// "qdbusconnection::exportallslots "表示把類hotel的所有slot都匯出為這個object的method
// 如果需要使用signal的話可以使用 qdbusconnection::exportallsignals
bus.registerobject("/hotel/registry", &my_hotel,
qdbusconnection::exportallslots);
#endif
return a.exec();
}
[socol@localhost service]$sudo cp hotelservice /usr/libexec/
[socol@localhost service]$sudo vim /usr/share/dbus-1/services/com.test.hotel.service
[socol@localhost service]$cat /usr/share/dbus-1/services/com.test.hotel.service
[d-bus service]
name=com.test.hotel
exec=/usr/libexec/hotelservice
3. 建立客戶端client
[socol@localhost testdbus]$ cd client
[socol@localhost client]$ ll
main.cpp
hotelclient.pro
[socol@localhost client]$cat main.cpp
#include #include #include #include int main(int argc, char *argv)[socol@localhost client]$cat hotelclient.proif (argc == 2) else
// 初始化自動生成的proxy類com::test::hotel::registry
com::test::hotel::registry myhotel("com.test.hotel",
"/hotel/registry",
qdbusconnection::sessionbus());
// 呼叫checkin
qdbuspendingreplyreply = myhotel.checkin(num_room);
// qdbusxml2cpp生成的proxy類是採用非同步的方式來傳遞message,
// 所以在此需要呼叫waitforfinished來等到message執行完成
reply.waitforfinished();
if (reply.isvalid()) else
return 0;
}
qt += dbus4.測試qt -= gui
target = hotelclient
config += console
libs += -l../hotelibs -lhotelibs
includepath += ../hotelibs
sources += main.cpp
[socol@localhost client]$d-feet
// select query() execut, result 10
[socol@localhost client]$./hotelclient
got 1 room
[socol@localhost client]$d-feet
// select query() execut, result 9
Qt 簡單程式例項Hello,world
建立程式 d qt work hello main.c 1 include 2 include 3 include 45 int main int argc,char argv 6 分析一下程式 main 程式入口函式。接下來新建了乙個qwidget物件。qwidget可以看作乙個視窗。在其上可以放...
Qt例項程式如何使用
qt creator提供f1檢視reference documentation的目錄與qt5.7.0 examples qt 5.7目錄下條目一一對應。該目錄下是對應模組的例程,之前一致沒發現還有這個規律,記錄下以後方便學習查詢。model view 檢視 view 和資料分離,檢視用於顯示和編輯資...
Qt單例項程式 防止程式多開
使用qlocalserver,qlocalsocket實現單例項程序,當已經存在相同程序時且視窗未啟用 遮擋,最小化,托盤 時,啟用程序主視窗。參考文章 class qlocalserver brunning false localserver null mainwindow null 說明 通過s...