Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/145.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何检查两个QLineEdit是否为空_C++_Qt_Qt5 - Fatal编程技术网

C++ 如何检查两个QLineEdit是否为空

C++ 如何检查两个QLineEdit是否为空,c++,qt,qt5,C++,Qt,Qt5,我有三个控件,两个QTextLine和一个QPushButton 启动程序时,添加按钮将被禁用,并且必须为两个QTextLine非空才能启用添加按钮 我有以下代码,但它不能正常工作: void Question_Answer::on_newQuestion_txt_textChanged(const QString &arg1) { if(arg1.isEmpty()) { ui->addNewQuestion_btn->setEnabled

我有三个控件,两个
QTextLine
和一个
QPushButton

启动程序时,添加按钮将被禁用,并且必须为两个
QTextLine
非空才能启用添加按钮

我有以下代码,但它不能正常工作:

void Question_Answer::on_newQuestion_txt_textChanged(const QString &arg1)
{
    if(arg1.isEmpty())
    {
        ui->addNewQuestion_btn->setEnabled(false);
    }
    else
    {
        ui->addNewQuestion_btn->setEnabled(true);
    }
}

void Question_Answer::on_newAnswer_txt_textChanged(const QString &arg1)
{
    if(ui->newAnswer_txt->text().isEmpty())
    {
        ui->addNewQuestion_btn->setEnabled(false);
    }
    else
    {
        ui->addNewQuestion_btn->setEnabled(true);
    }
}

现在,如何检查两个
QTextLine
是否为空,以及如果其中任何一个为空,如何禁用添加按钮。

只需连接一个插槽即可处理两个
行编辑的
textChanged
信号

void Question_Answer::onTextChanged(const QString &arg1){
    if(ui->newAnswer_txt->text().isEmpty() || ui->newQuestion_txt->text().isEmpty()){
        ui->addNewQuestion_btn->setEnabled(false);
    }else{
        ui->addNewQuestion_btn->setEnabled(true);
    }
}

只需连接一个插槽即可处理两个
LineEdits的
textChanged
信号

void Question_Answer::onTextChanged(const QString &arg1){
    if(ui->newAnswer_txt->text().isEmpty() || ui->newQuestion_txt->text().isEmpty()){
        ui->addNewQuestion_btn->setEnabled(false);
    }else{
        ui->addNewQuestion_btn->setEnabled(true);
    }
}
在header类中:

// ...
private slots:
    void onTextChanged();
// ...
在源文件中:

// Setup the connections in the constructor.

void Question_Answer::onTextChanged()
{
    const bool editable1 = ui->newAnswer_txt->text().size() > 0;
    const bool editable2 = ui->newQuestion_txt->text().size() > 0;
    ui->addNewQuestion_btn->setEnabled( editable1 && editable2 );
}
在header类中:

// ...
private slots:
    void onTextChanged();
// ...
在源文件中:

// Setup the connections in the constructor.

void Question_Answer::onTextChanged()
{
    const bool editable1 = ui->newAnswer_txt->text().size() > 0;
    const bool editable2 = ui->newQuestion_txt->text().size() > 0;
    ui->addNewQuestion_btn->setEnabled( editable1 && editable2 );
}

谢谢,但是我可以把connect方法放在哪里?您应该已经添加了两个
connect
调用。只需将它们的插槽都换成
onTextChanged
对不起,你能给我举个例子吗。因为我使用了设计模式来添加插槽。在QtDesigner中,底部有一个名为
Signals&slots Editor
的窗格。这样,您就可以看到已经建立的信号插槽连接。对于
newAnswer\u txt
newQuestion\u txt
textChanged
信号,应该有两个连接。将两者的插槽都更改为
onTextChanged
。接收器如何?谢谢,但我可以将连接方法放在哪里?您应该已经添加了两个
connect
调用。只需将它们的插槽都换成
onTextChanged
对不起,你能给我举个例子吗。因为我使用了设计模式来添加插槽。在QtDesigner中,底部有一个名为
Signals&slots Editor
的窗格。这样,您就可以看到已经建立的信号插槽连接。对于
newAnswer\u txt
newQuestion\u txt
textChanged
信号,应该有两个连接。将两者的插槽都更改为
onTextChanged
。接收器如何?