赞
踩
在用Qt做开发时,为了方便快速,一般都使用Qt设计师界面类来做界面相关的布局,这个类在当前工程中是没有.cpp或.h文件的,但主类又有引入了这个头文件,点开转到定义或声明时,是打不开的,如下图:
如果要把整个项目导成dll和lib,就按当前的文件导出,是会报找不到ui_XXX类的路径的。那什么样才能把设计好的.ui界面导成dll和lib呢,看了好多种办法,有真接是用手写代码的方式去而已界面,这样的工作量有点。下面我演示下我觉得可行办法,虽然有些笨,但起码可以调用。
1.新建一个工程,包含.ui文件,然后打开.ui文件拖入想要的控件,保存,编译.ui文件。
2.打开当前工程,找到目录下的.ui文件,复制下来,新建一个目录把它放进去。
3.然后去qt安路径下找到uic.exe文件,也把它复制到刚刚放.ui文件的目录下,此时当前目录只有两个文件。
4.按住窗口加R件,输入cmd,进入控件台界面,运行uic xxx.ui -o xxx.h或者uic xxx.ui -o xxx.cpp,在当前目录下就多了一个.h或者.cpp文件,这两个文件只要一个就可以了。
5.新建一个工程,选择类库。
6.把刚才生成的.h文件导入到当前工程,然后打开刚刚生成的源码,我们可以看到整个类的所有属性和成员函数。
.ui文件生成的.h文件
/******************************************************************************** ** Form generated from reading UI file 'QtGuiDllDemo.ui' ** ** Created by: Qt User Interface Compiler version 5.7.1 ** ** WARNING! All changes made in this file will be lost when recompiling UI file! ********************************************************************************/ #ifndef QTGUIDLLDEMO_H #define QTGUIDLLDEMO_H #include <QtCore/QVariant> #include <QtWidgets/QAction> #include <QtWidgets/QApplication> #include <QtWidgets/QButtonGroup> #include <QtWidgets/QHeaderView> #include <QtWidgets/QMainWindow> #include <QtWidgets/QMenu> #include <QtWidgets/QMenuBar> #include <QtWidgets/QPushButton> #include <QtWidgets/QStatusBar> #include <QtWidgets/QToolBar> #include <QtWidgets/QWidget> QT_BEGIN_NAMESPACE class Ui_QtGuiDllDemoClass { public: QAction *actionOpen; QWidget *centralWidget; QPushButton *pushButton; QPushButton *pushButton_2; QMenuBar *menuBar; QMenu *menu; QToolBar *mainToolBar; QStatusBar *statusBar; void setupUi(QMainWindow *QtGuiDllDemoClass) { if (QtGuiDllDemoClass->objectName().isEmpty()) QtGuiDllDemoClass->setObjectName(QStringLiteral("QtGuiDllDemoClass")); QtGuiDllDemoClass->resize(1046, 786); actionOpen = new QAction(QtGuiDllDemoClass); actionOpen->setObjectName(QStringLiteral("actionOpen")); centralWidget = new QWidget(QtGuiDllDemoClass); centralWidget->setObjectName(QStringLiteral("centralWidget")); pushButton = new QPushButton(centralWidget); pushButton->setObjectName(QStringLiteral("pushButton")); pushButton->setGeometry(QRect(330, 210, 231, 111)); pushButton_2 = new QPushButton(centralWidget); pushButton_2->setObjectName(QStringLiteral("pushButton_2")); pushButton_2->setGeometry(QRect(400, 430, 231, 81)); QtGuiDllDemoClass->setCentralWidget(centralWidget); menuBar = new QMenuBar(QtGuiDllDemoClass); menuBar->setObjectName(QStringLiteral("menuBar")); menuBar->setGeometry(QRect(0, 0, 1046, 23)); menu = new QMenu(menuBar); menu->setObjectName(QStringLiteral("menu")); QtGuiDllDemoClass->setMenuBar(menuBar); mainToolBar = new QToolBar(QtGuiDllDemoClass); mainToolBar->setObjectName(QStringLiteral("mainToolBar")); QtGuiDllDemoClass->addToolBar(Qt::TopToolBarArea, mainToolBar); statusBar = new QStatusBar(QtGuiDllDemoClass); statusBar->setObjectName(QStringLiteral("statusBar")); QtGuiDllDemoClass->setStatusBar(statusBar); menuBar->addAction(menu->menuAction()); menu->addAction(actionOpen); retranslateUi(QtGuiDllDemoClass); QMetaObject::connectSlotsByName(QtGuiDllDemoClass); } // setupUi void retranslateUi(QMainWindow *QtGuiDllDemoClass) { QtGuiDllDemoClass->setWindowTitle(QApplication::translate("QtGuiDllDemoClass", "QtGuiDllDemo", Q_NULLPTR)); actionOpen->setText(QApplication::translate("QtGuiDllDemoClass", "open", Q_NULLPTR)); pushButton->setText(QApplication::translate("QtGuiDllDemoClass", "\346\216\247\344\273\266", Q_NULLPTR)); pushButton_2->setText(QApplication::translate("QtGuiDllDemoClass", "\347\225\214\351\235\242", Q_NULLPTR)); menu->setTitle(QApplication::translate("QtGuiDllDemoClass", "\346\226\207\344\273\266", Q_NULLPTR)); } // retranslateUi }; namespace Ui { class QtGuiDllDemoClass: public Ui_QtGuiDllDemoClass {}; } // namespace Ui QT_END_NAMESPACE #endif // QTGUIDLLDEMO_H
7.按qt给的格式把.h更改之后加到当前类的下面。
在当前工程的类加了ui类的.h文件
#pragma once #include "guidlltest_global.h" #include <QtCore/QVariant> #include <QtWidgets/QAction> #include <QtWidgets/QApplication> #include <QtWidgets/QButtonGroup> #include <QtWidgets/QHeaderView> #include <QtWidgets/QMainWindow> #include <QtWidgets/QMenu> #include <QtWidgets/QMenuBar> #include <QtWidgets/QPushButton> #include <QtWidgets/QStatusBar> #include <QtWidgets/QToolBar> #include <QtWidgets/QWidget> class GUIDLLTEST_EXPORT GuiDllTest { public: GuiDllTest(); }; QT_BEGIN_NAMESPACE class GUIDLLTEST_EXPORT Ui_QtGuiDllDemoClass { public: QAction *actionOpen; QWidget *centralWidget; QPushButton *pushButton; QPushButton *pushButton_2; QMenuBar *menuBar; QMenu *menu; QToolBar *mainToolBar; QStatusBar *statusBar; void setupUi(QMainWindow *QtGuiDllDemoClass); void retranslateUi(QMainWindow *QtGuiDllDemoClass); }; namespace Ui { class QtGuiDllDemoClass : public Ui_QtGuiDllDemoClass {}; } // namespace Ui QT_END_NAMESPACE
加了ui类的.cpp文件
#include "GuiDllTest.h" GuiDllTest::GuiDllTest() { } void Ui_QtGuiDllDemoClass::setupUi(QMainWindow *QtGuiDllDemoClass) { if (QtGuiDllDemoClass->objectName().isEmpty()) QtGuiDllDemoClass->setObjectName(QStringLiteral("QtGuiDllDemoClass")); QtGuiDllDemoClass->resize(1046, 786); actionOpen = new QAction(QtGuiDllDemoClass); actionOpen->setObjectName(QStringLiteral("actionOpen")); centralWidget = new QWidget(QtGuiDllDemoClass); centralWidget->setObjectName(QStringLiteral("centralWidget")); pushButton = new QPushButton(centralWidget); pushButton->setObjectName(QStringLiteral("pushButton")); pushButton->setGeometry(QRect(330, 210, 231, 111)); pushButton_2 = new QPushButton(centralWidget); pushButton_2->setObjectName(QStringLiteral("pushButton_2")); pushButton_2->setGeometry(QRect(400, 430, 231, 81)); QtGuiDllDemoClass->setCentralWidget(centralWidget); menuBar = new QMenuBar(QtGuiDllDemoClass); menuBar->setObjectName(QStringLiteral("menuBar")); menuBar->setGeometry(QRect(0, 0, 1046, 23)); menu = new QMenu(menuBar); menu->setObjectName(QStringLiteral("menu")); QtGuiDllDemoClass->setMenuBar(menuBar); mainToolBar = new QToolBar(QtGuiDllDemoClass); mainToolBar->setObjectName(QStringLiteral("mainToolBar")); QtGuiDllDemoClass->addToolBar(Qt::TopToolBarArea, mainToolBar); statusBar = new QStatusBar(QtGuiDllDemoClass); statusBar->setObjectName(QStringLiteral("statusBar")); QtGuiDllDemoClass->setStatusBar(statusBar); menuBar->addAction(menu->menuAction()); menu->addAction(actionOpen); retranslateUi(QtGuiDllDemoClass); QMetaObject::connectSlotsByName(QtGuiDllDemoClass); } // setupUi void Ui_QtGuiDllDemoClass::retranslateUi(QMainWindow *QtGuiDllDemoClass) { QtGuiDllDemoClass->setWindowTitle(QApplication::translate("QtGuiDllDemoClass", "QtGuiDllDemo", Q_NULLPTR)); actionOpen->setText(QApplication::translate("QtGuiDllDemoClass", "open", Q_NULLPTR)); pushButton->setText(QApplication::translate("QtGuiDllDemoClass", "\346\216\247\344\273\266", Q_NULLPTR)); pushButton_2->setText(QApplication::translate("QtGuiDllDemoClass", "\347\225\214\351\235\242", Q_NULLPTR)); menu->setTitle(QApplication::translate("QtGuiDllDemoClass", "\346\226\207\344\273\266", Q_NULLPTR)); } // retranslateUi
8.重新生成解决方案,就在当前目录下生成一个dll文件和lib文件,按正常类调用就可以了。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。