[Qt-interest] QWizardPage::wizard() overprotected?
Scott Aron Bloom
Scott.Bloom at sabgroup.com
Thu Dec 11 16:35:27 CET 2008
> -----Original Message-----
> From: qt-interest-bounces at trolltech.com [mailto:qt-interest-
> bounces at trolltech.com] On Behalf Of Niels Dekker - no return address
> Sent: Thursday, December 11, 2008 1:50 AM
> To: qt-interest at trolltech.com
> Subject: Re: [Qt-interest] QWizardPage::wizard() overprotected?
>
> Scott Aron Bloom wrote:
> > Since your deriving from WizardPage anyway.. why not just overload
> > wizard() and make it public?
>
> Thanks for your reply. So you say I'd have to override wizard() on
all of my
> derived WizardPage classes, to make it public. But then why wasn't it
public
> in the first place? I mean, what does Qt protect me against?
>
That's a different question... But Im really confused here...
>From any function inside your wizardpage, you can call wizard()... So if
a wizard page needs to set you statusbar it doesn't need any change...
> > That said, why do you need access to the wizard outside of the
> > WizardPage?
>
> >From within any of my pages, I need to find my way back to the status
bar of
> my wizard. Unfortunately, a QWizard cannot have a status bar, so I
had to put
> it onto a QMainWindow. Anyway, to avoid code duplication, I wrote a
single
> function to get the status bar belonging to my wizard:
>
> // Returns the status bar, specific to my application.
> QStatusBar& getStatusBar(const QWizardPage& page)
> {
> // Get the "protected" wizard from the page.
> const QWizard* const wizard = getWizard(page);
> assert(wizard != NULL);
>
> // My wizard has a QMainWindow as parent.
> const QMainWindow* const mainWindow =
> dynamic_cast<const QMainWindow *>(wizard->parent());
> assert(mainWindow != NULL);
>
> QStatusBar* statusBar = mainWindow->statusBar();
> assert(statusBar != NULL);
> return *statusBar;
> }
>
>
> Now I can directly get the status bar, from within any of my pages:
> getStatusBar(*this).showMessage(tr("Status okay!"));
>
> Doesn't that sound reasonable to you?
>
> Kind regards,
Only if you want to violate basic OODA principals... Which sometimes you
have to do, but in this case there are much easier approaches...
First, create a signal in each page
Second, create a signal on your custom wizard
Third, create a slot in you main window
Then when the constructor of the wizard is creating each page, it
connects each pages signal to its own.
When the main window constructs the wizard, it connects the signal to
its slot.
Then any page or the wizard itself can easily set the message of the
status bar... and no hacks necessary..
Or another solution... Create a BASE CLASS..
Ie,
Class MyWizardPage : public QWizardPage
{
QStatusBar * getStatusBar()
{
const QMainWindow* const mainWindow =
dynamic_cast<const QMainWindow *>(wizard->parent());
assert(mainWindow != NULL);
QStatusBar* statusBar = mainWindow->statusBar();
assert(statusBar != NULL);
return statusBar;
}
}
Both of these methods are MUCH MUCH cleaner, in general, if you find
your self creating a global function to take in a class pointer to do
something to the pointer, make it a method on a derived class...
Scott
More information about the Qt-interest-old
mailing list