0

OK/キャンセルボタンを備えた「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
4

1 に答える 1

0

QDialog から継承する新しい Dialog クラスを作成しましたか?? 通常、 QDialog を使用している場合、ボタン スロットに接続することはできませんが、MainWindow のいずれかのスロットにaccepted() または reject() シグナルに接続する必要があります。

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 に答える