如何使用信号和插槽从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?
类,其中包含在使用信号和插槽连接后应触发搜索然后显示其他类的信息:
#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
的更具体的信息,我们也许能够为您提供更具体的解决方案。
相关文章:
- GTK 最大化表单 C++
- win 表单应用程序字符串^ 到 int
- 结构化绑定初始值设定项表单 { 赋值表达式 } 对于 clang 上的数组类型失败
- 可以在JSON文件的帮助下在Qt中创建UI表单
- 表单未显示在凭据提供程序中
- 表单显示对话框函数错误,并且不执行下面的语句
- 如何从 winapi 中的编辑表单获取文本?
- 在 cout 中将线条表单文件与自定义文本组合时出现问题
- 从 dll 调用 opencv 垫到 Windows 表单,图像出现故障
- 如何在不使用任何数据库的情况下制作动态注册表单?
- 家庭作业帮助 - 检查表单按钮上的数据
- C++ 在给定像素数据数组的表单上绘制矩形
- 复制初始化表单 '= {}'
- 如何创建第二个 QT .ui 表单
- 如何将变量从一个表单传递到另一个表单
- 如何从另一个Qt Widget表单类调用MainWindow类
- 为什么我的代码无法在二叉搜索树中正确执行预购表单?
- 如何实现表单对象的搜索
- 如何使用信号和插槽从qt表单中获取id,然后在链表中搜索它并使用另一个表单显示结果
- 如何在搜索表单的QMessageBox中包含QLineEdit