一些Qt错误 - 如何解决?

时间:2011-04-19 10:04:57

标签: c++ qt

我有以下main.cpp文件:

#include <QApplication>
#include "ui_checkabder.h"
#include <QDialog>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
Ui::CheckAbder ui;
QDialog *dialog = new QDialog;
ui.setupUi(dialog);
dialog->show();
return app.exec();
}

并且,在尝试运行程序时出现以下错误:

C:/Users/avbder/Desktop/abder/main.cpp:7: error: 'CheckAbder' is not a member of 'Ui'

C:/Users/avbder/Desktop/abder/main.cpp:7: error: expected ';' before 'ui'

C:/Users/avbder/Desktop/abder/main.cpp:7: error: expected ';' before 'ui'

C:/Users/avbder/Desktop/abder/main.cpp:9: error: 'ui' was not declared in this scope

ui_checkabder.h的内容如下:

/********************************************************************************
** Form generated from reading UI file 'checkabder.ui'
**
** Created: Mon Apr 18 10:01:09 2011
**      by: Qt User Interface Compiler version 4.7.3
**
** WARNING! All changes made in this file will be lost when recompiling UI file!
********************************************************************************/

#ifndef UI_CHECKABDER_H
#define UI_CHECKABDER_H

#include <QtCore/QVariant>
#include <QtGui/QAction>
#include <QtGui/QApplication>
#include <QtGui/QButtonGroup>
#include <QtGui/QHBoxLayout>
#include <QtGui/QHeaderView>
#include <QtGui/QLabel>
#include <QtGui/QLineEdit>
#include <QtGui/QPushButton>
#include <QtGui/QVBoxLayout>
#include <QtGui/QWidget>

QT_BEGIN_NAMESPACE

class Ui_Form
{
public:
    QVBoxLayout *verticalLayout;
    QHBoxLayout *horizontalLayout;
    QLabel *label;
    QLineEdit *lineEdit;
    QHBoxLayout *horizontalLayout_2;
    QPushButton *okButton;
    QPushButton *cancelButton;

    void setupUi(QWidget *Form)
    {
        if (Form->objectName().isEmpty())
            Form->setObjectName(QString::fromUtf8("Form"));
        Form->resize(400, 300);
        verticalLayout = new QVBoxLayout(Form);
        verticalLayout->setObjectName(QString::fromUtf8("verticalLayout"));
        horizontalLayout = new QHBoxLayout();
        horizontalLayout->setObjectName(QString::fromUtf8("horizontalLayout"));
        label = new QLabel(Form);
        label->setObjectName(QString::fromUtf8("label"));
        QFont font;
        font.setFamily(QString::fromUtf8("Comic Sans MS"));
        font.setPointSize(16);
        label->setFont(font);

        horizontalLayout->addWidget(label);

        lineEdit = new QLineEdit(Form);
        lineEdit->setObjectName(QString::fromUtf8("lineEdit"));

        horizontalLayout->addWidget(lineEdit);


        verticalLayout->addLayout(horizontalLayout);

        horizontalLayout_2 = new QHBoxLayout();
        horizontalLayout_2->setObjectName(QString::fromUtf8("horizontalLayout_2"));
        okButton = new QPushButton(Form);
        okButton->setObjectName(QString::fromUtf8("okButton"));
        okButton->setEnabled(false);
        QFont font1;
        font1.setFamily(QString::fromUtf8("Comic Sans MS"));
        font1.setBold(true);
        font1.setWeight(75);
        okButton->setFont(font1);

        horizontalLayout_2->addWidget(okButton);

        cancelButton = new QPushButton(Form);
        cancelButton->setObjectName(QString::fromUtf8("cancelButton"));
        cancelButton->setFont(font1);

        horizontalLayout_2->addWidget(cancelButton);


        verticalLayout->addLayout(horizontalLayout_2);


        retranslateUi(Form);

        QMetaObject::connectSlotsByName(Form);
    } // setupUi

    void retranslateUi(QWidget *Form)
    {
        Form->setWindowTitle(QApplication::translate("Form", "Form", 0, QApplication::UnicodeUTF8));
        label->setText(QApplication::translate("Form", "Name", 0, QApplication::UnicodeUTF8));
        okButton->setText(QApplication::translate("Form", "OK", 0, QApplication::UnicodeUTF8));
        cancelButton->setText(QApplication::translate("Form", "CANCEL", 0, QApplication::UnicodeUTF8));
    } // retranslateUi

};

namespace Ui {
    class Form: public Ui_Form {};
} // namespace Ui

QT_END_NAMESPACE

#endif // UI_CHECKABDER_H

关于如何解决这些问题的任何想法?

感谢。

3 个答案:

答案 0 :(得分:1)

您的班级名称是Ui_FormUi::Form,而不是CheckAbder。您应该在Designer中重命名它。

答案 1 :(得分:1)

你需要了解一点点。文件名与生成的Ui名称空间内容无关。 .ui文件中的小部件名为“Form”而不是CheckAbder。您可以通过两种方式解决问题:

  1. 在.cpp文件中开始使用Ui :: Form
  2. 在.ui文件中将表单重命名为CheckAbder
  3. 另外请确保您的.ui文件已添加到项目中,因此更改后会自动生成新的ui _ * .h文件.ui

答案 2 :(得分:-4)

namespace Ui {
    class Form: public Ui_Form {};
} // namespace Ui

应该是

namespace Ui {
    class CheckAbder: public Ui_Form {};
} // namespace Ui