Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/6.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++ 仅当填写了两个字段时才启用按钮_C++_Qt - Fatal编程技术网

C++ 仅当填写了两个字段时才启用按钮

C++ 仅当填写了两个字段时才启用按钮,c++,qt,C++,Qt,如何实现这样一种功能,即仅当两个行编辑中都填充了文本时才启用按钮?要监视两个行编辑中的更改: connect(lineEdit1, SIGNAL(textChanged(const QString&)), SLOT(checkShouldEnableButton())); connect(lineEdit2, SIGNAL(textChanged(const QString&)), SLOT(checkShouldEnableButton())); 然后,当文本同时出现在以下两

如何实现这样一种功能,即仅当两个行编辑中都填充了文本时才启用按钮?

要监视两个行编辑中的更改:

connect(lineEdit1, SIGNAL(textChanged(const QString&)), SLOT(checkShouldEnableButton()));
connect(lineEdit2, SIGNAL(textChanged(const QString&)), SLOT(checkShouldEnableButton()));
然后,当文本同时出现在以下两个位置时,您需要启用/禁用该按钮:

void YourWidget::checkShouldEnableButton() {
    button->setEnabled(
        !lineEdit1->text().isEmpty() && !lineEdit2->text().isEmpty()
        );
}

如果您只关心用户编辑,则可以使用信号而不是信号。

您希望监视两行编辑的更改:

connect(lineEdit1, SIGNAL(textChanged(const QString&)), SLOT(checkShouldEnableButton()));
connect(lineEdit2, SIGNAL(textChanged(const QString&)), SLOT(checkShouldEnableButton()));
然后,当文本同时出现在以下两个位置时,您需要启用/禁用该按钮:

void YourWidget::checkShouldEnableButton() {
    button->setEnabled(
        !lineEdit1->text().isEmpty() && !lineEdit2->text().isEmpty()
        );
}
如果您只关心用户编辑,则可以使用信号而不是信号。

将两个小部件的textChanged信号连接到同一插槽,该插槽调用

button -> setEnabled (edit1 -> text .size () && edit2 -> text .size ())
将两个小部件的textChanged信号连接到同一个插槽,该插槽调用

button -> setEnabled (edit1 -> text .size () && edit2 -> text .size ())

你能像那样连接不匹配的信号/插槽吗?不是贬低一个相互竞争的答案,而是截断的参数会发生什么情况?@spraff是一个没有参数的函数调用,它只调用方法。。。另一种方法是不可能的。正如@RedX所指出的,Qt在丢弃不必要的参数方面没有问题。但是,要使connect调用成功,存在的参数必须是相同的类型。您可以像这样连接不匹配的信号/插槽吗?不是贬低一个相互竞争的答案,而是截断的参数会发生什么情况?@spraff是一个没有参数的函数调用,它只调用方法。。。另一种方法是不可能的。正如@RedX所指出的,Qt在丢弃不必要的参数方面没有问题。但是,要使connect调用成功,存在的参数必须具有相同的类型。