我想有一个QMainWindow可以改变它在运行时的外观,即当用户点击按钮时。 除了保持对QtDesigner生成的不同UI类的引用外,还有另一种方法吗? 也许通过在布局中存储每个UI? 你怎么看 ?
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 ?
最满意答案
我想我现在明白了。
你有一个QMainWindow,当某个事件被触发时你想要改变那个特定窗口的外观,比如删除一些按钮,添加一个树形视图小部件或者不是。
那么简单的方法就是手动完成,删除一些小部件并使用常规C ++代码添加新部件。 如果你习惯了Qt Designer,这可能会很难。
我能想到的另一种方式是使用Qt Designer为其他外观生成代码并将其复制到特殊功能中。 Qt Designer生成的代码通常位于名为“ui_classname.h”的头文件中,并不难理解。 但是,您将需要删除其中的一部分,因为并非所有的都是必要的。
另外,您可以直接调用它,而不是从Qt Designer复制生成的代码。 通常你的小部件有一个指向生成的类的指针,并且在你的小部件的构造函数中,你会看到如下的东西:
MyWindow::MyWindow(QWidget *parent) : QMainWindow(parent), m_ui(new Ui::MyWindow) { m_ui->setupUi(this); }并在相应的头文件中:
class MyWindow : public QMainWindow { ... private: Ui::MyWindow *m_ui; };您可以为其他外观添加其他生成的类,并在事件触发时使用它们。
它可能看起来像这样:
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); ... }这有直接使用生成的类的好处,因此您在Qt Designer中所做的每个更改都不需要更改主窗口。 问题是我不确定在不止一次的情况下调用setupUi是否合法,并且在你的构件的构造函数之外的地方是合法的,所以你必须检查(通过查看setupUi函数中发生了什么)。
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).
更多推荐
发布评论