QT / C ++另一个关于访问UI文件的问题 [英] QT/C++ yet another issue about accessing UI files
问题描述
虽然我已阅读此问题的答案
Although I have read the answers to this question
QT / C ++ - 从不同的类访问MainWindow UI
并搜索整个www相当一段时间 - 不要让它工作。
QT/C++ - Accessing MainWindow UI from a different class and searched the whole www for quite some time - I don't get it working.
我被赋予使用QT Designer重新创建应用程序UI的任务。不幸的是,已经有一些UI传播了几个类(该团队然后遇到了这个想法,使用QT但不使用设计器,然而手写编码UI似乎没有意义)
I was given the task to recreate an applications UI using QT Designer. Unfortunately there was already some UI spread all over several classes (the team then came across the idea, that using QT but not using the Designer however "handcoding" the UI seems to make little sense)
现在我的工作是解开开放的两端,创建一个GUI(做),找到每一个可能的信号和插槽,并把它放在一起看起来不错,干净。
So now my job is to untangle the open ends, create a GUI (done that) find every possible signal and slot and put it all together to look nice and clean.
对于理论部分来说太多了。
So much for the theoretical part.
添加:我在C ++中的经验很少,无法搜索答案,没有时间读整本书,直到明天,否则我不会问。
Addition: I have very little experience in C++ and I seem to be getting nowhere searching for answers and don't have the time to read whole books until tomorrow, otherwise I wouldn't ask.
我有两个事情:
A)我的mainwindow.cpp,mainwindow.h和mainwindow.ui需要链接到其他文件例如previewwidget.cpp ...
previewwidget.cpp有很多代码:
A) My mainwindow.cpp, mainwindow.h and mainwindow.ui need to be linked into other files e.g. the previewwidget.cpp... the previewwidget.cpp had a lot of code like:
buttonLayout->addWidget(fpsSpinBox, 0, Qt::AlignCenter);
buttonLayout->addWidget(playButton, 0, Qt::AlignCenter);
buttonLayout->addWidget(backwardButton, 0, Qt::AlignCenter);
显然我在Designer中创建了相应的按钮。
现在在同一文件中连接SIGNAL SLOT条目(我添加了ui - >)
apparently I replaced it by creating the corresponding buttons in Designer. now in the same file theres the connect SIGNAL SLOT entries (I added the "ui->")
connect(ui->playButton, SIGNAL(clicked()), this, SIGNAL(playButtonClicked()));
connect(ui->stopButton, SIGNAL(clicked()), this, SIGNAL(stopButtonClicked()));
connect(ui->forwardButton, SIGNAL(clicked()), this, SIGNAL(forwardButtonClicked()));
但编译器一直告诉我:
\preview\previewwidget.cpp:77:错误:'ui'未在此范围内声明
\preview\previewwidget.cpp:77: Error:'ui' was not declared in this scope
我把ui_mainwindow.h放到标题中,但这不是解决方案。
I put the ui_mainwindow.h into the header but that wasn't the solution either.
B)这个问题可能非常接近第一个:由于设计师strickly保持模型/视图/控制分开我需要重写信号和插槽,以匹配新的UI - 任何人有一个很好的教程或任何提示我如何做这个快速和简单?
B) This Question is probably related very closely to the first one: since the Designer strickly keeps model/view/controll apart I need to rewrite the signals and slots to match the new UI - has anyone got a good tutorial or any hints for me how to do this fast and uncomplicated?
任何帮助将非常感激。
推荐答案
有一个类 MyWidget
和相应的 ui
文件 MyWidget.ui
。为了在您的课程中使用它,我将执行以下操作:
Let's assume you have a class called MyWidget
and a corresponding ui
file MyWidget.ui
. In order to use it in your class I would do the following:
-
在
MyWidget.ui
为objectName
设置一个值。如果您使用Designer打开文件,它是属性编辑器中的第一个属性。我将命名为MyWidget
In the
MyWidget.ui
set a value to theobjectName
. It's the first property in the Property Editor if you open the file with the Designer. I would name itMyWidget
在 MyWidget.h
您必须执行以下操作:
In the MyWidget.h
you have to do the following:
- 为ui对象声明一个命名空间并向前声明它。
- 添加作为成员变量(private)指向ui对象的指针。
示例头文件如下:
#ifndef MY_WIDGET_H_
#define MY_WIDGET_H_
#include <QWidget>
namespace Ui {
class MyWidget;
}
class MyWidget : public QWidget {
Q_OBJECT
public:
MyWidget(QWidget* parent = NULL);
~MyWidget();
// Add other class functions
private:
Ui::MyWidget ui;
}
#endif // MY_WIDGET_H_
- 在
MyWidget.cpp
- 包括自动生成的
ui_MyWidget.h $>
- 在构造函数中创建一个新的ui对象
- 在构造函数中调用setupUi函数
- 删除析构函数中的ui
- In the
MyWidget.cpp
- Include the automatically generated
ui_MyWidget.h
- Create a new ui object in the constructor
- Call the setupUi function in the constructor
- Delete the ui in the destructor
示例代码:
#include "MyWidget.h" #include "ui_MyWidget.h" MyWidget::MyWidget(QWidget *parent) :QWidget(parent), ui(new Ui::MyWidget) { ui->setupUi(this); } MyWidget::~MyWidget() { delete ui; }
现在您可以在整个课程中使用ui。例如,如果你有一个叫做
spinBox1
的旋转框,你可以使用Now you are ready to use the ui throughout your class. For example if you have a spin box called
spinBox1
you can take its value usingint val = ui->spinBox1->value();
.ui信号和广告位
将建议您使用
QtDesigner
,以便进行ui窗口小部件和插槽之间的连接。有关详情,请查看此链接。.ui Signals and Slots
I would advise you to use
QtDesigner
in order to make the connections between ui widgets and slots. Check this link for more details.如果您想要将小部件与自定义插槽相连,您可以再次执行。
If you want to connect a widget with a custom slot you can again do it using the designer.
- 切换到编辑信号/插槽模式(F4)
- 将显示一个配置连接对话框,其中显示了发射小部件的信号,以及用于接收小部件的插槽。点击右侧插槽列下方的
编辑...。 - 将显示信号/接收小组件对话框对话框。在这里可以点击插槽下面的加号图标来添加
任何名称的新插槽。 - 然后,您可以返回并连接到配置中的新插槽
- Switch to Edit Signals/Slots mode (F4)
- Drag and drop from the widget which it to emit the signal, to the widget which is to receive the signal.
- A Configure Connection dialog appears, showing the signals for the emitting widget, and the slots for the receiving widget. Click Edit... below the slots column on the right.
- A Signals/Slots of ReceivingWidget dialog appears. In here its is possible to click the plus icon beneath slots to add a new slot of any name.
- You can then go back and connect to your new slot in the Configure Connection dialog, or indeed in the Signal/Slot Editor dockwidget back in the main window.
这篇关于QT / C ++另一个关于访问UI文件的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
- Include the automatically generated
- 包括自动生成的