[Kde-bindings] Qt, Qyoto buttons work intermediately
arno at arnorehn.de
Sat Feb 20 15:08:28 UTC 2010
I really didn't want to give you any advice in good application design.. but
to get you going:
On Saturday 20 February 2010 15:30:24 linuxoidoz at yahoo.com.au wrote:
> Sorry, I've probably confused you by windows, they're not actually windows
> but rather QStackedWidget pages. Here's my example:
> I'm writing an application to convert video, audio and image files. So in
> its basic form I want to have a main general class with a StackedWidget
> and separate classes which will draw different gui widgets on different
> StackedWidget pages. So
> 1. I have a MainWindow class - main class with QStackedWidget
> 2. class StartWindow - start page with 3 button: Video, Audio, Image
> 3. class VideoWindow - video converter page
> 3. class AudioWindow - audio converter page
> 3. class ImageWindow - image converter page
> But I don't want to draw 4 pages (because only one will be displayed at a
> time and also to minimise the code). So I only create the start page and a
> second page based on what button has been pressed on the start page. And
> then just switch between the two by changing the StackedWidget page index.
> And when you return to the start page, the other page is destroyed.
> Confused? Well, what I can't figure out is, for example,
That completely defeats the purpose of a QStackedWidget. It's intended to hold
multiple widgets and always draw only one at a time. Qt won't draw all four
pages when just one is shown.
Make all your pages subclasses of QWidget. Create the UI's in their respective
constructors. Then create an instance of every page in MainWindow's
constructor and add every page with stackedWidget.AddPage(page) (again, take a
look at the Qt documentation. It's explained very nicely there).
> 1. How can I add a Video page to the StackedWidget from StartWindow if the
> StackedWidget itself is declared in MainWindow? How do you pass the page
> reference in the signal/slot which will do the page switch?
Create a custom signal. It's the same as with C# events here: If you have an
event which doesn't take any parameters, you can't just raise it with an
arbitrary number of parameters. Instead, you have to declare you own event.
It doesn't have to be a Qt signal in this case, either. You can actually use a
C# event. Do it like this:
In your StartPage widget, declare an event "PageChangeRequested" which takes
an integer parameter. Depending on which button the user clicked, you raise
this event with a different integer parameter (1 for video, 2 for audio, 3 for
image, for example).
In your MainWindow, subscribe to that event. In the event handler, just use
stackedWidgets.SetCurrentIndex(<parameter that you received from the event>);
And there you are.
> 2. Or even
> simpler, how can I change text in the status bar from another page if the
> status bar is declared in MainWindow? How do you pass a reference to the
> main window status bar in a signal declaration? How can you pass that
> variable if it only takes types?
Pass a reference of the statusBar widget to the constructor of the different
pages and save it away in a member variable. But that's no good OOP either.
Best way would again be to define a custom signal/event in your page widget.
Make it take a string and subscribe a method in MainWindow to it. From within
the MainWindow you can then easily change the statusBar text. The page widget
itself shouldn't really care about status bars. It should just signal "hey
there, I have a new status update. Does anybody care to display it anywere?".
The MainWindow can then say "Hey you page widget! Yes, I do care, give me the
text, I'll display it in a status bar!"
As IBBoard already said, your problems are general object orientation /
programming issues. Wikipedia has a nice article on that, maybe you want to
read it: http://en.wikipedia.org/wiki/Object_orientation . I'll post the key
sentence here: "Programming techniques may include features such as data
abstraction, encapsulation, modularity, polymorphism, and inheritance."
Currently you're missing (at least) encapsulation, modularity and inheritance.
I hope I could point you to the right direction. I won't reply to this any
further because it's not really related to bindings anymore.
arno at arnorehn.de
More information about the Kde-bindings