通过Qt中的信号和插槽系统传递变量

时间:2019-02-27 19:30:09

标签: c++ qt

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

$set_ = isset($set['id']) ? $set['id'] : '';
$value = isset($this->settings[$set_]) ? $this->settings[$set['id']] : '';

然后在另一个类中,我具有连接功能。当我以这种形式编写它时:

#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)));

1 个答案:

答案 0 :(得分:0)

我认为您的connect语句中有错字。 Qt可能会令人沮丧。 SIGNAL(textUpdateSign(std::string )应该是SIGNAL(textUpdateSignal(std::string )