QAction *saveAction = new QAction(tr("&Save"), this); connect(saveAction, &QAction::triggered, this, &MainWindow::save); saveAction->setEnabled(false); // disable Save action by default ... if (ui->textEdit->toPlainText().isEmpty()) saveAction->setEnabled(false); // Disable Save action if there is no text in textEdit else saveAction->setEnabled(true); // Enable Save action if there is text in textEdit
QAction *printAction = new QAction(tr("&Print"), this); printAction->setEnabled(false); // disable Print action by default connect(printAction, &QAction::triggered, this, &MainWindow::print); ... connect(ui->checkBox, &QCheckBox::stateChanged, [=](int state) { if(state == Qt::Checked) printAction->setEnabled(true); // Enable Print action if checkbox is checked else printAction->setEnabled(false); // Disable Print action if checkbox is unchecked });In this example, the "Print" action is disabled by default, and its state is updated based on the state of a checkbox. Package/library: Qt. In conclusion, the setEnabled function is a useful feature provided by Qt library and can be used to enable or disable QAction objects based on various conditions.