QT 创建者 - "QGraphicsScene" 不命名类型
QT creator - 'QGraphicsScene' does not name a type
我是QT新手,尝试自学。
根据示例/教程,我得到了这段代码。
目的是创建一个允许登录的简单界面。
My error:
mainwindow.h:22: error: 'QGraphicsScene' does not name a type
QGraphicsScene *scene;
我没有找到一个解决方案,但只提到从UI中删除对象,并在实例化QGraphicsScene
时使用this
而不是ui->graphicsView
。
我错过了什么?
头/mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QtCore>
#include <QtGui>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
Ui::MainWindow *ui;
QGraphicsScene *scene;
};
#endif // MAINWINDOW_H
来源/mainwindow.cpp
#include "mainwindow.h"
//#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
scene = new QGraphicsScene(ui->graphicsView);
scene->setSceneRect(ui->graphicsView->rect());
ui->graphicsView->setScene(scene);
ui->graphicsView->setFixedSize(400,400);
QPixmap pixmap("res/logo/logo-black.png");
scene->addPixmap(pixmap);
ui->graphicsView->show();
}
MainWindow::~MainWindow()
{
delete ui;
}
来源/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.ui <?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>283</width>
<height>415</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralWidget">
<widget class="QLineEdit" name="lineEdit">
<property name="geometry">
<rect>
<x>120</x>
<y>130</y>
<width>141</width>
<height>22</height>
</rect>
</property>
</widget>
<widget class="QLineEdit" name="lineEdit_2">
<property name="geometry">
<rect>
<x>120</x>
<y>170</y>
<width>141</width>
<height>22</height>
</rect>
</property>
</widget>
<widget class="QPushButton" name="pushButton">
<property name="geometry">
<rect>
<x>90</x>
<y>250</y>
<width>91</width>
<height>31</height>
</rect>
</property>
<property name="text">
<string>Login</string>
</property>
</widget>
<widget class="QCheckBox" name="checkBox">
<property name="geometry">
<rect>
<x>90</x>
<y>220</y>
<width>101</width>
<height>20</height>
</rect>
</property>
<property name="text">
<string>Remeber Me</string>
</property>
</widget>
<widget class="QLabel" name="label">
<property name="geometry">
<rect>
<x>20</x>
<y>130</y>
<width>59</width>
<height>14</height>
</rect>
</property>
<property name="text">
<string>Email</string>
</property>
</widget>
<widget class="QLabel" name="label_2">
<property name="geometry">
<rect>
<x>20</x>
<y>170</y>
<width>59</width>
<height>14</height>
</rect>
</property>
<property name="text">
<string>Password</string>
</property>
</widget>
<widget class="QPushButton" name="pushButton_2">
<property name="geometry">
<rect>
<x>96</x>
<y>310</y>
<width>80</width>
<height>22</height>
</rect>
</property>
<property name="text">
<string>Register</string>
</property>
</widget>
<widget class="QGraphicsView" name="graphicsView">
<property name="geometry">
<rect>
<x>80</x>
<y>0</y>
<width>121</width>
<height>111</height>
</rect>
</property>
<property name="autoFillBackground">
<bool>false</bool>
</property>
</widget>
</widget>
<widget class="QMenuBar" name="menuBar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>283</width>
<height>19</height>
</rect>
</property>
</widget>
<widget class="QToolBar" name="mainToolBar">
<attribute name="toolBarArea">
<enum>TopToolBarArea</enum>
</attribute>
<attribute name="toolBarBreak">
<bool>false</bool>
</attribute>
</widget>
<widget class="QStatusBar" name="statusBar"/>
</widget>
<layoutdefault spacing="6" margin="11"/>
<resources/>
<connections/>
</ui>
- 取消:
#include "ui_mainwindow.h"
inSources/mainwindow.cpp
原因我没有在我的项目目录中看到ui_mainwindow.h
,因此认为这是一个不必要的包含
- 添加
#include <QGraphicsScene>
inHeaders/mainwindow.h
从文档页找到-更多的是"如果我添加这个会发生什么"的尝试,有<QtGui>
包含<QGraphicsScene>
的想法
谢谢
必须包含以下内容:
#include <QGraphicsScene>
您包含了不必要的头,而没有包含必要的头。要使用widgets模块,您只需要执行#include <QtWidgets>
。mainwindow.h
文件应该如下所示启动:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QtWidgets>
namespace Ui {
...
相关文章:
- ArduinoJson 6.15.2:JsonObject没有命名类型
- 防止主数据类型C++的隐式转换
- 大量序列中核苷酸类型的快速计数
- 如何从C++中的依赖类型中获得它所依赖的类型
- 有关插入适配器的错误。[错误]请求从 'back_insert_iterator<vector<>>' 类型转换为非标量类型
- 是否可以初始化不可复制类型的成员变量(或基类)
- 如何获取std::result_of函数的返回类型
- 从父命名空间重载类型
- 如果C++类在类方法中具有动态分配,但没有构造函数/析构函数或任何非静态成员,那么它仍然是POD类型吗
- 我想将一个对T类型的非常量左值引用绑定到一个T类型的临时值
- Openssl 1.1.1d无效使用不完整的类型"struct dsa_st"
- 访问者访问变体并返回不同类型时出错
- 在VS2010-VS2015下编译时,如何使用decltype作为较大类型表达式的LHS
- 处理小于cpu数据总线的数据类型.(c++转换为机器代码)
- C++ 雷神库 - 使用资源加载器类时出现问题(不命名类型)
- 模板元程序查找相似的连续类型名称
- 是否可以从int转换为enum类类型
- 构造函数正在调用一个使用当前类类型的函数
- 我应该使用什么来代替void作为变体中的替代类型之一
- 类中的字符串不命名类型