Qt 禁止声明没有类型的 QListView

发布于 2024-12-05 05:53:27 字数 4160 浏览 0 评论 0原文

我的 Qt 项目中有一个非常奇怪的错误。这是代码, main_window.h:

#include <QtGui>
#include <QtSql>

class main_window : public QTabWidget
{
    Q_OBJECT

    /// @name List Widgets
private:
    QListWidget*    m_documents_list;
....

这是 main_window.cpp:

...
void main_window::create_documents_widget()
{
    m_documents = new QWidget(this);
    m_documents_list = new QListWidget(m_documents);
}
...

我无法理解的问题是在 QListView 中,我没有在我的项目中使用它。只有 QListWidget,但当我尝试构建项目时,会发生以下错误:

qlistview.h:194: error: ISO C++ forbids statements of 'QListView' with no type< /em>

qlistview.h:194: 错误:在“&”之前应有“,”或“...” token

还有以下奇怪的错误:

qlistwidget.h:308: error: no 'void QListWidget::removeItemWidget(QListWidgetItem) ' 在类 'QListWidget' 中声明的成员函数*

qlistwidget.h:311: 错误:无 'void QListWidget::addItem(QListWidgetItem)' 在类 'QListWidget' 中声明的成员函数*

qlistwidget.h:314: 错误:没有 'QListWidgetItem< /strong> QListWidget::itemAt(int, int) const' 成员函数在类“QListWidget”中声明*

等。

提前致谢。

UPD:我在 Windows 7 上使用 QtCreator 2.2.1。

UPD2:Qt 版本是 4.7.1。

UPD3:完整的输出

In file included from c:\QtSDK\Desktop\Qt\4.7.3\mingw\include\QtGui/QtGui:68,
                 from ..\my_project\/main_window.h:4,
                 from ..\my_project\main.cpp:2:
c:\QtSDK\Desktop\Qt\4.7.3\mingw\include\QtGui/qlistview.h:194: error: ISO C++ forbids declaration of 'QListView' with no type
c:\QtSDK\Desktop\Qt\4.7.3\mingw\include\QtGui/qlistview.h:194: error: expected ',' or '...' before '&' token
c:\QtSDK\Desktop\Qt\4.7.3\mingw\include\QtGui/qlistview.h:194: error: ISO C++ forbids declaration of 'QListView' with no type
c:\QtSDK\Desktop\Qt\4.7.3\mingw\include\QtGui/qlistview.h:194: error: ISO C++ forbids declaration of 'QListView' with no type
c:\QtSDK\Desktop\Qt\4.7.3\mingw\include\QtGui/qlistview.h:194: error: expected ';' before '&' token
In file included from c:\QtSDK\Desktop\Qt\4.7.3\mingw\include\QtGui/QtGui:69,
                 from ..\my_project\/main_window.h:4,
                 from ..\my_project\main.cpp:2:
c:\QtSDK\Desktop\Qt\4.7.3\mingw\include\QtGui/qlistwidget.h:202: error: redefinition of 'class QListWidget'
c:\QtSDK\Desktop\Qt\4.7.3\mingw\include\QtGui/qlistview.h:58: error: previous definition of 'class QListWidget'
c:\QtSDK\Desktop\Qt\4.7.3\mingw\include\QtGui/qlistwidget.h:308: error: no 'void QListWidget::removeItemWidget(QListWidgetItem*)' member function declared in class 'QListWidget'
c:\QtSDK\Desktop\Qt\4.7.3\mingw\include\QtGui/qlistwidget.h:311: error: no 'void QListWidget::addItem(QListWidgetItem*)' member function declared in class 'QListWidget'
c:\QtSDK\Desktop\Qt\4.7.3\mingw\include\QtGui/qlistwidget.h:314: error: no 'QListWidgetItem* QListWidget::itemAt(int, int) const' member function declared in class 'QListWidget'
c:\QtSDK\Desktop\Qt\4.7.3\mingw\include\QtGui/qlistwidget.h: In member function 'void QListWidgetItem::setSelected(bool)':
c:\QtSDK\Desktop\Qt\4.7.3\mingw\include\QtGui/qlistwidget.h:318: error: 'class QListWidget' has no member named 'setItemSelected'
c:\QtSDK\Desktop\Qt\4.7.3\mingw\include\QtGui/qlistwidget.h: In member function 'bool QListWidgetItem::isSelected() const':
c:\QtSDK\Desktop\Qt\4.7.3\mingw\include\QtGui/qlistwidget.h:321: error: 'class QListWidget' has no member named 'isItemSelected'
c:\QtSDK\Desktop\Qt\4.7.3\mingw\include\QtGui/qlistwidget.h: In member function 'void QListWidgetItem::setHidden(bool)':
c:\QtSDK\Desktop\Qt\4.7.3\mingw\include\QtGui/qlistwidget.h:324: error: 'class QListWidget' has no member named 'setItemHidden'
c:\QtSDK\Desktop\Qt\4.7.3\mingw\include\QtGui/qlistwidget.h: In member function 'bool QListWidgetItem::isHidden() const':
c:\QtSDK\Desktop\Qt\4.7.3\mingw\include\QtGui/qlistwidget.h:327: error: 'class QListWidget' has no member named 'isItemHidden'

I have a very strange error in my Qt project. Here is the code, the main_window.h:

#include <QtGui>
#include <QtSql>

class main_window : public QTabWidget
{
    Q_OBJECT

