如何处理使用.ui文件生成的.h文件

What to do with .h file generated using .ui file?

本文关键字:文件 ui 何处理 处理      更新时间:2023-10-16

我使用QT Desginer创建了一个finalversion_with_buttons.ui文件,后来,我使用命令将其转换为finalversion_with_buttons.h文件

uic -o finalversion_with_buttons.h finalversion_with_buttons.ui

在命令提示符下。

我知道我们不能有.cpp文件,而.h文件包含了我们需要的一切,现在我该如何执行/运行这个.h文件?

请查看Qt Creator文档,例如"创建基于Qt小工具的应用程序"。它将为您提供一些关于如何基于UI表单文件(又名Qt小工具(设置qmake/CMake项目的概述。UI文件本身不能单独使用。这只是UI描述。

创建一个".pro"或".cmake"文件总是有好处的,该文件包含编译项目的所有内容,它有几个好处,甚至对小程序。我强烈建议阅读这些网站,这对我创建/编译项目有很大帮助:https://www.cprogramming.com/tutorial/makefiles.htmlhttps://www.cprogramming.com/tutorial/makefiles_continued.html

这就是qt创建者自动生成的.pro文件所包含的内容,我想这是自我解释:


QT       += core gui multimedia multimediawidgets
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
CONFIG += c++11
# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS
# You can also make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0
SOURCES += 
main.cpp 
mainwindow.cpp
HEADERS += 
mainwindow.h
FORMS += 
mainwindow.ui
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
RESOURCES += 
mainwindow.qrc

如果您使用的是qt创建者的qt设计器部分,那么您就得到了这个xml.ui文件,编译它的最简单方法就是单击Build->Run或ctrl+R。如果你的.pro文件看起来像上面的例子,并且有正确的文件名,你应该可以使用它。

事实上,只有一个c++文件而没有头文件是可能的——这是为了使项目与类更模块化而发明的——这就是c++与c相比的全部。引用Bjarne的话"类用来隐藏丑陋的东西"。。因此,您可以阅读程序并理解它,甚至不知道类文件包含什么,也不需要通过适当的引用来处理代码,而且您不必在意。qt就是这么做的——在它的类中隐藏你必须自己做的丑陋的事情,这样你就可以调用QPushButton,它就可以工作了。(还有很多好处,但为了简单起见,qt只是c++类(

这是代码中没有头文件的类的示例:


[//example from here https://www.cprogramming.com/tutorial/lesson12.html][1]
#include <iostream>
using namespace std;
class Computer // Standard way of defining the class
{
public:
// This means that all of the functions below this(and any variables)
//  are accessible to the rest of the program.
//  NOTE: That is a colon, NOT a semicolon...
Computer();
// Constructor
~Computer();
// Destructor
void setspeed ( int p );
int readspeed();
protected:
// This means that all the variables under this, until a new type of
//  restriction is placed, will only be accessible to other functions in the
//  class.  NOTE: That is a colon, NOT a semicolon...
int processorspeed;
};
// Do Not forget the trailing semi-colon
Computer::Computer()
{
//Constructors can accept arguments, but this one does not
processorspeed = 0;
}
Computer::~Computer()
{
//Destructors do not accept arguments
}
void Computer::setspeed ( int p )
{
// To define a function outside put the name of the class
//  after the return type and then two colons, and then the name
//  of the function.
processorspeed = p;
}
int Computer::readspeed()  
{
// The two colons simply tell the compiler that the function is part
//  of the class
return processorspeed;
}
int main()
{
Computer compute;  
// To create an 'instance' of the class, simply treat it like you would
//  a structure.  (An instance is simply when you create an actual object
//  from the class, as opposed to having the definition of the class)
compute.setspeed ( 100 ); 
// To call functions in the class, you put the name of the instance,
//  a period, and then the function name.
cout<< compute.readspeed();
// See above note.
}

编译器在链接器完成后不会看到其他内容。所以

"我知道我们不能有.cpp文件,而.h文件包含我们需要的一切">

是不对的,因为您可以在上面的示例中看到。只是它不应该如何使用c++(或者早期称为带有类的c(。

但要回答您的问题:

"我如何执行/运行这个.h文件?">

  1. 如前所述,只需使用qt创建者并单击Run或crtl+R(它对开源和edu是免费的(
  2. 创建一个类似前面示例的项目文件,并使用qmake SampleProject.pro在命令行中。这将在项目目录中创建一个名为"Makefile"的文件。(如此处所述https://vitux.com/compiling-your-first-qt-program-in-ubuntu/而不是在同一目录中发出命令make(此处也有说明(
  3. 创建一个make文件,如链接1和2中所述
  4. 其他一切都超出了这个问题的范围,比如篡改使用gcc或g的语义++

也就是说,您可以使用qt创建者创建QPushButtons和所有的东西,也可以只在代码中创建按钮,而不使用.ui-xml文件如本文所述:https://www.bogotobogo.com/Qt/Qt5_LayoutNotUsingDesigner.php

但这里所有的人都强烈建议:给自己买一本好的qt/c++书或教程,学习关于类和qt的基础知识,你很快就会成为一名真正优秀的程序员。我也非常希望这篇文章能够为你澄清很多qt编程/编译,你会开始玩得很开心,并将创建非常好的应用程序:(干杯