qt4设计师添加自定义类

时间:2013-12-24 15:21:33

标签: c++ qt4 qt4.8

您好我在qt4设计师中制作了一个gui,并希望添加自定义类的自定义插槽。 它项目编译很好没有错误但我的自定义功能不会工作我做错了什么?我将向你展示为我设计的头文件qt4设计器,并显示你的自定义文件以及main.cpp ..首先是main.cpp

我修改了我的代码,这里是我现在已经添加了一个名为sweetest.cpp的文件并编辑了sweetest.h这里是我的新文件和我收到的错误..

首先是main.cpp

#include "ui_sweetguiform.h"

 int main(int argc, char *argv[])
 {
     QApplication app(argc, argv);
     QWidget *widget = new QWidget;
     Ui::SweetGuiForm ui;
     ui.setupUi(widget);

     widget->show();
     return app.exec();
 }

现在我的自定义头文件sweetest.cpp

#include "sweetest.h"
// trying to include the .moc file wouldnt work at all.

现在带有我的代码的sweettest.h文件

#include "ui_sweetguiform.h"

class SweetGuiForm : public QWidget
{
    Q_OBJECT
public:
    SweetGuiForm( ): ui( new Ui::SweetGuiForm )
    {
        ui->setupUi( this );
    connect(ui->buttonBox, SIGNAL(accepted()), this, SLOT(on_buttonBox_accepted()));
    }

public slots:
    void on_buttonBox_accepted()
    {
         ui.textEdit->setText(QString::number(23));

    }

protected:
    Ui::SweetGuiForm* ui;
};

这是我收到的编译错误..我真的被困了

In file included from sweetest.cpp:1:
sweetest.h: In member function ‘void SweetGuiForm::on_buttonBox_accepted()’:
sweetest.h:16: error: request for member ‘textEdit’ in ‘((SweetGuiForm*)this)->SweetGuiForm::ui’, which is of non-class type ‘Ui::SweetGuiForm*’
make: *** [sweetest.o] Error 1

我想我越来越近了

2 个答案:

答案 0 :(得分:1)

信号和插槽的工作方式是必须将信号连接到插槽。在您的代码中,最简单的方法是在SweetGuiForm的构造函数中。你需要添加:

SweetGuiForm() : ui(new Ui::SweetGuiForm) {
  ui->setupUi(this);
  connect(ui->buttonBox, SIGNAL(accepted()), this, SLOT(on_buttonBox_accepted()));
}

当buttonBox发出其接受的信号时,将调用与其连接的所有插槽。

更新1

