在Qt中通过信号和插槽系统传递变量[关闭]

问题描述 投票:-1回答:1

所以我想通过Qt中的信号和插槽系统传递一个std :: string变量;我在堆上有一个名为RecordTalks的类,当收到来自聊天的消息超过25个符号时,它会发出一个信号。代码如下:

#include "recordtalks.h"

// records chat to files RecordTalks::RecordTalks(QObject *parent) {

}

RecordTalks::~RecordTalks() {
    std::cout << "destructor" << "\n"; }

void RecordTalks::run(QString data) {
    std::cout << ">>> whole data is: " << data.toStdString() <<  " ------------------\n";
    std::string text = data.toStdString();

    extractNick(text);
    extractRoom(text);
    //std::cout << "save in recordTalks is: " << save << "\n";
    if((nick.size()!=0 && room.size()!=0))
    {
        extractText(data);
        pushToVector(removeDigitsAndSpecials(nick), subText);
    }
    else
       return; }

void RecordTalks::run() {
    save=false;
    save = console.saveNowState();
    if(save==true)
    {
        saveToFile(allTexts, "BOT"); //trigger line saving to file
        file.close();
        removeDuplicatedNicks();
        createNickFiles(nicks_v);
        match_nicks_and_allTexts(allTexts);
        closeNickFiles();

        nicks_v.clear();
        nickAndFile_pair_vec.clear();
        text.clear();
        allTexts.clear();
        nick.clear();
        room.clear();
        subText.clear();
        save=false;
    } 
}

void RecordTalks::extractText(QString &data) {
    std::string text = data.toStdString(); // tu zmiana
    size_t pos, pos2;

    if((pos=text.find("PRIVMSG #"))!=std::string::npos)
    {
        if((pos2=text.find(":",pos))!=std::string::npos)
        {
            subText = text.substr(pos2+1, text.size()-(pos2+1)  );
            if((pos=subText.find("\u0001ACTION "))!=std::string::npos)
            {
                subText = subText.substr(pos+8 , subText.size()-3-(pos+8));
            }
            else
                std::cout << "subText: " << subText  << "\n";
         }
    }
    else  //if nothing to write to log:
    {
        return ;
    } 
}

void RecordTalks::extractNick(std::string &text) {
    Extract_NickAndRoom e;
    QString temp = QString::fromStdString(text);
    nick = e.extractNick(temp);
    //std::cout << "nick is: " << nick << "\n";

}

void RecordTalks::extractRoom(std::string &text) {
    Extract_NickAndRoom e;
    QString temp = QString::fromStdString(text);
    room = e.extractRoom(temp);
    //std::cout << "room is: " << room << "\n"; }


void RecordTalks::pushToVector(std::string nick, std::string &subText)
//general vector with texts {
    if(subText.size()>25 ) //if line of text from irc is bigger than 25 signs
    {
        std::pair<std::string, std::string> para = std::make_pair(nick, subText);
        allTexts.push_back(para);  // pair --> vector


        emit textUpdateSignal(subText);
        std::cout << "EMITTTTTTTTTTTTTTTTTTT " << "\n";
        //std::cout << ">>> subText: " << subText << "  nick: " << nick << " <<<" << "\n";
    }

    if(allTexts.size()==60)
    {
        saveToFile(allTexts, "BOT"); //trigger line saving to file
        file.close();
        removeDuplicatedNicks();
        createNickFiles(nicks_v);
        match_nicks_and_allTexts(allTexts);
        closeNickFiles();

        nicks_v.clear();
        nickAndFile_pair_vec.clear();
        text.clear();
        allTexts.clear();
        nick.clear();
        room.clear();
        subText.clear();
        save=false;
    } 
}

void RecordTalks::saveToFile( std::vector< std::pair<std::string,
std::string> > &allTexts, std::string nickFileName) {
    std::string address = "D:\\Qt_workspace\\weatherBot\\logs\\" + nickFileName + ".txt";
    file.open(address, std::ios::out | std::ios::app);

    for (int i = 0; i < allTexts.size(); ++i)
    {
        file << allTexts[i].second; //writes text to file, not the nicks
    } 
}




