gpt4 book ai didi

c++ - Qt 中的 TeamSpeak SDK

转载 作者:太空狗 更新时间:2023-10-29 21:36:35 26 4
gpt4 key购买 nike

我正在尝试将 TeamSpeak SDK 用于 Qt 中的个人项目,当我在主体中使用此代码时它工作正常

编译没有问题。问题是当我在 Qt 主窗口中使用它时:

主窗口.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <teamspeak/public_definitions.h>
#include <teamspeak/public_errors.h>
#include <teamspeak/serverlib_publicdefinitions.h>
#include <teamspeak/serverlib.h>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
Q_OBJECT

public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();

private:
Ui::MainWindow *ui;
void onClientConnected(uint64 serverID, anyID clientID, uint64 channelID, unsigned int* removeClientError);
ServerLibFunctions funcs; // it's a struct that have pointer fucntions
};

#endif // MAINWINDOW_H

主窗口.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
funcs.onClientConnected = onClientConnected; // error here
}

MainWindow::~MainWindow()
{
delete ui;
}

void MainWindow::onClientConnected(uint64 serverID, anyID clientID, uint64 channelID, unsigned int* removeClientError) {
char* clientName;
unsigned int error;

/* Query client nickname */
if ((error = ts3server_getClientVariableAsString(serverID, clientID, CLIENT_NICKNAME, &clientName)) != ERROR_ok) {
char* errormsg;
if (ts3server_getGlobalErrorMessage(error, &errormsg) == ERROR_ok) {
printf("Error querying client nickname: %s\n", errormsg);
ts3server_freeMemory(errormsg);
}
return;
}

printf("Client '%s' joined channel %llu on virtual server %llu\n", clientName, (unsigned long long) channelID, (unsigned long long)serverID);

/* Example: Kick clients with nickname "BlockMe from server */
if (!strcmp(clientName, "BlockMe")) {
printf("Blocking bad client!\n");
*removeClientError = ERROR_client_not_logged_in; /* Give a reason */
}
}

我已经评论了我在 Mainwindow.cpp 中遇到错误的行和错误:

无法从类型“void (MainWindow::)(uint64, anyID, uint64, unsigned int*) {aka void (MainWindow::)(long long unsigned int, short unsigned int, long long unsigned int, unsigned int*)}' 输入 'void ()(uint64, anyID, uint64, unsigned int) {aka void ()(long long unsigned int, short unsigned int, long long unsigned int, unsigned int)}' funcs.onClientConnected = onClientConnected; ^

我正在使用 Windows 10 Mingw 编译器 Qt 5.6.1
我如何在 oop c++​​ 中使用这个回调函数

最佳答案

我解决了在 Qt 中使用 TeamSpeak 我在 main.cpp 中初始化服务器并在结构中分配所有回调函数的问题,现在我可以在主窗口中使用服务器的任何功能,例如,如果我想在文本编辑中显示 channel 我在任何 C++ 类或 Qt 对话框中使用它的功能,我可以毫无问题地调用它main.cpp 的代码

 // put the fucntion of the call back here
int main(int argc, char *argv[])
{
QApplication a(argc, argv);

char *version;
short abort = 0;
uint64 serverID;
unsigned int error;
int unknownInput = 0;
uint64* ids;
int i;

struct ServerLibFunctions funcs;

/* Initialize all callbacks with NULL */
memset(&funcs, 0, sizeof(struct ServerLibFunctions));
funcs.onClientConnected = onClientConnected;
funcs.onClientDisconnected = onClientDisconnected;
funcs.onClientMoved = onClientMoved;
funcs.onChannelCreated = onChannelCreated;
funcs.onChannelEdited = onChannelEdited;
funcs.onChannelDeleted = onChannelDeleted;
funcs.onServerTextMessageEvent = onServerTextMessageEvent;
funcs.onChannelTextMessageEvent = onChannelTextMessageEvent;
funcs.onUserLoggingMessageEvent = onUserLoggingMessageEvent;
funcs.onClientStartTalkingEvent = onClientStartTalkingEvent;
funcs.onClientStopTalkingEvent = onClientStopTalkingEvent;
funcs.onAccountingErrorEvent = onAccountingErrorEvent;
funcs.onCustomPacketEncryptEvent = nullptr;
funcs.onCustomPacketDecryptEvent = nullptr;

if((error = ts3server_initServerLib(&funcs, LogType_FILE | LogType_CONSOLE | LogType_USERLOGGING, NULL)) != ERROR_ok) {
char* errormsg;
if(ts3server_getGlobalErrorMessage(error, &errormsg) == ERROR_ok) {
printf("Error initialzing serverlib: %s\n", errormsg);
ts3server_freeMemory(errormsg);
}
return 1;
}

MainWindow w;
w.show();

return a.exec();
// use any function to edit server in any c++ class as show chan add chancel and so on
}

使用所有功能在任何 C++ 类中编辑服务器,它会工作,我认为我们不需要在 main 中多次初始化服务器,我们不需要,但它在类中,如果我想使用Qt GUI制作VoIP我们只需要服务器编辑功能如果有更好的答案请发布谢谢编辑
另一种解决方案是在 main 中使用回调函数初始化结构并将其传递到 Mainwindow 或构造函数中的任何 c++ 类,并使用它来初始化 TeamSpeak 的服务器库

关于c++ - Qt 中的 TeamSpeak SDK,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39795049/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com