在进一步检查代码时,您还缺少Qt MOC(元对象编译器)系统(http://qt-project.org/doc/qt-4.8/moc.html)使用的Qt宏:

class SweetGuiForm : public QWidget
{
  Q_OBJECT
  public:
  ...
};

您还必须通过MOC工具推送代码。它将生成需要包含在源中的源文件。我记得,你必须在cpp文件中包含它;包含在标题中是有问题的。以下就足够了:

sweetguiform.cpp:

#include "suiteguiform.h"
#include "sweetguiform.moc"

更新2

进一步思考后,当您使用特殊名称(例如on_buttonBox_accepted)命名插槽时,我忘记了自动信号/插槽连接功能。这里有一篇博文:http://qtway.blogspot.com/2010/08/automatic-connections-using-qt-signals.html。我自己没有使用它,所以我不能评论它在使用ui成员变量时的工作能力,尽管我怀疑它在这种安排中不起作用。无论如何,您仍然需要Q_OBJECT宏和MOC。

答案 1 :(得分:0)

好的家伙我想通了,并认为ide分享我发现的东西。 首先,文档在qt4中使用它非常好。

我发现你可以使用qt4设计器生成听众文件,首先我用自定义插槽编译并生成ui_sweetgui2.h,然后我可以打开第一次编译生成的sweetgui2.h文件我删除了什么qt4放在那里,把我的自定义插槽放在那个阶段。我坚持了好几个小时......天。 所以这里是最简单的应用程序,但它让我开始,所以这里的文件和代码对我有用,文档基本上让我点击进行了什么。

<强>的main.cpp 文档中的海峡只更改了类名“SweetGuiForm”。

 #include <QApplication>
 #include "sweetgui2.h"
 int main(int argc, char *argv[])
 {
     QApplication app(argc, argv);
     SweetGuiForm sweetgui;
     sweetgui.show();
     return app.exec();
 } 

下一步 sweetgui2.cpp 我第一次尝试c ++ ..丑陋而且有效。但是我再次找到了关于从textEdit获取文本的所有内容,并在计算器示例中键入将其转换为int并在qt4助手中搜索toPlainText()。我在下面的通知中包括我将在我的解释中定义新插槽的文件。希望我有意义。

#include <QtGui>
#include "sweetgui2.h"
    SweetGuiForm::SweetGuiForm(QWidget *parent)
: QWidget(parent)
{
    ui.setupUi(this);
}

void SweetGuiForm::on_buttonBox_accepted()
{
    QString stringamount = ui.textEdit->toPlainText();
    int digitamount = stringamount.toInt();
    ui.textEdit->setText(QString::number(25 + digitamount));
}

下一步 sweetgui2.h 我们上面包含的那个我的自定义标题文件我的自定义插槽....简单,就像我从计算器示例中说的那样扭曲了一个小...你会得到它当你在第一次编译时从设计器生成它时它不是它看起来像我删除了几乎所有的东西并打开计算器示例并在教程中跟随它向你展示如何制作你的第一个自定义插槽。

#ifndef SWEETGUI2_H
#define SWEETGUI2_H
#include "ui_sweetgui2.h"
class SweetGuiForm : public QWidget
{
    Q_OBJECT
public:
SweetGuiForm(QWidget *parent = 0);
private slots:
    void on_buttonBox_accepted();
private:
Ui::SweetGuiForm ui;
};
#endif // SWEETGUI2_H

再次直接来自文档。我使用计算器示例来获得基本流程。

下一步 ui_sweetgui2.h 我包含这个文件是因为我试图将我的插槽添加到由qt4 desinger生成的sweetgui2.h中。不工作的人..所以我首先编译你用设计师生成的sweetgui2.h文件,我去表单菜单然后查看代码,你可以保存头文件。然后当然保存ui文件。 并编译然后你最终得到ui_sweetgui2.h文件,看起来像设计师生成的sweetgui2.h

#ifndef UI_SWEETGUI2_H
#define UI_SWEETGUI2_H

#include <QtCore/QVariant>
#include <QtGui/QAction>
#include <QtGui/QApplication>
#include <QtGui/QButtonGroup>
#include <QtGui/QDialogButtonBox>
#include <QtGui/QHeaderView>
#include <QtGui/QTextEdit>
#include <QtGui/QWidget>

QT_BEGIN_NAMESPACE

class Ui_SweetGuiForm
{
public:
    QDialogButtonBox *buttonBox;
    QTextEdit *textEdit;

    void setupUi(QWidget *SweetGuiForm)
    {
        if (SweetGuiForm->objectName().isEmpty())
            SweetGuiForm->setObjectName(QString::fromUtf8("SweetGuiForm"));
        SweetGuiForm->resize(486, 238);
        buttonBox = new QDialogButtonBox(SweetGuiForm);
        buttonBox->setObjectName(QString::fromUtf8("buttonBox"));
        buttonBox->setGeometry(QRect(150, 200, 181, 26));
        buttonBox->setStandardButtons(QDialogButtonBox::Cancel|QDialogButtonBox::Ok);
        textEdit = new QTextEdit(SweetGuiForm);
        textEdit->setObjectName(QString::fromUtf8("textEdit"));
        textEdit->setGeometry(QRect(150, 50, 221, 91));

        retranslateUi(SweetGuiForm);
        QObject::connect(buttonBox, SIGNAL(rejected()), SweetGuiForm, SLOT(close()));

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

    void retranslateUi(QWidget *SweetGuiForm)
    {
        SweetGuiForm->setWindowTitle(QApplication::translate("SweetGuiForm", "SweetGuiBack", 0, QApplication::UnicodeUTF8));
    } // retranslateUi

};

namespace Ui {
    class SweetGuiForm: public Ui_SweetGuiForm {};
} // namespace Ui

QT_END_NAMESPACE

#endif // UI_SWEETGUI2_H 

然后我再次使用自定义插槽和shazam重新编译!现在我可以开始学习一些c ++了。 感谢所有提示人员,你们之间以及我到那里的文档。 希望这可以帮助。要看的主要是包含的东西,我的意思是我的 sweetgui2.cpp 文件 包括 sweetgui2.h 文件。我抓住了我所有的习惯。

我的 sweetgui2.h 文件 包括 ui_sweetgui2.h ,它包含设计师在第一次编译时所做的所有事情。 Main.cpp调用我的SweetGuiForm类。

你们都可以看到我用c ++开始的前几天,但这是一个很好的起点。它让我把基本的流程放在了我的脑海里。 qt4助手看看它。。它解释得很好,而且这些例子看起来非常好。 ho ho ho merry xmas。希望我的解释有所帮助。