Qt:Qt Designer按钮面板错误

时间:2012-10-30 06:27:03

标签: qt

我用ok / cancel按钮创建了一个名为'new_dialog'的模态对话框。现在我想在OK按钮的插槽上工作。我右键单击按钮窗格,从上下文菜单中选择“转到插槽”命令;并得到一个错误:

  

无法在mainwindow.cpp中找到具有Ui :: new_dialog的类。校验   包含指令   (这是一个英文翻译)

如何为按钮分配插槽?

谢谢!

一些代码:

mainwindow.cpp中有new.ui和ui_new.h,指令:

#include "ui_new.h"

从主窗口调用该对话框:

void MainWindow::on_pushButton_clicked()
{
    QDialog *new_dialog = new QDialog(0,0);
    Ui_New newUi;
    newUi.setupUi(new_dialog);
    new_dialog->exec();
}

ui_new.h:

#ifndef UI_NEW_H
#define UI_NEW_H

#include <QtCore/QVariant>
#include <QtGui/QAction>
#include <QtGui/QApplication>
#include <QtGui/QButtonGroup>
#include <QtGui/QDialog>
#include <QtGui/QDialogButtonBox>
#include <QtGui/QHeaderView>
#include <QtGui/QLabel>
#include <QtGui/QLineEdit>

QT_BEGIN_NAMESPACE

class Ui_New
{
public:
    QDialogButtonBox *buttonBox;
    QLabel *label;
    QLabel *label_2;
    QLineEdit *lineEdit;
    QLineEdit *lineEdit_2;

    void setupUi(QDialog *new_dialog)
    {
        if (new_dialog->objectName().isEmpty())
            new_dialog->setObjectName(QString::fromUtf8("Dialog"));
        new_dialog->setWindowModality(Qt::ApplicationModal);
        new_dialog->resize(250, 180);
        new_dialog->setModal(false);
        buttonBox = new QDialogButtonBox(new_dialog);
        buttonBox->setObjectName(QString::fromUtf8("buttonBox"));
        buttonBox->setGeometry(QRect(40, 140, 181, 32));
        buttonBox->setOrientation(Qt::Horizontal);
        buttonBox->setStandardButtons(QDialogButtonBox::Cancel|QDialogButtonBox::Ok);
        label = new QLabel(new_dialog);
        label->setObjectName(QString::fromUtf8("label"));
        label->setGeometry(QRect(20, 40, 46, 13));
        label_2 = new QLabel(new_dialog);
        label_2->setObjectName(QString::fromUtf8("label_2"));
        label_2->setGeometry(QRect(20, 70, 46, 13));
        lineEdit = new QLineEdit(new_dialog);
        lineEdit->setObjectName(QString::fromUtf8("lineEdit"));
        lineEdit->setGeometry(QRect(70, 40, 113, 20));
        lineEdit_2 = new QLineEdit(new_dialog);
        lineEdit_2->setObjectName(QString::fromUtf8("lineEdit_2"));
        lineEdit_2->setGeometry(QRect(70, 70, 113, 20));

        retranslateUi(new_dialog);
        QObject::connect(buttonBox, SIGNAL(accepted()), new_dialog, SLOT(accept()));
        QObject::connect(buttonBox, SIGNAL(rejected()), new_dialog, SLOT(reject()));

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

    void retranslateUi(QDialog *new_dialog)
    {
        new_dialog->setWindowTitle(QApplication::translate("Dialog", "New person", 0, QApplication::UnicodeUTF8));
        label->setText(QApplication::translate("Dialog", "Name", 0, QApplication::UnicodeUTF8));
        label_2->setText(QApplication::translate("Dialog", "Surname", 0, QApplication::UnicodeUTF8));
    } // retranslateUi

};

namespace Ui {
    class new_dialog: public Ui_New {};
} // namespace Ui

QT_END_NAMESPACE

#endif // UI_NEW_H

1 个答案:

答案 0 :(得分:0)

您是否创建了继承自QDialog的新Dialog类?通常,如果您使用QDialog,则无法连接到按钮插槽,但必须连接到接受()或拒绝()信号到MainWindow的任何插槽。

QDialog *new_dialog = new QDialog(0,0);
connect(new_dialog,SIGNAL(accepted()),this,SLOT(MySlot()));

注意:是主窗口指针

如果你创建了自己的Dialog类并且有指向按钮的指针,首先将Button clicked()连接到对话框插槽,然后从那里重定向到MainWindow。