首页 文章

Qt禁止声明没有类型的QListView

提问于
浏览
1

我的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 declaration of 'QListView' with no type

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

还有以下奇怪的错误:

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'*

等等

提前致谢 .

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'

2 回答

  • 2

    应该注意的是,QListWidget继承自QListView,因此您间接使用它 .

    您得到的错误看起来只是在头文件中缺少 #include <QListWidget> 行 .

    此外,可能是您在头文件中缺少#include警卫

    #ifndef MYCLASS
    #define MYCLASS
    
    class MyClass { ... };
    
    #endif
    

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

  • 2

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

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

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

    然后它是编译器的qt头的一些问题 - 然后无法从提供的数据提供答案 . 如果它有效,那么尝试慢慢地将代码的其他元素添加到这个简单的文件中 - 很可能在某些时候你会再次得到同样的错误 - 那么你就会知道最后添加的代码是有罪的 . 可能需要一些额外的想法来弄清楚如何找到问题 .

相关问题