QT / C ++另一个关于访问UI文件的问题 [英] QT/C++ yet another issue about accessing UI files

查看:1465
本文介绍了QT / C ++另一个关于访问UI文件的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

虽然我已阅读此问题的答案

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 the objectName. It's the first property in the Property Editor if you open the file with the Designer. I would name it MyWidget

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_



查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