0

我有 2 个基本相同的 Qt 程序,我将其中一个的代码复制到另一个以对其进行修改并将其保存为一个新项目,现在第二个没有运行并给出了一堆错误是有道理的,因为另一个运行并且它们在错误出现的地方是相同的。这是代码:

#ifndef FILM_H
#define FILM_H

#include <QWidget>
#include <QString>
#include <QDate>

class Film: public QObject{
Q_OBJECT

Q_PROPERTY( QString title READ getTitle WRITE setTitle);
Q_PROPERTY( int duration READ getDuration WRITE setDuration);
Q_PROPERTY( QString director READ getDirector WRITE setDirector);
Q_PROPERTY( QDate releaseDate READ getReleaseDate WRITE setReleaseDate);

public:
    Film(QString t,int dur,QString dir,QDate r);
    Film();
    void setTitle(QString t);
    void setDuration(int dur);
    void setDirector(QString dir);
    void setReleaseDate(QDate r);
    QString getTitle() const;
    int getDuration() const;
    QString getDirector() const;
    QDate getReleaseDate() const;
    QString toString();

private:
    QString m_title;
    int m_duration;
    QString m_director;
    QDate m_releaseDate;

};

#endif // FILM_H


#ifndef FILMWRITER_H
#define FILMWRITER_H
#include "Film.h"
#include <QtGui>
#include <QFile>

class FilmWriter{

public:
    void accessFilm(Film& f);

};
#endif // FILMWRITER_H


#ifndef FILMINPUT_H
#define FILMINPUT_H

#include <QMainWindow>
#include "Film.h"
#include "FilmWriter.h"
#include <QLabel>
#include <QTextEdit>
#include <QPushButton>

namespace Ui {
    class FilmInput;
}

class FilmInput : public QMainWindow
{
    Q_OBJECT

public:
    explicit FilmInput(QWidget *parent = 0);
    ~FilmInput();
    void obtainFilmData(Film& f);
    void saveFilm(Film& f);
public slots:
    void getFilm();
private:
    Ui::FilmInput *ui;
    //widgets
    QMainWindow* window;
    QLabel* infoLabel;
    QLabel* titleLabel;
    QLabel* durationLabel;
    QLabel* directorLabel;
    QLabel* relDateLabel;
    QTextEdit* titleEdit;
    QTextEdit* durationEdit;
    QTextEdit* directorEdit;
    QTextEdit* relDateEdit;
    QPushButton* saveBtn;
    QPushButton* cancelBtn;
    Film f;
    //sets up gui and connects signals and slots
    void setUpGui();
};

#endif // FILMINPUT_H


#include "Film.h"
#include <QDate>
#include <QString>

Film::Film(QString t,int dur,QString dir,QDate r):m_title(t),m_duration(dur),m_director(dir),m_releaseDate(r){

}

Film::Film(){
}

void Film::setTitle(QString t){
    m_title = t;
}

void Film::setDuration(int dur){
    m_duration = dur;
}

void Film::setDirector(QString dir){
    m_director = dir;
}

void Film::setReleaseDate(QDate r){
    m_releaseDate = r;
}

QString Film::getTitle() const{
    return QString("%1").arg(m_title);
}

int Film::getDuration() const{
    return m_duration;
}
QString Film::getDirector() const{
    return QString("%1").arg(m_director);
}
QDate Film::getReleaseDate() const{
    return m_releaseDate;
}
QString Film::toString()
{
  return m_title + " " + m_duration + " " + m_director + " " + m_releaseDate.toString();
}



#include "FilmWriter.h"
#include <QtGui>
#include <QFileDialog>
#include <QFile>
#include <QMessageBox>
#include <QObject>
#include <QTextStream>

void FilmWriter::accessFilm(Film& f){
    QVariant v1 = f.property("title");
    QVariant v2 = f.property("duration");
    QVariant v3 = f.property("director");
    QVariant v4 = f.property("releaseDate");
    QString str = v1.toString() +" "+ v2.toString() +" "+ v3.toString() +" "+ v4.toString();
    QMessageBox msgBox;
    msgBox.setText(str);
    msgBox.exec();
}



#include "filminput.h"
#include "ui_filminput.h"
#include <QtGui>
#include "Film.h"
#include "FilmWriter.h"
#include <QTextEdit>
#include <QDate>
#include <QString>

FilmInput::FilmInput(QWidget *parent) :
    QMainWindow(parent),
   ui(new Ui::FilmInput)
{
    ui->setupUi(this);
    setUpGui();
}

