当前位置: 代码迷 >> QT开发 >> 这段代码到底错在哪里?解决方案
  详细解决方案

这段代码到底错在哪里?解决方案

热度:22   发布时间:2016-04-25 04:48:16.0
这段代码到底错在哪里?
1、colornamesdialog.h文件:
#ifndef COLORNAMESDIALOG_H
#define COLORNAMESDIALOG_H

#include <QtGui/QDialog>

class QStringListModel;
class QSortFilterProxyModel;
class QListView;
class QLabel;
class QLineEdit;
class QComboBox;

class ColorNamesDialog : public QDialog
{
  Q_OBJECT

public:
  ColorNamesDialog(QWidget *parent = 0);
  ~ColorNamesDialog();
private:
  QStringListModel *sourceModel;
  QSortFilterProxyModel *proxyModel;
  QListView *listView;
  QLabel *filterLabel;
  QLineEdit *lineEdit;
  QLabel *patternSyntaxLabel;
  QComboBox *comboBox;
};

#endif // COLORNAMESDIALOG_H
2、colornamesdialog.cpp文件:
#include <QtGui>
#include "colornamesdialog.h"

ColorNamesDialog::ColorNamesDialog(QWidget *parent)
  : QDialog(parent)
{
  sourceModel = new QStringListModel(this);
  sourceModel->setStringList(QColor::colorNames());
  proxyModel = new QSortFilterProxyModel(this);
  proxyModel->setSourceModel(sourceModel);
  proxyModel->setFilterKeyColumn(0);
  listView = new QListView;
  listView->setModel(proxyModel);

  filterLabel = new QLabel(tr("&Filter"));
  lineEdit = new QLineEdit;
  patternSyntaxLabel = new QLabel(tr("&Pattern syntax"));
  comboBox = new QComboBox;
  comboBox->addItem(tr("Regular expressino"), QRegExp::RegExp);
  comboBox->addItem(tr("Wildcard"), QRegExp::Wildcard);
  comboBox->addItem(tr("Fixed string"), QRegExp::FixedString);

  QGridLayout *gridLayout = new QGridLayout;
  gridLayout->addWidget(listView, 0, 0, 1, 2);
  gridLayout->addWidget(filterLabel, 1, 0);
  gridLayout->addWidget(lineEdit, 1, 1);
  gridLayout->addWidget(patternSyntaxLabel, 2, 0);
  gridLayout->addWidget(comboBox, 2, 1);
  setLayout(gridLayout);
}

ColorNamesDialog::~ColorNamesDialog()
{

}
3、main.cpp文件:
#include <QtGui/QApplication>
#include "colornamesdialog.h"

int main(int argc, char *argv[])
{
  QApplication a(argc, argv);
  ColorNamesDialog w;
  w.show();

  return a.exec();
}
以上这段代码,编译的时候出现一些错误。不知是何原因。
c:\QtSDK\Desktop\Qt\4.7.4\mingw\include\QtGui\QtGui:78: In file included from c:\QtSDK\Desktop\Qt\4.7.4\mingw\include\QtGui/QtGui:78,
F:\Qt_Workspace\ColorNamesDialog\colornamesdialog.cpp:1: from colornamesdialog.cpp:1:
c:\QtSDK\Desktop\Qt\4.7.4\mingw\include\QtGui\qtreewidget.h:260: 错误:redefinition of 'class QListView'
c:\QtSDK\Desktop\Qt\4.7.4\mingw\include\QtGui\qlistview.h:58: 错误:previous definition of 'class QListView'
后面还有一堆错误,都是关于QListView的错误。请问这是什么原因呀?实在不解。

------解决方案--------------------
错误提示是QListView被重定义了
------解决方案--------------------
探讨

错误提示是QListView被重定义了

------解决方案--------------------
单看错误提示如楼上所说,但是我把代码拷贝到我机器上编译运行没有问题,我用是的Qt Library 4.7.3,不过感觉这个和类库版本关系也不大吧……o(╯□╰)o
------解决方案--------------------
.pp 没有包含头文件, 只是在.h中声明了 还要包含 要不然编译器肯定不过
------解决方案--------------------
说下 Qt creator 的版本。。。
------解决方案--------------------
  相关解决方案