QObjectクラスの外からメッセージボックスを表示して取得しようとしています。私はこのようなダイアログを生成することができるように見える:QObjectの外からQMessageBoxの結果を表示して取得する
#include <iostream>
#include <QApplication>
#include <QtConcurrentRun>
#include <QMessageBox>
class DialogHandler : public QObject
{
Q_OBJECT
signals:
void MySignal(int* returnValue);
public:
DialogHandler()
{
connect(this, SIGNAL(MySignal(int*)), this, SLOT(MySlot(int*)), Qt::BlockingQueuedConnection);
}
void EmitSignal(int* returnValue)
{
emit MySignal(returnValue);
}
public slots:
void MySlot(int* returnValue)
{
std::cout << "input: " << *returnValue << std::endl;
QMessageBox* dialog = new QMessageBox;
dialog->addButton(QMessageBox::Yes);
dialog->addButton(QMessageBox::No);
dialog->setText("Test Text");
dialog->exec();
int result = dialog->result();
if(result == QMessageBox::Yes)
{
*returnValue = 1;
}
else
{
*returnValue = 0;
}
}
};
#include "main.moc" // For CMake's automoc
void MyFunction(DialogHandler* dialogHandler)
{
int returnValue = -1;
dialogHandler->EmitSignal(&returnValue);
std::cout << "returnValue: " << returnValue << std::endl;
}
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
DialogHandler* dialogHandler = new DialogHandler;
QtConcurrent::run(MyFunction, dialogHandler);
std::cout << "End" << std::endl;
return app.exec();
}
:
#include <iostream>
#include <QApplication>
#include <QtConcurrentRun>
#include <QMessageBox>
class DialogHandler : public QObject
{
Q_OBJECT
signals:
void MySignal();
public:
DialogHandler()
{
connect(this, SIGNAL(MySignal()), this, SLOT(MySlot()));
}
void EmitSignal()
{
emit MySignal();
}
public slots:
void MySlot()
{
QMessageBox* dialog = new QMessageBox;
dialog->setText("Test Text");
dialog->exec();
int result = dialog->result();
if(result)
{
std::cout << "ok" << std::endl;
}
else
{
std::cout << "invalid" << std::endl;
}
}
};
#include "main.moc" // For CMake's automoc
void MyFunction(DialogHandler* dialogHandler)
{
dialogHandler->EmitSignal();
}
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
DialogHandler* dialogHandler = new DialogHandler;
MyFunction(dialogHandler);
return app.exec();
}
バックのMyFunctionで結果を得るためには、このような結果を記入するだけで行うオブジェクトを渡すために動作するようです
これは妥当と思われますか?それを行うより良い方法はありますか?
複数のスレッドを使用していますか?スロットにmsgboxを表示する以外に何もしないで、MyClassにQObject派生クラスを作成したり、他のスレッドのオブジェクトをウィジェットに接続してメッセージボックスを表示することができます。 –
ああ、私はそれをQObjectクラスに属さないことに帰していましたが、あなたはそのクラスが同じスレッドにないので、それが正しいことは間違いありません。私はここにいるので、信号とスロットを接続する方法はまだ分かりません。 Form :: MyClass :: MyFunction() { ...機能しています... ...エラーが発生します。 .. ProduceMessageBox() } MyClassはQObjectではないので、信号を送信できないため、Formの関数を呼び出すにはどうすればよいですか? –