Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9092989
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T22:58:32+00:00 2026-06-16T22:58:32+00:00

I would like to share an object (a QDir) between several pages of a

  • 0

I would like to share an object (a QDir) between several pages of a QWizard, so that if one page changes the value of this directory, this carries through to all other pages.nSince this object is not optional, I would like to share it by passing a reference rather than a pointer so I do not need to worry about a possible null pointer (is this correct? I’ve read opinions both ways). Here is a minimal test case:

test.hpp:

#include <QApplication>
#include <QWizard>
#include <QDir>
#include <QVBoxLayout>
#include <QWizardPage>
#include <QLineEdit>

class TestWizard : public QWizard {
    Q_OBJECT
public:
    TestWizard(QWidget* parent = 0);
    QDir directory;
};

class TestPage : public QWizardPage {
    Q_OBJECT

public:
    TestPage(QDir& _directory, int _id, QWidget* parent = 0);
    void initializePage();

public slots:
    void setDirectory(QString new_dir);

private:
    QVBoxLayout* layout;
    QDir directory;
    QLineEdit* dir_edit;
    int id;

};

test.cpp:

#include "test.hpp"
#include <iostream>

TestWizard::TestWizard(QWidget* parent) : QWizard(parent){
    directory = QDir();
    addPage(new TestPage(directory, 0));
    addPage(new TestPage(directory, 1));
    addPage(new TestPage(directory, 2));
}
TestPage::TestPage(QDir& _directory, int _id, QWidget* parent)
    : QWizardPage(parent){

    directory = _directory;
    id = _id;

    layout = new QVBoxLayout;
    dir_edit = new QLineEdit;
    layout->addWidget(dir_edit);
    this->setLayout(layout);
    connect(dir_edit, SIGNAL(textChanged(QString)),
        this, SLOT(setDirectory(QString)));
}
void TestPage::initializePage(){
    std::cout << id << ": " << directory.absolutePath().toUtf8().constData()
              << std::endl;
}
void TestPage::setDirectory(QString new_dir){
    directory.setPath(new_dir);
}
int main(int argc, char* argv[]){
    QApplication app(argc, argv);
    TestWizard wizard;
    wizard.show();
    return app.exec();
}

Clicking through this and editing the text gives the result that changing one text only affects the directory variable on one page. I know how to do this with a pointer, but I would like to understand why this does not work.

It looks like I have a basic misconception somewhere about what is behind this and I’d like to clear it up before I do something stupid 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T22:58:34+00:00Added an answer on June 16, 2026 at 10:58 pm

    I think I have been a bit of an idiot, but just to confirm:

    test.cpp:

    TestPage::TestPage(QDir& _directory, int _id, QWidget* parent)
        : QWizardPage(parent){
    
        directory = _directory; // This is where I'm going wrong
    ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some common code that I would like to share between pages and
Problem: I would like to share code between multiple assemblies. This shared code will
I would like to share an object between various instances of objects of the
I have an API Object that I made, and I would like to share
I would like to share my Oracle SQL Developer configuration across my several computers
My problem is: I have 3 procs that would like to share config loaded
So I'm creating an array called fruits which I would like to share between
Helllo, I would like to share small amounts of data (< 1K) between python
I'm creating an array of BackgroundWorker that shares one event handler like this: BackgroundWorker[]
I would like to share data between the objects. Actually, for example, there is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.