0

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

在 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
4

1 回答 1

0

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

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

注意:是主窗口指针

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

于 2012-10-30T06:50:19.110 回答