【发布时间】:2018-11-26 10:54:56
【问题描述】:
使用 Qt Designer,当我提升 Widget 时,我遇到了 ui_*.h 文件和提升类的头文件包含问题。该问题出现在未找到提升类的标头的 Linux 下。 Windows下有MSVC 2017,没问题...
在windows下,我放的是*.ui文件夹的相对路径。
在 Linux 下,似乎我必须从 *.pro 文件夹中放置相关文件...如果我必须在另一个项目中重用小部件,但这不是很好,但它可以工作
谁能解释一下?
谢谢!
编辑:
我在下面添加了示例代码。
你需要生成的ui_文件吗?
您可以在最后一个文件中看到 mycombobox.h 的路径是相对于根文件夹而不是 *.ui 文件夹的路径。
这个配置好像在windows下也能用,不过我想把*.ui文件的相对路径放上来。
我的文件夹结构:
in the root :
main.cpp
mainWindows.h
mainwindows.cpp
/Folder1 :
MyComboBox.h
/Folder1/Folder2 :
Form.h
Form.cpp
main.cpp 文件:
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
mainwindows.h:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QHBoxLayout>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = nullptr);
~MainWindow();
private:
Ui::MainWindow *ui;
};
mainwindows.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "folder1/folder2/form.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
centralWidget()->setLayout(new QHBoxLayout);
centralWidget()->layout()->addWidget(new Form);
}
MainWindow::~MainWindow()
{
delete ui;
}
/folder1/MyComboBox.h
#ifndef MYCOMBOBOX_H
#define MYCOMBOBOX_H
#include <QComboBox>
class MyComboBox:public QComboBox
{
Q_OBJECT
public:
MyComboBox(QWidget* parent=nullptr):QComboBox(parent)
{
addItem("My ComboBox");
}
virtual ~MyComboBox() {}
};
/folder1/folder2/Form.h
#ifndef FORM_H
#define FORM_H
#include <QWidget>
namespace Ui {
class Form;
}
class Form : public QWidget
{
Q_OBJECT
public:
explicit Form(QWidget *parent = nullptr);
~Form();
private:
Ui::Form *ui;
};
/folder1/folder2/Form.cpp
#include "form.h"
#include "ui_form.h"
Form::Form(QWidget *parent) :
QWidget(parent),
ui(new Ui::Form)
{
ui->setupUi(this);
}
Form::~Form()
{
delete ui;
}
最后但同样重要的是,Form.ui 文件:
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Form</class>
<widget class="QWidget" name="Form">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>300</height>
</rect>
</property>
<property name="windowTitle">
<string>Form</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="MyComboBox" name="comboBox"/>
</item>
</layout>
</widget>
<customwidgets>
<customwidget>
<class>MyComboBox</class>
<extends>QComboBox</extends>
<header>folder1/mycombobox.h</header>
</customwidget>
</customwidgets>
<resources/>
<connections/>
</ui>
【问题讨论】:
-
如果您需要帮助,请提供minimal reproducible example
-
什么问题?,我在Linux中测试过你的代码,我没有遇到问题
-
该代码在 Linux 下工作,因为对 mycombobox.h Form.ui 的传递是项目根路径的相对传递。在 Windows 下,pass 必须来自 *.ui 文件路径,这样比较好。
-
路径,不是pass,对不起
标签: qt qt5 qt-designer