我有一个如下的Qdialog:
和另一个当我单击“确定”而没有选择图像和分类器文件时触发的那个,就像这里一样
我已将Browse ...按钮链接到QFileDialog。现在,当我点击浏览图像按钮时,它会打开一个文件对话框,然后当我选择文件并按OK时,它会重新打开它。其他浏览按钮和包含标签的对话框也是如此。
任何想法,当事件只发生一次时,为什么会发生2次?
这是插槽:
void MySecondQtProject::on_pushImage_clicked()
{
image =
QFileDialog::getOpenFileName(this, tr("open file"), "C:/Users/mankum93/Documents/Visual Studio 2013/Projects/BananaClassifierOpenCV/BananaClassifierOpenCV",
"Images (*.png *.xpm *.jpg)");
}
void MySecondQtProject::on_pushClassifier_clicked()
{
classifier =
QFileDialog::getOpenFileName(this, tr("open file"), "C:/Users/mankum93/Documents/Visual Studio 2013/Projects/BananaClassifierOpenCV/BananaClassifierOpenCV",
"XML files (*.xml)");
}
void MySecondQtProject::on_pushOK_clicked()
{
accept();
//Now lets compare for invalidity of any string:
if (image.isNull() || classifier.isNull())
{
ShowErrorDialog dialog(this);
dialog.exec();
}
else
{
//Convert Qstring to std::string
String imageFileName = image.toLocal8Bit().constData();
String cascadeFileName = classifier.toLocal8Bit().constData();
// Now, we can go to main detection logic
detectObject(imageFileName, cascadeFileName);
}
}
PS:我是QT的新手
编辑1:元对象代码
void MySecondQtProject::qt_static_metacall(QObject *_o, QMetaObject::Call _c, int _id, void **_a)
{
if (_c == QMetaObject::InvokeMetaMethod) {
MySecondQtProject *_t = static_cast<MySecondQtProject *>(_o);
switch (_id) {
case 0: _t->on_pushImage_clicked(); break;
case 1: _t->on_pushClassifier_clicked(); break;
case 2: _t->on_pushOK_clicked(); break;
default: ;
}
}
Q_UNUSED(_a);
}
EDIT2:用户界面文件代码
/********************************************************************************
** Form generated from reading UI file 'mysecondqtprojectHp7256.ui'
**
** Created by: Qt User Interface Compiler version 5.4.1
**
** WARNING! All changes made in this file will be lost when recompiling UI file!
********************************************************************************/
#ifndef MYSECONDQTPROJECTHP7256_H
#define MYSECONDQTPROJECTHP7256_H
#include <QtCore/QVariant>
#include <QtWidgets/QAction>
#include <QtWidgets/QApplication>
#include <QtWidgets/QButtonGroup>
#include <QtWidgets/QDialog>
#include <QtWidgets/QHeaderView>
#include <QtWidgets/QPushButton>
#include <QtWidgets/QVBoxLayout>
#include <QtWidgets/QWidget>
QT_BEGIN_NAMESPACE
class Ui_MySecondQtProjectClass
{
public:
QPushButton *pushOK;
QWidget *layoutWidget;
QVBoxLayout *verticalLayout;
QPushButton *pushImage;
QPushButton *pushClassifier;
void setupUi(QDialog *MySecondQtProjectClass)
{
if (MySecondQtProjectClass->objectName().isEmpty())
MySecondQtProjectClass->setObjectName(QStringLiteral("MySecondQtProjectClass"));
MySecondQtProjectClass->resize(600, 272);
pushOK = new QPushButton(MySecondQtProjectClass);
pushOK->setObjectName(QStringLiteral("pushOK"));
pushOK->setGeometry(QRect(290, 220, 91, 31));
layoutWidget = new QWidget(MySecondQtProjectClass);
layoutWidget->setObjectName(QStringLiteral("layoutWidget"));
layoutWidget->setGeometry(QRect(200, 100, 181, 91));
verticalLayout = new QVBoxLayout(layoutWidget);
verticalLayout->setSpacing(6);
verticalLayout->setContentsMargins(11, 11, 11, 11);
verticalLayout->setObjectName(QStringLiteral("verticalLayout"));
verticalLayout->setContentsMargins(0, 0, 0, 0);
pushImage = new QPushButton(layoutWidget);
pushImage->setObjectName(QStringLiteral("pushImage"));
verticalLayout->addWidget(pushImage);
pushClassifier = new QPushButton(layoutWidget);
pushClassifier->setObjectName(QStringLiteral("pushClassifier"));
verticalLayout->addWidget(pushClassifier);
retranslateUi(MySecondQtProjectClass);
QObject::connect(pushImage, SIGNAL(clicked()), MySecondQtProjectClass, SLOT(on_pushImage_clicked()));
QObject::connect(pushClassifier, SIGNAL(clicked()), MySecondQtProjectClass, SLOT(on_pushClassifier_clicked()));
QObject::connect(pushOK, SIGNAL(clicked()), MySecondQtProjectClass, SLOT(on_pushOK_clicked()));
QMetaObject::connectSlotsByName(MySecondQtProjectClass);
} // setupUi
void retranslateUi(QDialog *MySecondQtProjectClass)
{
MySecondQtProjectClass->setWindowTitle(QApplication::translate("MySecondQtProjectClass", "MySecondQtProject", 0));
pushOK->setText(QApplication::translate("MySecondQtProjectClass", "OK", 0));
pushImage->setText(QApplication::translate("MySecondQtProjectClass", "Browse for the Image", 0));
pushClassifier->setText(QApplication::translate("MySecondQtProjectClass", "Browse for the classifier", 0));
} // retranslateUi
};
namespace Ui {
class MySecondQtProjectClass: public Ui_MySecondQtProjectClass {};
} // namespace Ui
QT_END_NAMESPACE
#endif // MYSECONDQTPROJECTHP7256_H
现在,代码为另一个errordialog:
/********************************************************************************
** Form generated from reading UI file 'showerrordialogmU7256.ui'
**
** Created by: Qt User Interface Compiler version 5.4.1
**
** WARNING! All changes made in this file will be lost when recompiling UI file!
********************************************************************************/
#ifndef SHOWERRORDIALOGMU7256_H
#define SHOWERRORDIALOGMU7256_H
#include <QtCore/QVariant>
#include <QtWidgets/QAction>
#include <QtWidgets/QApplication>
#include <QtWidgets/QButtonGroup>
#include <QtWidgets/QDialog>
#include <QtWidgets/QHeaderView>
#include <QtWidgets/QLabel>
QT_BEGIN_NAMESPACE
class Ui_ShowErrorDialog
{
public:
QLabel *label;
void setupUi(QDialog *ShowErrorDialog)
{
if (ShowErrorDialog->objectName().isEmpty())
ShowErrorDialog->setObjectName(QStringLiteral("ShowErrorDialog"));
ShowErrorDialog->resize(400, 65);
label = new QLabel(ShowErrorDialog);
label->setObjectName(QStringLiteral("label"));
label->setGeometry(QRect(70, 20, 241, 21));
retranslateUi(ShowErrorDialog);
QMetaObject::connectSlotsByName(ShowErrorDialog);
} // setupUi
void retranslateUi(QDialog *ShowErrorDialog)
{
ShowErrorDialog->setWindowTitle(QApplication::translate("ShowErrorDialog", "ShowErrorDialog", 0));
label->setText(QApplication::translate("ShowErrorDialog", "Please properly select the Image or the classifier", 0));
} // retranslateUi
};
namespace Ui {
class ShowErrorDialog: public Ui_ShowErrorDialog {};
} // namespace Ui
QT_END_NAMESPACE
#endif // SHOWERRORDIALOGMU7256_H
主Cpp文件的代码
#include "mysecondqtproject.h"
#include "showerrordialog.h"
#include <QFileDialog>
#include "objectdetect.h"
MySecondQtProject::MySecondQtProject(QWidget *parent)
: QDialog(parent)
{
ui.setupUi(this);
}
MySecondQtProject::~MySecondQtProject()
{
}
void MySecondQtProject::on_pushImage_clicked()
{
image =
QFileDialog::getOpenFileName(this, tr("open file"), "C:/Users/mankum93/Documents/Visual Studio 2013/Projects/BananaClassifierOpenCV/BananaClassifierOpenCV",
"Images (*.png *.xpm *.jpg)");
}
void MySecondQtProject::on_pushClassifier_clicked()
{
classifier =
QFileDialog::getOpenFileName(this, tr("open file"), "C:/Users/mankum93/Documents/Visual Studio 2013/Projects/BananaClassifierOpenCV/BananaClassifierOpenCV",
"XML files (*.xml)");
}
void MySecondQtProject::on_pushOK_clicked()
{
accept();
//Now lets compare for invalidity of any string:
if (image.isNull() || classifier.isNull())
{
ShowErrorDialog dialog(this);
dialog.exec();
}
else
{
//Convert Qstring to std::string
String imageFileName = image.toLocal8Bit().constData();
String cascadeFileName = classifier.toLocal8Bit().constData();
// Now, we can go to main detection logic
detectObject(imageFileName, cascadeFileName);
}
}
编辑3:以下是您可能希望看到的其他有用文件
/********************************************************************************
** Form generated from reading UI file 'mysecondqtproject.ui'
**
** Created by: Qt User Interface Compiler version 5.4.1
**
** WARNING! All changes made in this file will be lost when recompiling UI file!
********************************************************************************/
#ifndef UI_MYSECONDQTPROJECT_H
#define UI_MYSECONDQTPROJECT_H
#include <QtCore/QVariant>
#include <QtWidgets/QAction>
#include <QtWidgets/QApplication>
#include <QtWidgets/QButtonGroup>
#include <QtWidgets/QDialog>
#include <QtWidgets/QHeaderView>
#include <QtWidgets/QPushButton>
#include <QtWidgets/QVBoxLayout>
#include <QtWidgets/QWidget>
QT_BEGIN_NAMESPACE
class Ui_MySecondQtProjectClass
{
public:
QPushButton *pushOK;
QWidget *layoutWidget;
QVBoxLayout *verticalLayout;
QPushButton *pushImage;
QPushButton *pushClassifier;
void setupUi(QDialog *MySecondQtProjectClass)
{
if (MySecondQtProjectClass->objectName().isEmpty())
MySecondQtProjectClass->setObjectName(QStringLiteral("MySecondQtProjectClass"));
MySecondQtProjectClass->resize(600, 272);
pushOK = new QPushButton(MySecondQtProjectClass);
pushOK->setObjectName(QStringLiteral("pushOK"));
pushOK->setGeometry(QRect(290, 220, 91, 31));
layoutWidget = new QWidget(MySecondQtProjectClass);
layoutWidget->setObjectName(QStringLiteral("layoutWidget"));
layoutWidget->setGeometry(QRect(200, 100, 181, 91));
verticalLayout = new QVBoxLayout(layoutWidget);
verticalLayout->setSpacing(6);
verticalLayout->setContentsMargins(11, 11, 11, 11);
verticalLayout->setObjectName(QStringLiteral("verticalLayout"));
verticalLayout->setContentsMargins(0, 0, 0, 0);
pushImage = new QPushButton(layoutWidget);
pushImage->setObjectName(QStringLiteral("pushImage"));
verticalLayout->addWidget(pushImage);
pushClassifier = new QPushButton(layoutWidget);
pushClassifier->setObjectName(QStringLiteral("pushClassifier"));
verticalLayout->addWidget(pushClassifier);
retranslateUi(MySecondQtProjectClass);
QObject::connect(pushImage, SIGNAL(clicked()), MySecondQtProjectClass, SLOT(on_pushImage_clicked()));
QObject::connect(pushClassifier, SIGNAL(clicked()), MySecondQtProjectClass, SLOT(on_pushClassifier_clicked()));
QObject::connect(pushOK, SIGNAL(clicked()), MySecondQtProjectClass, SLOT(on_pushOK_clicked()));
QMetaObject::connectSlotsByName(MySecondQtProjectClass);
} // setupUi
void retranslateUi(QDialog *MySecondQtProjectClass)
{
MySecondQtProjectClass->setWindowTitle(QApplication::translate("MySecondQtProjectClass", "MySecondQtProject", 0));
pushOK->setText(QApplication::translate("MySecondQtProjectClass", "OK", 0));
pushImage->setText(QApplication::translate("MySecondQtProjectClass", "Browse for the Image", 0));
pushClassifier->setText(QApplication::translate("MySecondQtProjectClass", "Browse for the classifier", 0));
} // retranslateUi
};
namespace Ui {
class MySecondQtProjectClass: public Ui_MySecondQtProjectClass {};
} // namespace Ui
QT_END_NAMESPACE
#endif // UI_MYSECONDQTPROJECT_H
第二个:
/********************************************************************************
** Form generated from reading UI file 'showerrordialog.ui'
**
** Created by: Qt User Interface Compiler version 5.4.1
**
** WARNING! All changes made in this file will be lost when recompiling UI file!
********************************************************************************/
#ifndef UI_SHOWERRORDIALOG_H
#define UI_SHOWERRORDIALOG_H
#include <QtCore/QVariant>
#include <QtWidgets/QAction>
#include <QtWidgets/QApplication>
#include <QtWidgets/QButtonGroup>
#include <QtWidgets/QDialog>
#include <QtWidgets/QHeaderView>
#include <QtWidgets/QLabel>
QT_BEGIN_NAMESPACE
class Ui_ShowErrorDialog
{
public:
QLabel *label;
void setupUi(QDialog *ShowErrorDialog)
{
if (ShowErrorDialog->objectName().isEmpty())
ShowErrorDialog->setObjectName(QStringLiteral("ShowErrorDialog"));
ShowErrorDialog->resize(400, 65);
label = new QLabel(ShowErrorDialog);
label->setObjectName(QStringLiteral("label"));
label->setGeometry(QRect(70, 20, 241, 21));
retranslateUi(ShowErrorDialog);
QMetaObject::connectSlotsByName(ShowErrorDialog);
} // setupUi
void retranslateUi(QDialog *ShowErrorDialog)
{
ShowErrorDialog->setWindowTitle(QApplication::translate("ShowErrorDialog", "ShowErrorDialog", 0));
label->setText(QApplication::translate("ShowErrorDialog", "Please properly select the Image or the classifier", 0));
} // retranslateUi
};
namespace Ui {
class ShowErrorDialog: public Ui_ShowErrorDialog {};
} // namespace Ui
QT_END_NAMESPACE
#endif // UI_SHOWERRORDIALOG_H