void RecordTalks::createNickFiles(std::vector<std::string> &nicks_v) {
   /* make vector of pair nick and file, so we can identify the file name and then
       put text-matching-to-file-nick with file name */
    for (int i = 0; i < nicks_v.size(); ++i)
    {
        std::fstream file2("D:\\Qt_workspace\\weatherBot\\logs\\"+ nicks_v[i] + ".txt",
                           std::ios::out | std::ios::app);
        std::pair<std::string, std::fstream> nickAndFile_pair;
        nickAndFile_pair = std::make_pair(removeDigitsAndSpecials(nicks_v[i]),
                                          std::move(file2));
        nickAndFile_pair_vec.push_back(std::move(nickAndFile_pair));
    } 
}// nicks_v[i] put inside function removeDigitsAndSpecials(std::string& nick)


void RecordTalks::match_nicks_and_allTexts(std::vector<
std::pair<std::string, std::string> > allTexts) {
    for (int i = 0; i < nickAndFile_pair_vec.size(); ++i)
    {
        for (int j = 0; j < allTexts.size(); ++j)
        {
            if(nickAndFile_pair_vec[i].first == allTexts[j].first)
            {
                nickAndFile_pair_vec[i].second << allTexts[j].second;
                //nickAndFile_pair_vec[j].second.flush();
            }
        }
    }

    std::cout << "allTexts size in match_nicks_and_allTexts is: ";
    std::cout << allTexts.size() << "\n"; 
}


void RecordTalks::removeDuplicatedNicks() {
    std::vector< std::pair<std::string, std::string> > temp = allTexts;
    std::cout << "temp size: " << temp.size() << "\n";

    for (int i = 0; i < temp.size(); ++i)
    {
        for (int j = i+1; j < temp.size(); ++j)
        {
            if(temp[i].first == temp[j].first  &&  i+1< temp.size())
            {
                temp.erase(temp.begin()+j);
                j--;
            }
        }
    }


    for (int i = 0; i < temp.size(); ++i)
    {
        nicks_v.push_back(temp[i].first);
    }
    std::cout << "nicks_v.size: " << nicks_v.size() << "\n";

}

void RecordTalks::closeNickFiles() //closes separate nick files {
    for (int i = 0; i < nickAndFile_pair_vec.size(); ++i)
    {
        nickAndFile_pair_vec[i].second.close();
    }
    std::cout << "Files closed..." << "\n"; }


std::string RecordTalks::removeDigitsAndSpecials(std::string& nick) {
    for (int i = 0; i < nick.size(); ++i)
    {
        if(std::isalpha(nick[i]))
        {

        }

        else if(std::isdigit(nick[i]))
        {
             nick.erase(nick.begin()+i);
             std::cout << "removing: " << nick[i] << "\n";
             i--;
        }
        else
        {
            nick.erase(nick.begin()+i);
            std::cout << "removing: " << nick[i] << "\n";
            i--;
        }
    }
    return nick; 
}

然后在另一个类我有连接功能。当我用这种形式写它:

connect(recordTalks, &RecordTalks::textUpdateSignal, this, &Socket::updateDatabaseSLOT); 

插槽接收信号。但是当我以这种方式编写函数时:

connect(recordTalks, SIGNAL(textUpdateSign(std::string)), this, 
        SLOT(updateDatabaseSLOT(std::string)));

插槽没有被解雇。所以我的问题是,原因是什么。在其他情况下,后一版本工作得很好。

插槽看起来像这样:

void Socket::updateDatabaseSLOT(std::string textUpdate)
{
    std::cout << "updates text: << textUpdate << "\n";

     database.push_back(textUpdate);
}
c++ qt
1个回答
0
投票

我认为你的连接声明中有拼写错误。 Qt可能会令人沮丧。 SIGNAL(textUpdateSign(std::string )应该是SIGNAL(textUpdateSignal(std::string )

© www.soinside.com 2019 - 2024. All rights reserved.