C++ 子类中未调用QT5插槽
我有一个超类C++ 子类中未调用QT5插槽,c++,qt,signals-slots,C++,Qt,Signals Slots,我有一个超类BackgroundWorkerWithWaitDialog,它管理一个WaitDialog,实现一个带有“中止”按钮的进度条框。它用作QThread中的QObject 我的意图是从BackgroundWorker派生任何后台任务,只实现execute()命令,轮询中止标志以停止它 BackgroundWorkerWithWaitDialog源自BackgroundWorker,它是一个QObject,所有类都是Q_对象,工人类能够使用信号和插槽更新gui。此通信(工作人员到Gui对
BackgroundWorkerWithWaitDialog
,它管理一个WaitDialog
,实现一个带有“中止”按钮的进度条框。它用作QThread中的QObject
我的意图是从BackgroundWorker派生任何后台任务,只实现execute()
命令,轮询中止
标志以停止它
BackgroundWorkerWithWaitDialog源自BackgroundWorker
,它是一个QObject,所有类都是Q_对象,工人类能够使用信号和插槽更新gui。此通信(工作人员到Gui对象)工作正常
问题在于,尽管WaitDialog响应按钮点击,但是,中止()
信号从WaitDialog发出,但BackGroundWorker没有收到。(Gui->Worker)
我有什么遗漏吗?我暂时用侦听器模式修复了这个问题,但坦率地说,我不喜欢这种混合修复
为什么不调用已中止的插槽
提前谢谢
因此,总结一下:
- BackgroundWorkerWithWaitDialog(BWWD)派生自从QObject继承的BackgroundWorker
- BackgroundWorkerWithWaitDialog在构造函数中接收从QDialog派生的WaitDialog(WD)
- 构造函数connect()中的BWWWD将waitDialog aborted()信号与abortIssued()插槽连接起来
- WD发出信号,但未调用abortIssued
- BWWD在单独的QThread中执行
值得一提的是,BWWD类是由其他特定的Worker类派生的,这些类实现了特定的函数,这些函数利用中止的()函数来中断处理。您的问题指出: 我有一个超类BackgroundWorker,它管理一个WaitDialog,该对话框实现了一个带有“中止”按钮的进度条框 在
BackgroundWorkerWithWaitDialog
构造函数中,您不会将dialog
传递给BackgroundWorker
的构造函数,也不会在设置连接时使用BackgroundWorker
成员变量
您的问题开场白有误,或者您正在连接一个从未使用过的对话框。这不会提供问题的答案。要评论或要求作者澄清,请在他们的帖子下方留下评论。你是对的,我正在更正文章。我想简化上下文,但实际上我忘了在代码中更改一些名称。BackgroundWorker对dialog一无所知,它是QThread中使用的通用QObject;waitdialog在BackgroundWorker WithGUI中正确连接。@HappyCactus,那么为了确认,您可以完全不考虑
BackgroundWorker
,它不会改变任何东西吗?如果是这样的话,请继续编辑。嗯,不是100%确定,但是我相信BW类可以被忽略。WaitDialog仅与BWWD有关,并且与两者之间的信号/插槽有关。谢谢。@HappyCactus,所以我将首先使“BWWWD”成为一个不从“BW”继承的类,然后看看会发生什么。如果这不能解决您需要调整问题的问题,因为这是一个与继承无关的connect
问题。
class WaitDialog : public QDialog
{
Q_OBJECT
public:
explicit WaitDialog(QWidget *parent = 0);
~WaitDialog();
public slots:
void setText(QString text);
void setProgress(bool shown, int max);
void enableAbort(bool enable);
void setProgression (int level);
signals:
void aborted();
private slots:
void on_cmdAbort_clicked();
private:
Ui::WaitDialog *ui;
};
void WaitDialog::on_cmdAbort_clicked()
{
qDebug() << "ABORT";
emit aborted();
}
/// BackgroundWorker is QObject-derived.
class BackgroundWorkerWithWaitDialog : public BackgroundWorker
{
Q_OBJECT
public:
explicit BackgroundWorkerWithWaitDialog(MainWindow *main, WaitDialog *dialog);
...
public slots:
virtual void abortIssued();
BackgroundWorkerWithWaitDialog::BackgroundWorkerWithWaitDialog(MainWindow *main, WaitDialog *dialog)
: BackgroundWorker(main),
mWaitDialog(dialog),
mAborted(false)
{
connect (this, SIGNAL(progress(int)), mWaitDialog, SLOT(setProgression(int)));
connect (this, SIGNAL(messageChanged(QString)), mWaitDialog, SLOT(setText(QString)));
connect (this, SIGNAL(progressBarVisibilityChanged(bool,int)), mWaitDialog, SLOT(setProgress(bool,int)));
connect (this, SIGNAL(abortButtonVisibilityChanged(bool)), mWaitDialog, SLOT(enableAbort(bool)));
connect (mWaitDialog, SIGNAL(aborted()), this, SLOT(abortIssued()));
}
void BackgroundWorkerWithWaitDialog::abortIssued()
{
// THIS IS NEVER EXECUTED
mAborted = true;
}