views:

297

answers:

4

I would like to have a QMainWindow that can change it's look at runtime, i.e when a user clicks a button. Besides keeping references to different UI classes generated by the QtDesigner, is there another way of doing that? Maybe by storing each UI in a layout ? What do you think ?

+1  A: 

You can dynamically load UI layouts at runtime. Check Trolltech documentation for QUiLoader class.

This also enables you to upgrade UI layouts without modifying a single line of code.

Marcin Gil
It's a solution, but it's a very slow one. Can you think of other ways of achieving this ?
Geo
Unfortunately not. You can always ask on qtcentre.org.
Marcin Gil
A: 

If I'm getting this right, I think you might want to take a loot at style sheets. It allows you to have "skins" for your widgets, much like CSS.

If I didn't get this right, and what you're trying to do is generate .ui files on the fly and create widgets with those using QUiLoader, then you're probably going at this the wrong way since I can't think of a good reason a regular application would need that.

Idan K
Think of it as a tabbed pane of some sort. It's just that I have to display the different UI's in the same tab.
Geo
If I understand correctly, at #1 tab you select some check boxes and contents of next tabs changes.
Marcin Gil
in that case a QWizard sounds appropriate.
Idan K
A: 

I think I got it now.

You have a QMainWindow and when a certain event is triggered you want to change the appearance of that particular window, like remove some buttons, add a treeview widget or what not.

Well the straight forward approach would be to do it manually, remove some widgets and add new ones using regular C++ code. This can be abit hard if you're used to Qt Designer.

The other way I can think of is using Qt Designer to generate the code for the other appearances and copy it to a special function. The code generated by Qt Designer is usually in a header file called "ui_classname.h" and isn't hard to understand. You will however need to remove some of it as not all would be necessary.

Also, instead of copying the generated code from Qt Designer you could just call it. Usually your widget has a pointer to the generated class and in your widget's constructor you see something like this:

MyWindow::MyWindow(QWidget *parent) : QMainWindow(parent), m_ui(new Ui::MyWindow)
{
    m_ui->setupUi(this);
}

and in the corresponding header file:

class MyWindow : public QMainWindow {
...
private:
    Ui::MyWindow *m_ui;
};

You could add the additional generated classes for the other appearances and use them when your event triggers.

It might look like this:

class MyWindow : public QMainWindow {
...
private:
    void changeAppearance(int id);

    Ui::MyWindow *m_ui;
    Ui::MyWindowFirstAppearance *m_uiFirst;
    Ui::MyWindowSecondAppearance *m_uiSecond;
...
};

void MyWindow::changeAppearance(int id)
{
     // some code to remove the current appearance, basically the opposite of what setupUi is doing
     if (id == 0)
           m_ui->setupUi(this);
     else...
           m_uiFirst->setupUi(this);
     ...
}

This has the benefit of using the generated classes directly, so every change you do in Qt Designer doesn't require a change to your main window. The problem is I'm not sure if it's legal to call setupUi more than once and in a place other than your widget's constructor, so you'll have to check that (by looking at what's happening in the setupUi function).

Idan K
Yeah, this is the approach I'm using so far, the thing is you need to hold references to the appearances . Thanks !
Geo
+5  A: 

How much of the main window do you want to change? You could use a QStackWidget any place you want to change things, and change the shown page of the widget when the button is pressed. This will be quick to change, but for large or complicated UIs it may be slightly slower at startup, since it will be creating the widgets for both UIs at the same time. (There are fairly easy ways to change this, also, but they do add complications to something that could be straightforward for most people.) Also, if both layouts should have the same data, just in different places, you have the additional overhead of keeping both sets of UIs up to date while the program is running.

Caleb Huitt - cjhuitt
I want to change all of the UI. Think of them as completely different screens, with the same window dimension ( width/heigth )
Geo