简体   繁体   English

Qt C ++中的组合框选择插槽和信号

[英]Combo box selection slot and signal in Qt C++

Trying to connect the new selection change in a QComboBox to use the string of that selection in a slot. 尝试在QComboBox连接新的选择更改,以在插槽中使用该选择的字符串。

Basically, after the currentIndexChanged signal, the displayData mapp will already hold all data populated from another part of the program (eg a parser) and then, using the current selection of the combo box in comboBoxChange , a key will be given to the displayData map in order to get the desired values in the form of the tset struct. 基本上,后currentIndexChanged信号时, displayData MAPP将已持有从程序的另一部分(例如解析器)并填充随后的所有数据,使用当前selection的组合框的comboBoxChange ,一个密钥将被给予displayData地图为了以tset结构的形式获得所需的值。

Below is all my code setup and attempts slot and connect signals. 下面是我所有的代码设置,并尝试插槽和连接信号。 Please help understand how to do it. 请帮助了解如何做。

mainwindow.h 主窗口

struct tset {

  // struct declaration //

QString name;
double height;
}

class MainWindow : public QMainWindow {

private slots:

void comboBoxChange(QMap<QPair<QString, QString>, tset>&  displayData,  QString selection);
}

mainwindow.cpp 主窗口

#include "mainwindow.h"

//connect(ui->comboBox, &QComboBox::currentIndexChanged(const QString&), this,&MainWindow:: comboBoxChange(QMap<QPair<QString, QString>, tset>, QString selection));

connect(ui-> comboBox, &QComboBox::currentIndexChanged, this, &MainWindow::on_ comboBoxChange(QMap<QPair<QString, QString>, tset>, QString));


void MainWindow:: comboBoxChange (QMap<QPair<QString, QString>, tset> &displayData, QString s) {
// use the selection string to decide which data to get from the QMap
// from s derive blah and blah2
tset test= = displayData.value(qMakePair(QString{"blah"}, QString{"blah2"}));

qDebug() << test.name << test.height;
}

In the cpp, you have to use the connection function that is inherited from the QObject base class. 在cpp中,您必须使用从QObject基类继承的连接函数。

connect(const QObject * sender, const char * signal, const QObject * receiver, const char * method):
  1. sender: Pointer to the sender 发送者:指向发送者的指针
  2. signal: Definition of the signal. signal:信号的定义。 SIGNAL() macro with the name of the signal and the parameter SIGNAL()宏,带有信号名称和参数
  3. receiver: Pointer to the receiver 接收器:指向接收器的指针
  4. method: Definition of the receiver method. method:接收方方法的定义。 SIGNAL() or SLOT macro with the name of the receiver signal or function and the parameter of it. SIGNAL()或SLOT宏,带有接收器信号或函数的名称及其参数。

Hint: The connection is set at runtime. 提示:连接在运行时设置。 There is no compiler error if the connection is not correct(Only a QDebug message) 如果连接不正确,则没有编译器错误(仅QDebug消息)

connect(ui->comboBox, SIGNAL(currentIndexChanged(const QString&)), this, SLOT(comboBoxChange(const QString&)));

Here are the declaration in the header. 这是标题中的声明。 You have forgot to mention Q_OBJECT at the top of the declaration. 您忘记在声明的顶部提到Q_OBJECT了。 You need it for all classes that contain signals or slots. 您需要所有包含信号或插槽的类。

class MainWindow : public QMainWindow
{
  Q_OBJECT

  private slots:
    void comboBoxChange(const QString& selection_text);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM