如何使用信号和插槽从qt表单中获取id,然后在链表中搜索它并使用另一个表单显示结果

How can I use signal and slots to get an id from a qt form, then search for it in a linked list and display the result using another form?

本文关键字:表单 搜索 链表 结果 显示 另一个 插槽 信号 何使用 qt id      更新时间:2023-10-16

类,其中包含在使用信号和插槽连接后应触发搜索然后显示其他类的信息:

#include "recruitsearch.h"
#include "ui_recruitsearch.h"
#include <cctype>
#include <QtGui>
#include <string>
#include <QtCore>
using namespace std;
RecruitSearch::RecruitSearch(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::RecruitSearch)
{
    ui->setupUi(this);
}
RecruitSearch::~RecruitSearch()
{
    delete ui;
}
void RecruitSearch::on_pushButton_clicked()
{
    //if(EmployerSearch::ui->buttonBox->clicked();
    if(ui->rfrId->text().isEmpty() || ui->rfrId->text().isNull() || (is_Digit(ui->rfrId->text().toStdString())==false) ){
        QMessageBox::warning(this,"Error", "Please enter a valid RFR id(digits only)");
    }
    else{
        accepted();
        this->close();
    }
}
int RecruitSearch:: getRfrId(){
    return ui->rfrId->text().toInt();
}

bool RecruitSearch::is_Digit( string input){
    for (int i = 0; i < input.length(); i++) {
           if (!std::isdigit(input[i]))
               return false;
       }
       return true;
}

带显示屏的类。我将如何连接两个插槽并使用第一个表单中的 id 搜索链接列表,然后使用另一个表单显示结果:

#include "rfrform.h"
#include "ui_rfrform.h"
#include <cctype>
#include <string>
#include <QString>
#include <QtGui>
#include <QtCore>
#include <iostream>
using namespace std;

RfrForm::RfrForm(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::RfrForm)
{
    ui->setupUi(this);
}
RfrForm::~RfrForm()
{
    delete ui;
}

void RfrForm::setEmpName(string name){
    QString qstr=QString::fromStdString(name);
    ui->EmployerName->setText(qstr);
}
void RfrForm::setAOE(string aoe){
    QString  qstr=QString::fromStdString(aoe);
    ui->AOE->setText(qstr);
}
void RfrForm::setEmpId(int id){
    QString  qstr=QString::number(id);
    ui->EmpId->setText(qstr);
}// end of setId
void RfrForm::setNumOfPos(int num){
    QString qstr=QString::number(num);
    ui->numOfPos->setText(qstr);
}
void RfrForm::setGender(string gen){
    QString qstr=QString::fromStdString(gen);
    ui->gender->setText(qstr);
}
void RfrForm::setMaxRecruits(int max){
    QString qstr=QString::number(max);
    ui->MaxRecruits->setText(qstr);
}

void RfrForm::display(RFR *temp){
    this->show();
}

将Qt中的信号和插槽视为简单的回调。例如,标准信号/插槽可能如下所示:

....
QAction* act = new QAction(this);
connect(act, SIGNAL(triggered()), this, SLOT(handleAct()));
....

请注意,第一个参数是指向将发出信号的对象的指针(因此,信号方法,在这种情况下triggered必须与我们的发送者相关联,在本例中为 QAction),第二个参数是信号函数签名。同样,最后两个参数是槽的指针(即事件处理程序,在本例中为当前类)和某些处理程序函数的函数签名。

这是针对一些看起来像这样的类(请注意Q_OBJECT宏)

class TestMe
{
  Q_OBJECT
public:
  ...
protected slots:
  void handleAct();
};

其中handleAct()方法是您设计的一些任意函数。现在,为了更具体地回答您的问题,我们将需要更多关于您正在使用哪种类型的QWidget的详细信息(即我们知道它发出哪些信号)。在任何情况下,基本思想是从特定元素中捕获特定类型的符号,然后相应地处理信息。

有时,要

执行要查找的信息传输类型,可能需要将指向对象的特定指针作为类成员,以便可以轻松地获取相应QWidget中的信息(通常是text()方法或类似方法)。

但是,同样重要的是要注意,您可以覆盖QWidget类型并创建自定义信号和插槽。因此,如果您有一个返回 id 的自定义信号和一个将 id 值作为输入的信号,您可以连接这些信号和插槽(查看emit功能)

如果您提供有关您正在使用QWidget的更具体的信息,我们也许能够为您提供更具体的解决方案。