我需要用不同的按钮调用 2 个函数 我有这段代码:
signalMapperSelections = new QSignalMapper();
QPushButton *selected_type_button = new QPushButton();
selected_type_button->setObjectName("selected_type_button");
selected_type_button->setText(get_selected_type().replace(" ", "\n"));
selected_type_button->setMinimumHeight(80);
selected_type_button->setMinimumWidth(80);
selected_type_button->setMaximumHeight(80);
selected_type_button->setMaximumWidth(80);
selected_type_button->setStyleSheet(style_toolbutton);
ui->verticalLayout_selections->addWidget(selected_type_button);
connect(selected_type_button, SIGNAL(clicked()), signalMapperSelections, SLOT(map()));
signalMapperSelections->setMapping(selected_type_button, get_selected_type());
connect(signalMapperSelections, SIGNAL(mapped(QString)), this, SLOT(show_brands(QString)));
QPushButton *selected_brand_button = new QPushButton();
selected_brand_button->setObjectName("selected_brand_button");
selected_brand_button->setText(get_selected_brand().replace(" ", "\n"));
selected_brand_button->setMinimumHeight(80);
selected_brand_button->setMinimumWidth(80);
selected_brand_button->setMaximumHeight(80);
selected_brand_button->setMaximumWidth(80);
selected_brand_button->setStyleSheet(style_toolbutton);
ui->verticalLayout_selections->addWidget(selected_brand_button);
connect(selected_brand_button, SIGNAL(clicked()), signalMapperSelections, SLOT(map()));
signalMapperSelections->setMapping(selected_brand_button, get_selected_brand());
connect(signalMapperSelections, SIGNAL(mapped(QString)), this, SLOT(show_models(QString)));
当我点击“selected_type_button”时,我只想运行“show_brands”。但它正在运行“show_brands”和“show_models”这两个功能...... 我试过 Qt::UniqueConnection,但它没有解决这个问题。 我认为发生这种情况是因为两个按钮都使用相同的信号......但我不知道如何解决它。
我该如何解决这个问题?
最佳答案
When I click "selected_type_button" I only want to run "show_brands".
在您的情况下根本没有理由使用 QSignalMapper
。您只需将 selected_type_button
的 clicked
信号连接到 show_brands
插槽,并将 selected_brand_button 的
到 clicked
信号连接起来show_models
插槽。
QString is the selected type or selected brand....
这个 QString
参数与 clicked
信号源无关(因此,它不需要任何使用 QSignalMapper
的映射,请阅读QSignalMapper
在文档 here 中)。您当前使用的映射是在连接时设置的(而不是在发射时),这意味着 get_selected_type()
/get_selected_brand()
将在调用 setMapping
的时间(这显然不是你的意思)。
要在单击按钮时获取项目,您可以直接调用插槽中的 get_selected_type()
/get_selected_brand()
函数,您的代码将是像这样:
QPushButton *selected_type_button = new QPushButton();
selected_type_button->setObjectName("selected_type_button");
selected_type_button->setText(get_selected_type().replace(" ", "\n"));
selected_type_button->setMinimumHeight(80);
selected_type_button->setMinimumWidth(80);
selected_type_button->setMaximumHeight(80);
selected_type_button->setMaximumWidth(80);
selected_type_button->setStyleSheet(style_toolbutton);
ui->verticalLayout_selections->addWidget(selected_type_button);
//Qt 5 new connect syntax (replace ClassName with the current class's name)
connect(selected_type_button, &QPushButton::clicked, this, &ClassName::show_brands);
QPushButton *selected_brand_button = new QPushButton();
selected_brand_button->setObjectName("selected_brand_button");
selected_brand_button->setText(get_selected_brand().replace(" ", "\n"));
selected_brand_button->setMinimumHeight(80);
selected_brand_button->setMinimumWidth(80);
selected_brand_button->setMaximumHeight(80);
selected_brand_button->setMaximumWidth(80);
selected_brand_button->setStyleSheet(style_toolbutton);
ui->verticalLayout_selections->addWidget(selected_brand_button);
//replace ClassName with the current class's name)
connect(selected_brand_button, &QPushButton::clicked, this, &ClassName::show_models);
您的 show_brands
广告位应该类似于:
//no need for the QString argument
void ClassName::show_brands(){
QString selectedType= get_selected_type();
//show_brands here
}
show_models
插槽也是如此:
void ClassName::show_models(){
QString selectedBrand= get_selected_brand();
//show_models here
}
关于c++ - Qt 按钮调用 2 个插槽,我只需要一个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40180950/