如何把"The value of the spinbox is " + QString::number(ui->spinBox->value())和"The value of the spinbox_2 is " + QString::number(ui->spinBox_2->value())在同一消息盒子里换行显示
时间: 2024-02-17 11:01:15 浏览: 24
您可以使用 HTML 标签中的 `<br>` 来实现换行。具体代码如下:
```
QString message = "The value of the spinbox is " + QString::number(ui->spinBox->value()) + "<br>" + "The value of the spinbox_2 is " + QString::number(ui->spinBox_2->value());
QMessageBox::information(this, "Message", message);
```
在以上代码中,我们使用了 `<br>` 标签来插入换行符,同时将两个字符串拼接在一起。最终的结果就是在同一消息盒子里换行显示。
相关问题
如果"The value of the spinbox is " + QString::number(ui->spinBox->value())里面的spinbox有非0的参数(不包括负数),那如何让另外一行"The value of the spinbox_2 is " + QString::number(ui->spinBox_2->value())不显示在消息盒子里
您可以使用条件语句来控制消息盒子中的文本。具体来说,您可以在显示消息盒子之前检查 spinbox_2 的值是否为0,如果为0,则不在消息盒子中显示相关的文本。
以下是示例代码:
```c++
if(ui->spinBox_2->value() != 0) {
QMessageBox::information(this, "Message", "The value of the spinbox is " + QString::number(ui->spinBox->value()) + "\n" +
"The value of the spinbox_2 is " + QString::number(ui->spinBox_2->value()));
} else {
QMessageBox::information(this, "Message", "The value of the spinbox is " + QString::number(ui->spinBox->value()));
}
```
在上面的代码中,我们首先检查 spinbox_2 的值是否为0。如果不是0,则在消息盒子中显示两个 spinbox 的值;如果是0,则只显示一个 spinbox 的值。
在QT里,有一个int A=ui->spinBox->value()*2+ ui->spinBox_2->value()*1.5+ ui->spinBox_3->value()*15+ ui->spinBox_4->value()*10+ ui->spinBox_5->value()*6+ ui->spinBox_6->value()*3+ ui->spinBox_7->value()*15+ ui->spinBox_8->value()*15+ ui->spinBox_9->value()*5;我需要A的值可以显示在QMessageBox msgBoxa里
您可以使用以下代码将A的值显示在QMessageBox中:
```
int A = ui->spinBox->value()*2 + ui->spinBox_2->value()*1.5 + ui->spinBox_3->value()*15 + ui->spinBox_4->value()*10 + ui->spinBox_5->value()*6 + ui->spinBox_6->value()*3 + ui->spinBox_7->value()*15 + ui->spinBox_8->value()*15 + ui->spinBox_9->value()*5;
QMessageBox msgBoxa;
msgBoxa.setText("A=" + QString::number(A));
msgBoxa.exec();
```
这里首先计算出A的值,然后使用QString::number()将其转换为QString类型,并将其作为QMessageBox的文本内容。最后使用msgBoxa.exec()函数显示QMessageBox。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)