    /// @name List Widgets
private:
    QListWidget*    m_documents_list;
....

and here is main_window.cpp:

...
void main_window::create_documents_widget()
{
    m_documents = new QWidget(this);
    m_documents_list = new QListWidget(m_documents);
}
...

The problem that I can't understand is in QListView, I'm not using it in my project. There is only QListWidget, but when I'm trying to build the project the following errors occur:

qlistview.h:194: error: ISO C++ forbids declaration of 'QListView' with no type

qlistview.h:194: error: expected ',' or '...' before '&' token

Also the following strange errors:

qlistwidget.h:308: error: no 'void QListWidget::removeItemWidget(QListWidgetItem)' member function declared in class 'QListWidget'*

qlistwidget.h:311: error: no 'void QListWidget::addItem(QListWidgetItem)' member function declared in class 'QListWidget'*

qlistwidget.h:314: error: no 'QListWidgetItem QListWidget::itemAt(int, int) const' member function declared in class 'QListWidget'*

etc.

Thanks in advance.

UPD: I'm using QtCreator 2.2.1 on Windows 7.

UPD2: Qt version is 4.7.1.

UPD3: The complete output

In file included from c:\QtSDK\Desktop\Qt\4.7.3\mingw\include\QtGui/QtGui:68,
                 from ..\my_project\/main_window.h:4,
                 from ..\my_project\main.cpp:2:
c:\QtSDK\Desktop\Qt\4.7.3\mingw\include\QtGui/qlistview.h:194: error: ISO C++ forbids declaration of 'QListView' with no type
c:\QtSDK\Desktop\Qt\4.7.3\mingw\include\QtGui/qlistview.h:194: error: expected ',' or '...' before '&' token
c:\QtSDK\Desktop\Qt\4.7.3\mingw\include\QtGui/qlistview.h:194: error: ISO C++ forbids declaration of 'QListView' with no type
c:\QtSDK\Desktop\Qt\4.7.3\mingw\include\QtGui/qlistview.h:194: error: ISO C++ forbids declaration of 'QListView' with no type
c:\QtSDK\Desktop\Qt\4.7.3\mingw\include\QtGui/qlistview.h:194: error: expected ';' before '&' token
In file included from c:\QtSDK\Desktop\Qt\4.7.3\mingw\include\QtGui/QtGui:69,
                 from ..\my_project\/main_window.h:4,
                 from ..\my_project\main.cpp:2:
c:\QtSDK\Desktop\Qt\4.7.3\mingw\include\QtGui/qlistwidget.h:202: error: redefinition of 'class QListWidget'
c:\QtSDK\Desktop\Qt\4.7.3\mingw\include\QtGui/qlistview.h:58: error: previous definition of 'class QListWidget'
c:\QtSDK\Desktop\Qt\4.7.3\mingw\include\QtGui/qlistwidget.h:308: error: no 'void QListWidget::removeItemWidget(QListWidgetItem*)' member function declared in class 'QListWidget'
c:\QtSDK\Desktop\Qt\4.7.3\mingw\include\QtGui/qlistwidget.h:311: error: no 'void QListWidget::addItem(QListWidgetItem*)' member function declared in class 'QListWidget'
c:\QtSDK\Desktop\Qt\4.7.3\mingw\include\QtGui/qlistwidget.h:314: error: no 'QListWidgetItem* QListWidget::itemAt(int, int) const' member function declared in class 'QListWidget'
c:\QtSDK\Desktop\Qt\4.7.3\mingw\include\QtGui/qlistwidget.h: In member function 'void QListWidgetItem::setSelected(bool)':
c:\QtSDK\Desktop\Qt\4.7.3\mingw\include\QtGui/qlistwidget.h:318: error: 'class QListWidget' has no member named 'setItemSelected'
c:\QtSDK\Desktop\Qt\4.7.3\mingw\include\QtGui/qlistwidget.h: In member function 'bool QListWidgetItem::isSelected() const':
c:\QtSDK\Desktop\Qt\4.7.3\mingw\include\QtGui/qlistwidget.h:321: error: 'class QListWidget' has no member named 'isItemSelected'
c:\QtSDK\Desktop\Qt\4.7.3\mingw\include\QtGui/qlistwidget.h: In member function 'void QListWidgetItem::setHidden(bool)':
c:\QtSDK\Desktop\Qt\4.7.3\mingw\include\QtGui/qlistwidget.h:324: error: 'class QListWidget' has no member named 'setItemHidden'
c:\QtSDK\Desktop\Qt\4.7.3\mingw\include\QtGui/qlistwidget.h: In member function 'bool QListWidgetItem::isHidden() const':
c:\QtSDK\Desktop\Qt\4.7.3\mingw\include\QtGui/qlistwidget.h:327: error: 'class QListWidget' has no member named 'isItemHidden'

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

呆° 2024-12-12 05:53:27

首先 - 你还应该提到 qt 版本,因为在这种情况下这是最重要的。

这似乎是编译器或 qt 的一些奇怪的怪癖 - 我的建议是首先在出现问题的地方创建最简单的程序。如果它也在程序中显示,

#include <QtGui/QListWidget>
int main(int argc, char* argv[]){
    QListWidget* w = 0;
}

那么它是编译器的 qt 标头存在问题 - 那么无法从提供的数据中提供答案。如果它有效,那么尝试慢慢地将代码的其他元素添加到这个简单的文件中 - 最有可能在某个时候您会再次遇到相同的错误 - 然后您就会知道最后添加的代码段是有罪的。一旦发现问题,可能需要一些额外的思考来找出如何解决问题。

Firstly - you should also mention qt version, as in this case that's most important.

This seems like some weird quirk of compiler or qt - my recomendation would be first to create simplest program where problem occurs. If it shows also in program like

#include <QtGui/QListWidget>
int main(int argc, char* argv[]){
    QListWidget* w = 0;
}

then it is some problem with qt headers of compiler - then answer cannot be provided from data provided. If it works, then try to slowly add other elements of your code to this simple file - most likely at some point you will get the same error again - then you will know that last added piece of code is guilty. Some additional thinking might be required to figure out how to remove problem once located.

绮烟 2024-12-12 05:53:27

需要注意的是,QListWidget继承自QListView,因此您是间接使用它。

您收到的错误看起来像是您只是在头文件中缺少 #include 行。

另外,也可能是您的头文件中缺少 #include 保护。

#ifndef MYCLASS
#define MYCLASS

class MyClass { ... };

#endif

如果您的头文件被多个项目 #included,那么这将解释您所看到的错误。

It should be noted that QListWidget inherits from QListView, so you are indirectly using it.

The error your getting looks like you're simply missing a #include <QListWidget> line in your header file.

Also, it could be that that you're missing #include guards in your header file

#ifndef MYCLASS
#define MYCLASS

class MyClass { ... };

#endif

If your header file is #included by more than one project then that would explain the errors you're seeing.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文