3

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

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

主窗口.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 中出现错误和错误的行:

无法将 'MainWindow::onClientConnected' 从类型 '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 )}'函数。 onClientConnected = onClientConnected; ^

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

4

2 回答 2

2

我解决了在 Qt 中使用 TeamSpeak 的问题我在 main.cpp 中初始化服务器并在结构中分配所有回调函数,现在我可以在主窗口中使用服务器的任何功能,例如,如果我想显示频道在文本编辑中,我在任何 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
}

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

于 2016-09-30T21:45:01.280 回答
0

您的问题可以用下面的简单代码来模拟。根据我的理解,您尝试分配的功能是特定于类的成员。因此,当您尝试分配时,编译器会将其视为签名不匹配。您可能需要在非类成员中编写所需的功能,然后进行分配。(除非有办法允许此分配)。

将具有函数输入的结构作为函数类的朋友,如下所示。

 #include <iostream>

// common namespace
using namespace std;


//Structure with function pointers.
struct funcPtrStruct
{
    public:
    void (*func)(int a, float b);
};


//The non member function that address your functionality
void testFuncOne(int a ,float b)
{
    //Do something
}


//Class implementation
class Test
{

public:
    Test()
    {
        funcPtrStruct funPtrSt;
        //func = &testFunc; //Here the error is shown " a value of type .....can not convert......"
        funPtrSt.func = &testFuncOne; //This is working but it is not a class memeber.
    }
private:

     friend struct funcPtrStruct; //Make the structure of function pointers as your friend.
};


int main() {
    return 0;
}
于 2016-09-30T20:10:41.417 回答