FilmInput::~FilmInput()
{
    delete ui;
}

void FilmInput::setUpGui(){
    //initialise widgets
    infoLabel = new QLabel("Please enter film data which will be saved to a file",this);
    titleLabel = new QLabel("Film Title",this);
    durationLabel = new QLabel("Film Duration",this);
    directorLabel = new QLabel("Film Director",this);
    relDateLabel = new QLabel("Film Release Date",this);
    titleEdit = new QTextEdit(this);
    durationEdit = new QTextEdit(this);
    directorEdit = new QTextEdit(this);
    relDateEdit = new QTextEdit(this);
    saveBtn = new QPushButton("Save Film",this);
    cancelBtn = new QPushButton("Cancel",this);
    //set layout
    QFormLayout* layout = new QFormLayout();
    layout->addWidget(infoLabel);
    layout->addWidget(titleLabel);
    layout->addWidget(titleEdit);
    layout->addWidget(durationLabel);
    layout->addWidget(durationEdit);
    layout->addWidget(directorLabel);
    layout->addWidget(directorEdit);
    layout->addWidget(relDateLabel);
    layout->addWidget(relDateEdit);
    layout->addWidget(saveBtn);
    layout->addWidget(cancelBtn);

    this->ui->widget->setLayout(layout);
    this->setWindowTitle("Film Archive");
    connect(saveBtn,SIGNAL(clicked()),this, SLOT(getFilm()));
    connect(cancelBtn,SIGNAL(clicked()),this,SLOT(close()));
}

void FilmInput::getFilm(){
    Film f1(titleEdit->toPlainText(),durationEdit->toPlainText().toInt() ,directorEdit->toPlainText(),
             QDate::fromString(relDateEdit->toPlainText(),"dd/MM/YYYY"));;
    obtainFilmData(f1);
}

void FilmInput::obtainFilmData(Film &f){
    FilmWriter f2;
    f2.accessFilm(f);
}




#include <QtGui/QApplication>
#include "filminput.h"

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

    return a.exec();
}

错误转储:

..\Ass1Q2\filminput.cpp: In constructor 'FilmInput::FilmInput(QWidget*)': 
..\Ass1Q2\filminput.cpp:12: error: invalid use of incomplete type 'struct Ui::FilmInput' 
..\Ass1Q2\/filminput.h:12: error: forward declaration of 'struct Ui::FilmInput' 
..\Ass1Q2\filminput.cpp:14: error: invalid use of incomplete type 'struct Ui::FilmInput' 
..\Ass1Q2\/filminput.h:12: error: forward declaration of 'struct Ui::FilmInput' 
..\Ass1Q2\filminput.cpp: In destructor 'virtual FilmInput::~FilmInput()': 
..\Ass1Q2\filminput.cpp:20: warning: possible problem detected in invocation of delete operator: 
..\Ass1Q2\filminput.cpp:20: warning: invalid use of incomplete type 'struct Ui::FilmInput' 
..\Ass1Q2\/filminput.h:12: warning: forward declaration of 'struct Ui::FilmInput' 
..\Ass1Q2\filminput.cpp:20: note: neither the destructor nor the class-specific operator delete will be called, even if they are declared when the class is defined. 
..\Ass1Q2\filminput.cpp: In member function 'void FilmInput::setUpGui()': 
..\Ass1Q2\filminput.cpp:50: error: invalid use of incomplete type 'struct Ui::FilmInput' 
..\Ass1Q2\/filminput.h:12: error: forward declaration of 'struct Ui::FilmInput' 
mingw32-make[1]: Leaving directory `C:/Unisa/COS3711/assignments/Ass1Q2-build-desktop' 
mingw32-make: Leaving directory `C:/Unisa/COS3711/assignments/Ass1Q2-build-desktop' 
mingw32-make[1]: *** [debug/filminput.o] Error 1 
mingw32-make: *** [debug] Error 2 
The process "C:/Qt/2010.04/mingw/bin/mingw32-make.exe" exited with code %2.
Error while building project Ass1Q2 (target: Desktop)
When executing build step 'Make' 
4

1 回答 1

0

您复制了代码,但没有复制 pro 文件。因此,您可能会错过将 UI 文件添加到配置文件,以调用 UI 编译器来生成“ui_FileInput.h”源。请复制 UI 文件并使用 FORMS += FileInput.ui 更新 .pro 文件可能会解决您的问题。要更好地了解,您可以评论“ui_FileInput.h”并查看行为。

于 2013-08-15T02:04:54.897 回答