【问题标题】:Qt: Qt Designer button panel errorQt:Qt Designer 按钮面板错误
【发布时间】:2012-10-30 06:27:02
【问题描述】:

我创建了一个名为“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

【问题讨论】:

    标签: qt


    【解决方案1】:

    您是否创建了继承自 QDialog 的新 Dialog 类?一般来说,如果你使用 QDialog ,你不能连接到按钮槽,但必须连接到 MainWindow 的任何槽的accepted() 或 denied() 信号。

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

    注意:this是主窗口指针

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-09-28
      • 2013-05-15
      • 2011-01-11
      • 1970-01-01
      • 2012-10-05
      • 2015-11-10
      • 2021-09-10
      • 2020-06-19
      相关资源
      最近更新 更多