C++ 如何在Qt上使用带有if语句的组合框?

C++ 如何在Qt上使用带有if语句的组合框?,c++,qt,qtserialport,C++,Qt,Qtserialport,因此,我在QT上使用combobox,在combobox中我添加了两个设备名称,我想更改combobox中的名称,以便通过radiobox将数据发送到我的端口。我尝试了if语句,但它不起作用 #include "mainwindow.h" #include "ui_mainwindow.h" #include <QtSerialPort/QSerialPort> #include <QMessageBox> QSerialPort serial; MainWindo

因此,我在QT上使用combobox,在combobox中我添加了两个设备名称,我想更改combobox中的名称,以便通过radiobox将数据发送到我的端口。我尝试了if语句,但它不起作用

 #include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QtSerialPort/QSerialPort>
#include <QMessageBox>

QSerialPort serial;

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    serial.setPortName("COM3");
    serial.setBaudRate(QSerialPort::Baud19200);
    serial.setDataBits(QSerialPort::Data8);                         
    serial.setParity(QSerialPort::NoParity);
    serial.setStopBits(QSerialPort::OneStop);
    serial.setFlowControl(QSerialPort::NoFlowControl);
    ui->comboBox->addItem("SP157B");
    ui->comboBox->addItem("JSP");
}

void MainWindow::on_pushButton_clicked()
{
    if(ui->comboBox->currentText())
    {
        if(ui->radioButton->isChecked())
        {
            QByteArray Rouge("\x15\x7B\x1A\xAA");       
            serial.write(Rouge);
            QString t = ui->label->text();
            ui->label->setText(t+"\n15 7B 1A AA");
        }
        if(ui->radioButton_2->isChecked())
        {
            QByteArray Vert("\x15\x7B\x19\xA9");    //allume en vert
            serial.write(Vert);
        }
        if(ui->radioButton_3->isChecked())
        {
            QByteArray Orange("\x15\x7B\x1B\xAB"); 
            serial.write(Orange);
        }
    }
}
#包括“mainwindow.h”
#包括“ui_main window.h”
#包括
#包括
QSerialPort系列;
主窗口::主窗口(QWidget*父窗口):
QMainWindow(父级),
用户界面(新用户界面::主窗口)
{
用户界面->设置用户界面(此);
serial.setPortName(“COM3”);
串行数据传输速率(QSerialPort::Baud19200);
serial.setDataBits(QSerialPort::Data8);
serial.setParity(QSerialPort::NoParity);
串行.setStopBits(QSerialPort::OneStop);
serial.setFlowControl(QSerialPort::NoFlowControl);
用户界面->组合框->添加项(“SP157B”);
用户界面->组合框->添加项(“JSP”);
}
void主窗口::在按钮上单击()
{
如果(ui->comboBox->currentText())
{
如果(ui->radioButton->isChecked())
{
QByteArray胭脂(“\x15\x7B\x1A\xAA”);
连载:写(胭脂);
QString t=ui->label->text();
用户界面->标签->设置文本(t+“\n15 7B 1A AA”);
}
如果(ui->radioButton_2->isChecked())
{
QByteArray垂直(“\x15\x7B\x19\xA9”);//垂直诱惑
串行写入(Vert);
}
如果(ui->radioButton\u 3->isChecked())
{
QByteArray橙色(“\x15\x7B\x1B\xAB”);
串行写入(橙色);
}
}
}

ui->comboBox->currentText()
是一个
QString
。是否要检查
QString
是否为空?当combox为“SP157B”时,我想使用if(ui->radioButton->isChecked()),但我不能使用if语句,我想知道还有其他方法吗?还感谢您可能想将
QString
与字符串文字进行比较,而不必担心
QString
来自组合框。我不明白。您能给我举个例子吗?
QString text=ui->combobox->currentText()
then
if(text.compare(“SP157B”)==0){//文本匹配}