Solved getting value of a field from wizard
-
i have a wizard with pages, and there's a combo box in one of pages, i register it using
registerField()
, then read the value of that field onaccept()
slot.and somehow i get 1 instead of the actual string, for non empty inputs i get 1, for empty ones - 0.
notice i've set default property for that combo box in wizard subclass.
any idea why?
here's code for
wizard.h
#pragma once #include <QPushButton> #include <QtWidgets/QWidget> #include <QtWidgets/QComboBox> #include <QWizard> #include <QWizardPage> #include <QHBoxLayout> #include <QLabel> #include <QFileDialog> #include <QDebug> class WizardPage : public QWizardPage { public: WizardPage(QWidget *p = nullptr) : QWizardPage(p) { _file_info_label = new QLabel(); _file_combo_box = new QComboBox(this); _file_combo_box->addItem(""); _file_combo_box->setEditable(true); browse_btn = new QPushButton(this); browse_btn->setMaximumWidth(30); connect(browse_btn, &QAbstractButton::clicked, this, &WizardPage::onClicked); auto input_layout = new QHBoxLayout(); input_layout->addWidget(_file_combo_box); input_layout->addWidget(browse_btn); auto page_layout = new QVBoxLayout(); page_layout->addWidget(_file_info_label); page_layout->addLayout(input_layout); setLayout(page_layout); } public slots: virtual void onClicked(){} protected: QPushButton * browse_btn; QLabel * _file_info_label; QComboBox * _file_combo_box; }; class FilePage : public WizardPage { public: FilePage(QWidget *p = nullptr) : WizardPage(p) { _file_info_label->setText("iNPUT:"); registerField("file", _file_combo_box); } public slots: virtual void onClicked() { auto path = QFileDialog::getExistingDirectory(this, "Open Directory"); if (!path.isEmpty()) { _file_combo_box->addItem(path); _file_combo_box->setCurrentIndex( _file_combo_box->count() - 1); } } }; class OutPage : public WizardPage { public: OutPage(QWidget *p = nullptr) : WizardPage(p) { _file_info_label->setText("output"); registerField("aaa", _file_combo_box); } public slots: virtual void onClicked() { } }; class Wiz : public QWizard { public: Wiz(QWidget *p = nullptr) : QWizard(p) { addPage(new FilePage); addPage(new OutPage); setDefaultProperty("QComboBox", "file", SIGNAL(currentTextChanged(const QString &))); } void accept() { QString a = field("file").toString(); qDebug() << a; } };
and
main.cpp
#include <QApplication> #include "wizard.h" int main(int argc, char *argv[]) { QApplication app(argc, argv); auto pWizard = new Wiz; pWizard->show(); return app.exec(); }
-
Hi @user4592357,
somehow i get 1 instead of the actual string, for non empty inputs i get 1, for empty ones - 0.
registerField("file", _file_combo_box);
As per the QWizardPage::registerField() documentation:
QWizard knows the most common Qt widgets. For these (or their subclasses), you don't need to specify a property or a changedSignal. The table below lists these widgets:
Widget Property Change Notification Signal QComboBox int currentIndex currentIndexChanged()
So the property being chosen by default, is QComboBox::currentIndex. Sounds like you want to use the QComboBox::currentText property instead. Presumably (haven't tried) you can do:
registerField("file", _file_combo_box, "currentText", SIGNAL(currentTextChanged(QString)));
Note, I would guess that the Qt default is the index, because that is more reliable for things like internationalisation (just it case that's important to your application).
Cheers.