Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. getting value of a field from wizard
Qt 6.11 is out! See what's new in the release blog

getting value of a field from wizard

Scheduled Pinned Locked Moved Solved General and Desktop
2 Posts 2 Posters 900 Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • U Offline
    U Offline
    user4592357
    wrote on last edited by
    #1

    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 on accept() 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();
    }
    
    Paul ColbyP 1 Reply Last reply
    0
    • U user4592357

      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 on accept() 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();
      }
      
      Paul ColbyP Offline
      Paul ColbyP Offline
      Paul Colby
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      3

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved