c++ - QT/C++ 关于访问 UI 文件的另一个问题

标签 c++ qt user-interface header-files signals-slots

虽然我看过这个问题的答案

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 但不使用 Designer,但是“手动编码”UI 似乎没有什么意义)

所以现在我的工作是理清开放的一端,创建一个 GUI(完成)找到每个可能的信号和插槽,并将它们放在一起看起来漂亮干净。

理论部分就这么多。

补充:我对C++的经验很少,我似乎无处寻找答案,直到明天才有时间阅读整本书,否则我不会问。

我对两件事有点抓狂:

A) 我的 mainwindow.cpp、mainwindow.h 和 mainwindow.ui 需要链接到其他文件,例如previewwidget.cpp ... previewwidget.cpp 有很多代码,例如:

 buttonLayout->addWidget(fpsSpinBox, 0, Qt::AlignCenter);
 buttonLayout->addWidget(playButton, 0, Qt::AlignCenter);
 buttonLayout->addWidget(backwardButton, 0, Qt::AlignCenter);

显然我通过在设计器中创建相应的按钮来替换它。 现在在同一个文件中有连接 SIGNAL SLOT 条目(我添加了“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: Error:'ui' was not declared in this scope

我将 ui_mainwindow.h 放入标题中,但这也不是解决方案。


B) 这个问题可能与第一个问题密切相关:由于设计师严格地将模型/ View /控件分开,我需要重写信号和槽以匹配新的 UI - 有任何人都得到了一个很好的教程或任何提示,告诉我如何快速而简单地做到这一点?

如有任何帮助,我们将不胜感激。

最佳答案

假设您有一个名为 MyWidget 的类和一个相应的 ui 文件 MyWidget.ui。为了在您的类里面使用它,我将执行以下操作:

  • MyWidget.ui 中为 objectName 设置一个值。如果您使用设计器打开文件,它是属性编辑器中的第一个属性。我会将其命名为 MyWidget

  • MyWidget.h 中,您必须执行以下操作:

    • 为 ui 对象声明一个命名空间并向前声明它。
    • 添加一个指向 ui 对象的指针作为成员变量(私有(private))。

示例头文件如下:

#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

示例代码:

#include "MyWidget.h"
#include "ui_MyWidget.h"

MyWidget::MyWidget(QWidget *parent)
:QWidget(parent), ui(new Ui::MyWidget)
{
    ui->setupUi(this);
}

MyWidget::~MyWidget()
{
    delete ui;
}

现在您已准备好在类里面使用该用户界面。例如,如果您有一个名为 spinBox1 的旋转框,您可以使用

获取它的值
int val = ui->spinBox1->value();

.ui 信号和槽

我建议您使用 QtDesigner 来建立 ui 小部件和插槽之间的连接。检查this link更多细节。

如果您想将小部件与自定义插槽连接,您可以再次执行此操作 using the designer .

  1. Switch to Edit Signals/Slots mode (F4)
  2. Drag and drop from the widget which it to emit the signal, to the widget which is to receive the signal.
  3. 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.
  4. 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.
  5. 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.

关于c++ - QT/C++ 关于访问 UI 文件的另一个问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8868916/

相关文章:

c++ - Debug和Release模式下的AES加密问题

c++ - 在 Windows 10 上尝试获取默认 Web 浏览器路径失败

c++ - 在 OS X 中针对不同的 SDK 运行应用程序?

c++ - 如何将 "pull"数据从c++转qml?

c++ - 将 FFTW 库添加到 QT 项目中

java - 设置UI内容仅添加最后一行

java - 是否可以在 Eclipse-RCP 中定义 ViewPart 或 Workbench 的最小尺寸?

c++ - 在 C++ 中生成包含负范围的随机数

c++ - QT中直接写入设备

c++ - 在 C++ 中创建 WinAPI GUI 应用程序的现代 native 方式