문제

I have a subclass of QWizard generated by Qt Creator's Form Wizard. As described in the docs, I re-implemented nextId() to validate inputs and create a non-linear wizard. The problem is that nextId() is called twice: once at entering the page and once at exiting. I need a simple last-minute check on fields and then guide user through next page, or show an error message and keep user in current page.

Note: I've made the wizard using Qt Creator's Form generator. Pages are included in the ui file. So re-implementing QWizardPage::nextId() is not an option.

Update: Here is code:

int WizardBackupDatabase::nextId() const
{
    Page nextPage;
    Page currentPageType = static_cast<Page>(currentId());
    qDebug() << currentId(); // This prints twice
    switch (currentPageType) {
    case Page::Intro:
        nextPage = Page::DataSource;
        break;
    case Page::DataSource:
        if(checkSource()) {
            nextPage = Page::Settings;
        }
        else {
            nextPage = Page::DataSource;
        }
        break;
    case Page::Settings:
        if(checkSettings()) {
            nextPage = Page::Verify;
        }
        else {
            nextPage = Page::Settings;
        }
        break;
    case Page::Verify:
        nextPage = Page::Operation;
        break;
    case Page::Operation:
        return -1;
    default:
        return -1;
    }
    return static_cast<int>(nextPage);
}
도움이 되었습니까?

해결책

By the time nextId is called, you're already committed to switching the page. It simply doesn't make sense to do any validation at that point.

It is in fact a semantic error to have a nextId implementation perform any validation, or display any user interface. It can be called as many times as the implementation wishes, and your code should cope with it. Just think about what this method is equivalent to. It's a query method not unlike the size method on a container.

To validate the inputs, you should ideally reimplement isComplete, or declare mandatory fields, or reimplement validateCurrentPage.

validateCurrentPage is called by QWizard::next. If it returns true, then nextId is called to query the page to switch to, and the pages are switched. The private switchToPage code calls nextId again to verify that the next page isn't invalid. That's the source of the double call that you experience.

Your checkSettings calls don't belong in nextId at all. They belong in validateCurrentPage.

Qt source code is conveniently available through the woboq source code browser. Sometimes source code is better than any documentation. If you believe that the documentation is incomplete, I'm sure a well-done contribution that fixes the deficiency will be gladly accepted :)

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top