跳到主内容

Qt 桌面应用程序

一个为 Casdoor的 Qt桌面应用程序示例

How to Run the Example

前置要求

初始化

You need to initialize 7 string parameters:

名称描述文件
endpoint您的 Casdoor 服务器主机/域mainwindow.h
client_id您的 Casdoor 应用程序的客户端 IDmainwindow.h
client_secretThe Client Secret of your Casdoor applicationmainwindow.h
certificateCasdoor 应用程序证书的公钥mainwindow.h
org_name您的Casdoor应用程序的名称mainwindow.h
app_name您的Casdoor应用程序的名称mainwindow.h
redirect_url您的Casdoor 应用程序的回调URL路径将是 casdoor://callback 如果没有提供mainwindow.h

If you don't set the endpoint parameter, this project will use http://localhost:8000 as the default Casdoor server.

Running the Application

Using Qt Creator

  1. Open casdoor-cpp-qt-example.pro
  2. Set the INCLUDEPATH of OpenSSL in casdoor-cpp-qt-example.pro
  3. Press Ctrl + R to start

效果预览

After running this Qt desktop application, a new window will be shown on your desktop.

索引

If you click the Sign In button, a login window will be shown on your desktop.

登录

After a successful login, a user profile window will be shown on your desktop, displaying your user information.

用户个人资料

You can preview the entire process in the following GIF image.

预览 gif

How to Integrate

Opening the Login Window

// Load and display the login page of Casdoor
m_webview->page()->load(*m_signin_url);
m_webview->show();

Listening to the Open Application Event

// Initialize the TcpServer object and listen on port 8080
m_tcpserver = new QTcpServer(this);
if (!m_tcpserver->listen(QHostAddress::LocalHost, 8080)) {
qDebug() << m_tcpserver->errorString();
close();
}
connect(m_tcpserver, SIGNAL(newConnection()), this, SLOT(on_tcp_connected()));

Using Auth Code to Get the User Info

// Get the token and parse it with the JWT library
std::string token = m_casdoor->GetOAuthToken(code.toStdString());
auto decoded = m_casdoor->ParseJwtToken(token);