如何在按QPushButton时使用函数调用在QMessage中实现消息输出?

问题描述 投票:0回答:1

在面试中面对以下任务:

在此任务中,您需要使用QtWidgets创建一个简单的用户界面(UI)并将其连接到提供的库。 ComboBox必须包含三个项目。当前项目更改时 - 该按钮也会更改。根据按钮的不同,您必须从库中调用相应的函数并在消息中显示其输出。因此,每个按钮显示不同的消息。

我完成了2/3的任务,但我无法弄清楚如何使用按钮实现任务并通过调用函数在QMessage中显示消息。

头文件附加到任务,其中包括三个函数字符串getFunction1(),字符串getFunction2(),字符串getFunction3()以及.dll库的原型。

我非常感谢澄清如何最好地执行任务。

c++ qt
1个回答
0
投票

你可以这样做:

  1. 在Designer上创建一个QComboBox,并使用QWidget构造函数中的项目对其进行初始化。 Widget::Widget(QWidget *parent) : QWidget(parent), ui(new Ui::Widget) { ui->setupUi(this); ui->comboBox->addItem("first"); ui->comboBox->addItem("second"); ui->comboBox->addItem("third"); ui->comboBox->setCurrentIndex(0); ui->pushButton->setText("first");//because of the combox's current index is zero, thus "first"; }
  2. 在Designer中创建一个QPushButtonon,并在上面的构造函数中使用文本进行初始化。
  3. 右键单击设计器的QComboBox,然后单击Goto slot并选择slot on_comboBox_currentIndexChanged(const QString &arg1)并单击确定。
  4. 内部插槽on_comboBox_currentIndexChanged(const QString &arg1)添加语句ui->pushButton->setText(arg1); //这是因为如果用户单击组合框上的另一个选项,我们应该更新按钮的文本。 void Widget::on_comboBox_currentIndexChanged(const QString &arg1) { ui->pushButton->setText(arg1);//updating the button's text. }
  5. 右键单击设计器的QPushButton,然后单击Goto slot并选择slot on_pushButton_clicked()并单击确定。
  6. 在槽内on_pushButton_clicked()有这个代码,并根据按钮的文本调用相应的dll函数。 void Widget::on_pushButton_clicked() { if(ui->pushButton->text()=="first") { //call getFunction1() and assign the return value to QString msg QString msg; QMessageBox::about(this,"called function return value",msg);//showing in the msgbox. } else if(ui->pushButton->text()=="second") { //call getFunction2() and assign the return value to QString msg QString msg; QMessageBox::about(this,"called function return value",msg); } else { //call getFunction3() and assign the return value to QString msg QString msg; QMessageBox::about(this,"called function return value",msg); } }
© www.soinside.com 2019 - 2024. All rights reserved.